summaryrefslogtreecommitdiff
path: root/SDL_Android/SmartDeviceLinkProxyAndroid/src/com/smartdevicelink/util/DebugTool.java
blob: 932115594e9a21798da2cfa74f7c5f5313f9818c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
package com.smartdevicelink.util;

import java.util.Vector;

import android.util.Log;

import com.smartdevicelink.exception.SmartDeviceLinkException;
import com.smartdevicelink.proxy.Version;
import com.smartdevicelink.transport.SiphonServer;

public class DebugTool {
	

	public static final String TAG = "SmartDeviceLinkProxy";

	private static boolean isErrorEnabled = false;
	private static boolean isWarningEnabled = false;
	private static boolean isInfoEnabled = false;
	
	public static void enableDebugTool() {
		isErrorEnabled = true;
		isWarningEnabled = true;
		isInfoEnabled = true;
	}

	public static void disableDebugTool() {
		isErrorEnabled = true;
		isWarningEnabled = false;
		isInfoEnabled = false;
	}
	
	public static boolean isDebugEnabled() 
	{
		if (isWarningEnabled && isInfoEnabled) return true;
		
		return false;		
	}
	
	private static String prependProxyVersionNumberToString(String string) {
		if (Version.VERSION != null && string != null) {
			string = Version.VERSION + ": " + string;
		}
		
		return string;
	}

	public static void logError(String msg) {
		
		Boolean wasWritten = false;
		
		msg = prependProxyVersionNumberToString(msg);
		
		wasWritten = logToSiphon(msg);
		
		if (isErrorEnabled && !wasWritten) {
			NativeLogTool.logError(TAG, msg);
		}
	}

	public static void logError(String msg, Throwable ex) {
		Boolean wasWritten = false;
		
		msg = prependProxyVersionNumberToString(msg);
		
		if (ex != null) {
			wasWritten = logToSiphon(msg + " Exception String: " + ex.toString());
		} else {
			wasWritten = logToSiphon(msg);
		}
		
		if (isErrorEnabled && !wasWritten) {
			NativeLogTool.logError(TAG, msg, ex);
		}
	}
	
	public static void logWarning(String msg) {
		Boolean wasWritten = false;
		
		msg = prependProxyVersionNumberToString(msg);
		
		wasWritten = logToSiphon(msg);
		
		if (isWarningEnabled && !wasWritten) {
			NativeLogTool.logWarning(TAG, msg);
		}
	}

	public static void logInfo(String msg) {
		Boolean wasWritten = false;
		
		msg = prependProxyVersionNumberToString(msg);
		
		wasWritten = logToSiphon(msg);
		
		if (isInfoEnabled && !wasWritten) {
			NativeLogTool.logInfo(TAG, msg);
		}
	}

	public static void logInfo(String msg, boolean bPrependVersion) {
		Boolean wasWritten = false;
		
		if (bPrependVersion) msg = prependProxyVersionNumberToString(msg);
		
		wasWritten = logToSiphon(msg);
		
		if (isInfoEnabled && !wasWritten) {
			NativeLogTool.logInfo(TAG, msg);
		}
	}
	
	protected static Boolean logToSiphon(String msg) {
		// Initialize the SiphonServer, will be ignored if already initialized
		SiphonServer.init();
		
		// Write to the SiphonServer
		return SiphonServer.sendSiphonLogData(msg);
	}

	protected static String getLine(Throwable ex) {
		if (ex == null) { return null; }
		String toPrint = ex.toString() + " :" + ex.getMessage();
		for (int i=0; i<ex.getStackTrace().length; i++) {
			StackTraceElement elem = ex.getStackTrace()[i];
			toPrint += "\n  " + elem.toString();
		}
		
		if (ex instanceof SmartDeviceLinkException) {
			SmartDeviceLinkException smartDeviceLinkEx = (SmartDeviceLinkException) ex;
			if (smartDeviceLinkEx.getInnerException() != null && smartDeviceLinkEx != smartDeviceLinkEx.getInnerException()) {
				toPrint += "\n  nested:\n";
				toPrint += getLine(smartDeviceLinkEx.getInnerException());
			}
		}
		
		return toPrint;
	}


