Debug.java
7.8 KB
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
package com.dinhcv.lifelogpedometer.utils;
import android.util.Log;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.Locale;
@SuppressWarnings("unused")
public class Debug
{
/*
* define constants for debug mode
*/
private static final int DBG_LEVEL_DBG = 5;
private static final int DBG_LEVEL_NORMAL = 4;
private static final int DBG_LEVEL_WARN = 3;
private static final int DBG_LEVEL_ERROR = 2;
private static final int DBG_LEVEL_FATAL = 1;
/*
* variables
*/
private static final int DEBUG_LEVEL = DBG_LEVEL_NORMAL;
private static final String TAG = "LifeLog";
private static final double MB = 1.0 * 1024 * 1024; // MB
/**
* Hide constructor
*/
private Debug(){
}
private static int getDebugLevel(){
return DEBUG_LEVEL;
}
/**
* Description : Trace fatal message
*
* @param fmt : text message
*/
public static void fatal(String fmt, Object... args)
{
if (getDebugLevel() >= DBG_LEVEL_FATAL)
{
final String stMsg = format("[FATAL]" + fmt, args);
final String stPoint = format(" at %s", getDebugPoint());
Log.e(TAG, "[FATAL]###########################################");
Log.e(TAG, stMsg);
Log.e(TAG, stPoint);
Log.e(TAG, "[FATAL]###########################################");
}
}
/**
* Description : Trace error message
*
* @param args : text message
*/
public static void error(String fmt, Object... args)
{
if (getDebugLevel() >= DBG_LEVEL_ERROR)
{
final String stMsg = format("[ERR]" + fmt, args);
final String stPoint = format(" at %s", getDebugPoint());
Log.e(TAG, stMsg);
Log.e(TAG, stPoint);
}
}
/**
* Description : Trace warning message
*
* @param args : text message
*/
public static void warn(String fmt, Object... args)
{
if (getDebugLevel() >= DBG_LEVEL_WARN)
{
final String stMsg = format("[WRN]" + fmt, args);
final String stPoint = format(" at %s", getDebugPoint());
Log.w(TAG, stMsg);
Log.w(TAG, stPoint);
}
}
/**
* Description : Trace warning message
*
* @param args : text message
*/
public static void dbg(String fmt, Object... args)
{
if (getDebugLevel() >= DBG_LEVEL_DBG)
{
Log.v(TAG, format("[ d ]" + fmt, args));
}
}
/**
* Description : Trace normal message
*
* @param args : text message
*/
public static void normal(String fmt, Object... args)
{
if (getDebugLevel() >= DBG_LEVEL_NORMAL)
{
Log.v(TAG, format("[ - ]" + fmt, args));
}
}
/**
* Description : verify
*
* @param b : Condition assert
*/
@SuppressWarnings("unused")
public static void verify(boolean b) {
if (!(b)) {
final String stMsg = format("###### ASSERT FAILED ######");
final String stPoint = format(" at %s", getDebugPoint());
Log.w(TAG, stMsg);
Log.w(TAG, stPoint);
}
}
/**
* Description : verify
*
* @param b : Condition assert
*/
@SuppressWarnings("unused")
public static void verify(boolean b, String msg) {
if (!(b)) {
final String stMsg = format("###### ASSERT FAILED ######");
final String stPoint = format(" at %s", getDebugPoint());
Log.w(TAG, stMsg);
if (msg != null) {
Log.w(TAG, " msg: " + msg);
}
Log.w(TAG, stPoint);
}
}
/**
* Print Stack trace when exception
* @param e Exception
*/
public static void printStackTrace( Exception e ){
StringWriter sw = new StringWriter();
e.printStackTrace( new PrintWriter(sw) );
String msg = sw.toString();
if (getDebugLevel() >= DBG_LEVEL_ERROR) {
Log.e(TAG, msg);
}
}
/**
* Is debug mode, true for debug mode
* @return DEBUG mode
*/
public static boolean isDebugEnable() {
return DEBUG_LEVEL >= DBG_LEVEL_DBG;
}
/**
* Use to trace current line/file at debug log point
*
* @return The point with the Java's logging default format, so, you can
* double click on the tracing line in the LogCat tool
*/
private static String getDebugPoint()
{
final String fullClassName = Thread.currentThread().getStackTrace()[4].getClassName();
final String className = fullClassName.substring(fullClassName.lastIndexOf(".") + 1);
final String methodName = Thread.currentThread().getStackTrace()[4].getMethodName();
final int lineNumber = Thread.currentThread().getStackTrace()[4].getLineNumber();
//
return format("%s.%s(%s.java:%d)", fullClassName, methodName, className, lineNumber);
}
/**
* Log current status of Heap
*
*/
@SuppressWarnings("unused")
public static void logHeap() {
final String tag = "HEAP";
final Double allocated = android.os.Debug.getNativeHeapAllocatedSize() / MB;
final Double available = android.os.Debug.getNativeHeapSize() / MB;
final Double free = android.os.Debug.getNativeHeapFreeSize() / MB;
final Double memUsed = Runtime.getRuntime().totalMemory() / MB;
final Double memMax = Runtime.getRuntime().maxMemory() / MB;
final Double memFree = Runtime.getRuntime().freeMemory() / MB;
Log.d(tag, format("Heap native: allocated %6.3f MB of %6.3f - free %6.3f MB", allocated, available, free));
Log.d(tag, format("Memory : allocated %6.3f MB of %6.3f - free %6.3f MB", memUsed, memMax, memFree));
}
private static String format(String fmt, Object... args)
{
if ((args != null) && (args.length > 0)) {
return String.format(Locale.US, fmt, args);
}
else {
return fmt;
}
}
/**
* Register a UnExceptionHandler to catch force-close error for thread
*/
public static void registerUnExceptionHandler() {
if(!(Thread.getDefaultUncaughtExceptionHandler() instanceof ExceptionHandler)) {
Thread.setDefaultUncaughtExceptionHandler( new ExceptionHandler() );
}
}
/**
* Trace current stack
*/
@SuppressWarnings("unused")
public static void stackTrace() {
Log.w(TAG, "########### STACK TRACE START #############");
for (final StackTraceElement ste : Thread.currentThread().getStackTrace()) {
System.out.println(ste);
}
Log.w(TAG, "########### STACK TRACE END #############");
}
public static class ExceptionHandler implements UncaughtExceptionHandler {
private final UncaughtExceptionHandler defaultUEH;
/*
* if any of the parameters is null, the respective functionality will
* not be used
*/
public ExceptionHandler() {
this.defaultUEH = Thread.getDefaultUncaughtExceptionHandler();
}
@Override
public void uncaughtException(Thread t, Throwable e) {
final Writer result = new StringWriter();
final PrintWriter printWriter = new PrintWriter(result);
e.printStackTrace(printWriter);
printWriter.close();
defaultUEH.uncaughtException(t, e);
}
}
}
/*******************************************************************************
* End of file
******************************************************************************/