My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
/**
* BlueCove - Java library for Bluetooth
* Copyright (C) 2006-2009 Vlad Skarzhevskyy
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* @author vlads
* @version $Id$
*/
package com.intel.bluetooth;

import java.util.Calendar;
import java.util.Date;
import java.util.Enumeration;
import java.util.Vector;

/**
* BlueCove log system.
*
* If enabled "-Dbluecove.debug=true" System.out.println would be used for debug. Alternatively if log4j is available in
* classpath Bluecove log would be redirected to log4j and can be enable using log4j configuration.
*
* The methods of this class would be removed automaticaly because they are empty if debugCompiledOut = true. This class
* itself will disappear from bytecode after obfuscation by proguard in this case. Change to create mini jar.
*
*/
public abstract class DebugLog {

private static final boolean debugCompiledOut = false;

public final static int DEBUG = 1;

public final static int ERROR = 4;

private static boolean debugEnabled = false;

private static boolean initialized = false;

private static boolean debugInternalEnabled = false;

private static final String FQCN = DebugLog.class.getName();

private static final Vector fqcnSet = new Vector();

private static boolean java13 = false;

private static Vector loggerAppenders = new Vector();

/**
* Different log system can be injected in BlueCove using DebugLog.addAppender(customLoggerAppender)
*
*/
public static interface LoggerAppender {
public void appendLog(int level, String message, Throwable throwable);
}

public static interface LoggerAppenderExt extends LoggerAppender {
public boolean isLogEnabled(int level);
}

static {
fqcnSet.addElement(FQCN);
}

private DebugLog() {

}

private synchronized static void initialize() {
if (initialized) {
return;
}
initialized = true;
debugEnabled = BlueCoveImpl.getConfigProperty(BlueCoveConfigProperties.PROPERTY_DEBUG, false);
if (debugEnabled && debugCompiledOut) {
debugEnabled = false;
System.err.println("BlueCove debug functions have been Compiled Out");
}
boolean useStdOut = BlueCoveImpl.getConfigProperty(BlueCoveConfigProperties.PROPERTY_DEBUG_STDOUT, true);
debugInternalEnabled = useStdOut && debugEnabled;
boolean useLog4j = BlueCoveImpl.getConfigProperty(BlueCoveConfigProperties.PROPERTY_DEBUG_LOG4J, true);
if (useLog4j) {
try {
LoggerAppenderExt log4jAppender = (LoggerAppenderExt) Class.forName(
"com.intel.bluetooth.DebugLog4jAppender").newInstance();
System.out.println("BlueCove log redirected to log4j");
addAppender(log4jAppender);
if (log4jAppender.isLogEnabled(DEBUG)) {
debugEnabled = true || debugEnabled;
}
} catch (Throwable e) {
}
}
}

public static boolean isDebugEnabled() {
if (!initialized) {
initialize();
}
return debugEnabled;
}

public static void setDebugEnabled(boolean debugEnabled) {
if (!initialized) {
initialize();
}
if (debugEnabled && debugCompiledOut) {
debugEnabled = false;
System.err.println("BlueCove debug functions have been Compiled Out");
} else {
BlueCoveImpl.instance().enableNativeDebug(debugEnabled);
DebugLog.debugEnabled = debugEnabled;
DebugLog.debugInternalEnabled = DebugLog.debugEnabled;
}
}

public static void debug(String message) {
if (!debugCompiledOut && isDebugEnabled()) {
log(message, null, null);
printLocation();
callAppenders(DEBUG, message, null);
}
}

public static void debug(String message, String v) {
if (!debugCompiledOut && isDebugEnabled()) {
log(message, " ", v);
printLocation();
callAppenders(DEBUG, message + " " + v, null);
}
}

public static void debug(String message, Throwable t) {
if (!debugCompiledOut && isDebugEnabled()) {
log(message, " ", t.toString());
printLocation();
// I have the reson not to make this as function.
if (!UtilsJavaSE.javaSECompiledOut) {
if (!UtilsJavaSE.ibmJ9midp) {
t.printStackTrace(System.out);
} else if (debugInternalEnabled) {
t.printStackTrace();
}
} else {
t.printStackTrace();
}
callAppenders(DEBUG, message, t);
}
}

public static void debug(String message, Object obj) {
if (!debugCompiledOut && isDebugEnabled()) {
log(message, " ", obj.toString());
printLocation();
callAppenders(DEBUG, message + " " + obj.toString(), null);
}
}

public static void debug(String message, String v, String v2) {
if (!debugCompiledOut && isDebugEnabled()) {
log(message, " ", v + " " + v2);
printLocation();
callAppenders(DEBUG, message + " " + v + " " + v2, null);
}
}

public static void debug(String message, long v) {
if (!debugCompiledOut && isDebugEnabled()) {
log(message, " ", Long.toString(v));
printLocation();
callAppenders(DEBUG, message + " " + Long.toString(v), null);
}
}

public static void debug0x(String message, long v) {
if (!debugCompiledOut && isDebugEnabled()) {
log(message, " 0x", Utils.toHexString(v));
printLocation();
callAppenders(DEBUG, message + " 0x" + Utils.toHexString(v), null);
}
}

public static void debug0x(String message, String s, long v) {
if (!debugCompiledOut && isDebugEnabled()) {
log(message, " " + s + " 0x", Utils.toHexString(v));
printLocation();
callAppenders(DEBUG, message + " " + s + " 0x" + Utils.toHexString(v), null);
}
}

public static void debug(String message, boolean v) {
if (!debugCompiledOut && isDebugEnabled()) {
log(message, " ", String.valueOf(v));
printLocation();
callAppenders(DEBUG, message + " " + v, null);
}
}

public static void debug(String message, byte[] data) {
debug(message, data, 0, (data == null) ? 0 : data.length);
}

public static void debug(String message, byte[] data, int off, int len) {
if (!debugCompiledOut && isDebugEnabled()) {
StringBuffer buf = new StringBuffer();
if (data == null) {
buf.append(" null byte[]");
} else {
buf.append(" [");
for (int i = off; i < off + len; i++) {
if (i != off) {
buf.append(", ");
}
buf.append((new Byte(data[i])).toString());
}
buf.append("]");
if (len > 4) {
buf.append(" ").append(len);
}
}
log(message, buf.toString(), null);
printLocation();
callAppenders(DEBUG, message + buf.toString(), null);
}
}

public static void debug(String message, int[] data) {
debug(message, data, 0, (data == null) ? 0 : data.length);
}

public static void debug(String message, int[] data, int off, int len) {
if (!debugCompiledOut && isDebugEnabled()) {
StringBuffer buf = new StringBuffer();
if (data == null) {
buf.append(" null int[]");
} else {
buf.append(" [");
for (int i = off; i < off + len; i++) {
if (i != off) {
buf.append(", ");
}
buf.append(Integer.toString(data[i]));
}
buf.append("]");
if (len > 4) {
buf.append(" ").append(len);
}
}
log(message, buf.toString(), null);
printLocation();
callAppenders(DEBUG, message + buf.toString(), null);
}
}

public static void nativeDebugCallback(String fileName, int lineN, String message) {
try {
if ((fileName != null) && fileName.startsWith(".\\")) {
fileName = fileName.substring(2);
}
DebugLog.debugNative(fileName + ":" + lineN, message);
} catch (Throwable e) {
try {
System.out.println("Error when calling debug " + e);
} catch (Throwable e2) {
// We don't want any Exception propagate to Native Code.
}
}
}

public static void debugNative(String location, String message) {
if (!debugCompiledOut && isDebugEnabled()) {
log(message, "\n\t ", location);
callAppenders(DEBUG, message, null);
}
}

public static void error(String message) {
if (!debugCompiledOut && isDebugEnabled()) {
log("error ", message, null);
printLocation();
callAppenders(ERROR, message, null);
}
}

public static void error(String message, long v) {
if (!debugCompiledOut && isDebugEnabled()) {
log("error ", message, " " + v);
printLocation();
callAppenders(ERROR, message + " " + v, null);
}
}

public static void error(String message, String v) {
if (!debugCompiledOut && isDebugEnabled()) {
log("error ", message, " " + v);
printLocation();
callAppenders(ERROR, message + " " + v, null);
}
}

public static void error(String message, Throwable t) {
if (!debugCompiledOut && isDebugEnabled()) {
log("error ", message, " " + t);
printLocation();
// I have the reson not to make this as function.
if (!UtilsJavaSE.javaSECompiledOut) {
if (!UtilsJavaSE.ibmJ9midp) {
t.printStackTrace(System.out);
} else if (debugInternalEnabled) {
t.printStackTrace();
}
} else {
t.printStackTrace();
}

callAppenders(ERROR, message, t);
}
}

public static void fatal(String message) {
log("error ", message, null);
printLocation();
callAppenders(ERROR, message, null);
}

public static void fatal(String message, Throwable t) {
log("error ", message, " " + t);
printLocation();
// I have the reson not to make this as function.
if (!UtilsJavaSE.javaSECompiledOut) {
if (!UtilsJavaSE.ibmJ9midp) {
t.printStackTrace(System.out);
} else if (debugInternalEnabled) {
t.printStackTrace();
}
} else {
t.printStackTrace();
}

callAppenders(ERROR, message, t);
}

private static void callAppenders(int level, String message, Throwable throwable) {
for (Enumeration iter = loggerAppenders.elements(); iter.hasMoreElements();) {
LoggerAppender a = (LoggerAppender) iter.nextElement();
a.appendLog(level, message, throwable);
}
}

public static void addAppender(LoggerAppender newAppender) {
loggerAppenders.addElement(newAppender);
}

public static void removeAppender(LoggerAppender newAppender) {
loggerAppenders.removeElement(newAppender);
}

private static String d00(int i) {
if (i > 9) {
return String.valueOf(i);
} else {
return "0" + String.valueOf(i);
}
}

private static String d000(int i) {
if (i > 99) {
return String.valueOf(i);
} else if (i > 9) {
return "0" + String.valueOf(i);
} else {
return "00" + String.valueOf(i);
}
}

private static void log(String message, String va1, String va2) {
if (!debugInternalEnabled) {
return;
}
try {
Calendar calendar = Calendar.getInstance();
calendar.setTime(new Date(System.currentTimeMillis()));

StringBuffer sb;
sb = new StringBuffer();
sb.append(d00(calendar.get(Calendar.HOUR_OF_DAY))).append(":");
sb.append(d00(calendar.get(Calendar.MINUTE))).append(":");
sb.append(d00(calendar.get(Calendar.SECOND))).append(".");
sb.append(d000(calendar.get(Calendar.MILLISECOND))).append(" ");

sb.append(message);
if (va1 != null) {
sb.append(va1);
}
if (va2 != null) {
sb.append(va2);
}

System.out.println(sb.toString());
} catch (Throwable ignore) {
}
}

private static void printLocation() {
if (java13 || !debugInternalEnabled) {
return;
}
try {
UtilsJavaSE.StackTraceLocation ste = UtilsJavaSE.getLocation(fqcnSet);
if (ste != null) {
System.out.println("\t " + formatLocation(ste));
}
} catch (Throwable e) {
java13 = true;
}
}

private static String formatLocation(UtilsJavaSE.StackTraceLocation ste) {
if (ste == null) {
return "";
}
// Make Line# clickable in eclipse
return ste.className + "." + ste.methodName + "(" + ste.fileName + ":" + ste.lineNumber + ")";
}

}

Change log

r2915 by skarzhevskyy on Mar 13, 2009   Diff
Updated javadocs and Copyright
Go to: 
Project members, sign in to write a code review

Older revisions

r2505 by skarzhevskyy on Dec 5, 2008   Diff
Testing many devices on Linux
r2476 by skarzhevskyy on Dec 1, 2008   Diff
Correction to headers
r2416 by skarzhevskyy on Oct 9, 2008   Diff
Change license to Apache License,
Version 2.0
All revisions of this file

File info

Size: 12279 bytes, 441 lines

File properties

svn:mime-type
text/plain
svn:eol-style
native
svn:keywords
Date Author Id Revision
Powered by Google Project Hosting