	protected static Vector<IConsole> consoleListenerList = new Vector<IConsole>();

	protected final static boolean isTransportEnabled = false;
	protected final static boolean isRPCEnabled = false;

	public static void addConsole(IConsole console) {
		synchronized(consoleListenerList) {
			consoleListenerList.addElement(console);
		}
	}

	public static void removeConsole(IConsole console) {
		synchronized(consoleListenerList) {
			consoleListenerList.removeElement(console);
		}
	}

	public static void clearConsoles() {
		synchronized(consoleListenerList) {
			consoleListenerList.removeAllElements();
		}
	}
	
	public static void logTransport(String msg) {
		if (isTransportEnabled) {
			Log.d(TAG, msg);
			logInfoToConsole(msg);
		}
	}

	public static void logRPCSend(String rpcMsg) {
		if (isRPCEnabled) {
			Log.d(TAG, "Sending RPC message: " + rpcMsg);
			logRPCSendToConsole(rpcMsg);
		}
	}

	public static void logRPCReceive(String rpcMsg) {
		if (isRPCEnabled) {
			Log.d(TAG, "Received RPC message: " + rpcMsg);
			logRPCSendToConsole(rpcMsg);
		}
	}

	protected static void logInfoToConsole(String msg) {
		Vector<IConsole> localList;
		synchronized(consoleListenerList) {
			localList = (Vector<IConsole>) consoleListenerList.clone();
		}
		
		for (int i = 0; i < localList.size(); i++) {
			IConsole consoleListener = (IConsole) localList.elementAt(i);
			try {
				consoleListener.logInfo(msg);
			} catch (Exception ex) {
				Log.e(TAG, "Failure propagating logInfo: " + ex.toString(), ex);
			} // end-catch
		}
	}
	
	protected static void logErrorToConsole(String msg) {
		Vector<IConsole> localList;
		synchronized(consoleListenerList) {
			localList = (Vector<IConsole>) consoleListenerList.clone();
		}
		for (int i = 0; i < localList.size(); i++) {
			IConsole consoleListener = (IConsole) localList.elementAt(i);
			try {
				consoleListener.logError(msg);
			} catch (Exception ex) {
				Log.e(TAG, "Failure propagating logError: " + ex.toString(), ex);
			} // end-catch
		}
	}
	
	protected static void logErrorToConsole(String msg, Throwable e) {
		Vector<IConsole> localList;
		synchronized(consoleListenerList) {
			localList = (Vector<IConsole>) consoleListenerList.clone();
		}
		
		for (int i = 0; i < localList.size(); i++) {
			IConsole consoleListener = (IConsole) localList.elementAt(i);
			try {
				consoleListener.logError(msg, e);
			} catch (Exception ex) {
				Log.e(TAG, "Failure propagating logError: " + ex.toString(), ex);
			} // end-catch
		}
	}
	
	protected static void logRPCSendToConsole(String msg) {
		Vector<IConsole> localList;
		synchronized(consoleListenerList) {
			localList = (Vector<IConsole>) consoleListenerList.clone();
		}
		
		for (int i = 0; i < localList.size(); i++) {
			IConsole consoleListener = (IConsole) localList.elementAt(i);
			try {
				consoleListener.logRPCSend(msg);
			} catch (Exception ex) {
				Log.e(TAG, "Failure propagating logRPCSend: " + ex.toString(), ex);
			} // end-catch
		}
	}
	
	protected static void logRPCReceiveToConsole(String msg) {
		Vector<IConsole> localList;
		synchronized(consoleListenerList) {
			localList = (Vector<IConsole>) consoleListenerList.clone();
		}
		
		for (int i = 0; i < localList.size(); i++) {
			IConsole consoleListener = (IConsole) localList.elementAt(i);
			try {
				consoleListener.logRPCReceive(msg);
			} catch (Exception ex) {
				Log.e(TAG, "Failure propagating logRPCReceive: " + ex.toString(), ex);
			} // end-catch
		}
	}
}