2
2
3
3
import android .os .Environment ;
4
4
import android .support .annotation .IntDef ;
5
- import android .support .annotation .NonNull ;
6
5
import android .util .Log ;
7
6
8
7
import org .json .JSONArray ;
@@ -51,15 +50,15 @@ public final class LogUtils {
51
50
52
51
@ IntDef ({V , D , I , W , E , A })
53
52
@ Retention (RetentionPolicy .SOURCE )
54
- public @interface TYPE {
55
-
53
+ private @interface TYPE {
56
54
}
57
55
58
56
private static final int FILE = 0xF1 ;
59
57
private static final int JSON = 0xF2 ;
60
58
private static final int XML = 0xF4 ;
61
- private static String dir ;// log存储目录
62
59
private static ExecutorService executor ;
60
+ private static String defaultDir ;// log默认存储目录
61
+ private static String dir ; // log存储目录
63
62
64
63
private static boolean sLogSwitch = true ; // log总开关,默认开
65
64
private static String sGlobalTag = null ; // log标签
@@ -75,49 +74,39 @@ public final class LogUtils {
75
74
private static final String LEFT_BORDER = "║ " ;
76
75
private static final String BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
77
76
private static final int MAX_LEN = 4000 ;
77
+ private static final Format FORMAT = new SimpleDateFormat ("MM-dd HH:mm:ss.SSS " , Locale .getDefault ());
78
78
79
79
private static final String NULL_TIPS = "Log with null object." ;
80
80
private static final String NULL = "null" ;
81
81
private static final String ARGS = "args" ;
82
- private static final Format FORMAT = new SimpleDateFormat ("MM-dd HH:mm:ss.SSS " , Locale .getDefault ());
83
82
84
83
private LogUtils () {
85
84
throw new UnsupportedOperationException ("u can't instantiate me..." );
86
85
}
87
86
88
87
public static class Builder {
89
-
90
88
public Builder () {
89
+ if (defaultDir != null ) return ;
91
90
if (Environment .MEDIA_MOUNTED .equals (Environment .getExternalStorageState ())
92
91
&& Utils .getContext ().getExternalCacheDir () != null )
93
- dir = Utils .getContext ().getExternalCacheDir () + FILE_SEP + "log" + FILE_SEP ;
92
+ defaultDir = Utils .getContext ().getExternalCacheDir () + FILE_SEP + "log" + FILE_SEP ;
94
93
else {
95
- dir = Utils .getContext ().getCacheDir () + FILE_SEP + "log" + FILE_SEP ;
94
+ defaultDir = Utils .getContext ().getCacheDir () + FILE_SEP + "log" + FILE_SEP ;
96
95
}
97
96
}
98
97
99
- public Builder (@ NonNull String dir ) {
100
- LogUtils .dir = dir ;
101
- if (!dir .endsWith (FILE_SEP ))
102
- LogUtils .dir += FILE_SEP ;
103
- }
104
-
105
- public Builder (@ NonNull File dir ) {
106
- LogUtils .dir = dir .getAbsolutePath () + FILE_SEP ;
107
- }
108
-
109
98
public Builder setLogSwitch (boolean logSwitch ) {
110
99
LogUtils .sLogSwitch = logSwitch ;
111
100
return this ;
112
101
}
113
102
114
- public Builder setGlobalTag (String tag ) {
115
- if (!isSpace (tag )) {
116
- LogUtils .sGlobalTag = tag ;
117
- sTagIsSpace = false ;
118
- } else {
103
+ public Builder setGlobalTag (final String tag ) {
104
+ if (isSpace (tag )) {
119
105
LogUtils .sGlobalTag = "" ;
120
106
sTagIsSpace = true ;
107
+ } else {
108
+ LogUtils .sGlobalTag = tag ;
109
+ sTagIsSpace = false ;
121
110
}
122
111
return this ;
123
112
}
@@ -132,6 +121,20 @@ public Builder setLog2FileSwitch(boolean log2FileSwitch) {
132
121
return this ;
133
122
}
134
123
124
+ public Builder setDir (final String dir ) {
125
+ if (isSpace (dir )) {
126
+ LogUtils .dir = null ;
127
+ } else {
128
+ LogUtils .dir = dir .endsWith (FILE_SEP ) ? dir : dir + FILE_SEP ;
129
+ }
130
+ return this ;
131
+ }
132
+
133
+ public Builder setDir (final File dir ) {
134
+ LogUtils .dir = dir == null ? null : dir .getAbsolutePath () + FILE_SEP ;
135
+ return this ;
136
+ }
137
+
135
138
public Builder setBorderSwitch (boolean borderSwitch ) {
136
139
LogUtils .sLogBorderSwitch = borderSwitch ;
137
140
return this ;
@@ -141,6 +144,17 @@ public Builder setLogFilter(@TYPE int logFilter) {
141
144
LogUtils .sLogFilter = logFilter ;
142
145
return this ;
143
146
}
147
+
148
+ @ Override
149
+ public String toString () {
150
+ return "switch: " + sLogSwitch
151
+ + LINE_SEP + "tag: " + (sGlobalTag .equals ("" ) ? "null" : sGlobalTag )
152
+ + LINE_SEP + "head: " + sLogHeadSwitch
153
+ + LINE_SEP + "file: " + sLog2FileSwitch
154
+ + LINE_SEP + "dir: " + (dir == null ? defaultDir : dir )
155
+ + LINE_SEP + "border: " + sLogBorderSwitch
156
+ + LINE_SEP + "filter: " + (sLogFilter == V ? "verbose" : "not verbose" );
157
+ }
144
158
}
145
159
146
160
public static void v (Object contents ) {
@@ -229,9 +243,9 @@ private static void log(int type, String tag, Object... contents) {
229
243
case A :
230
244
if (type >= sLogFilter ) {
231
245
printLog (type , tag , msg );
232
- }
233
- if ( sLog2FileSwitch ) {
234
- print2File ( tag , msg );
246
+ if ( sLog2FileSwitch ) {
247
+ print2File ( tag , msg );
248
+ }
235
249
}
236
250
break ;
237
251
case FILE :
@@ -247,28 +261,32 @@ private static void log(int type, String tag, Object... contents) {
247
261
}
248
262
249
263
private static String [] processContents (int type , String tag , Object ... contents ) {
250
- StackTraceElement targetElement = Thread .currentThread ().getStackTrace ()[5 ];
251
- String className = targetElement .getClassName ();
252
- String [] classNameInfo = className .split ("\\ ." );
253
- if (classNameInfo .length > 0 ) {
254
- className = classNameInfo [classNameInfo .length - 1 ];
255
- }
256
- if (className .contains ("$" )) {
257
- className = className .split ("\\ $" )[0 ];
258
- }
259
- if (!sTagIsSpace ) {// 如果全局tag不为空,那就用全局tag
264
+ String head = "" ;
265
+ if (!sTagIsSpace && !sLogHeadSwitch ) {
260
266
tag = sGlobalTag ;
261
- } else {// 全局tag为空时,如果传入的tag为空那就显示类名,否则显示tag
262
- tag = isSpace (tag ) ? className : tag ;
267
+ }else {
268
+ StackTraceElement targetElement = Thread .currentThread ().getStackTrace ()[5 ];
269
+ String className = targetElement .getClassName ();
270
+ String [] classNameInfo = className .split ("\\ ." );
271
+ if (classNameInfo .length > 0 ) {
272
+ className = classNameInfo [classNameInfo .length - 1 ];
273
+ }
274
+ if (className .contains ("$" )) {
275
+ className = className .split ("\\ $" )[0 ];
276
+ }
277
+ if (sTagIsSpace ) {
278
+ tag = isSpace (tag ) ? className : tag ;
279
+ }
280
+ if (sLogHeadSwitch ) {
281
+ head = new Formatter ()
282
+ .format ("Thread: %s, %s(%s.java:%d)" + LINE_SEP ,
283
+ Thread .currentThread ().getName (),
284
+ targetElement .getMethodName (),
285
+ className ,
286
+ targetElement .getLineNumber ())
287
+ .toString ();
288
+ }
263
289
}
264
- String head = sLogHeadSwitch
265
- ? new Formatter ()
266
- .format ("Thread: %s, %s(%s.java:%d)" + LINE_SEP ,
267
- Thread .currentThread ().getName (),
268
- targetElement .getMethodName (),
269
- className ,
270
- targetElement .getLineNumber ()).toString ()
271
- : "" ;
272
290
String body = NULL_TIPS ;
273
291
if (contents != null ) {
274
292
if (contents .length == 1 ) {
@@ -344,11 +362,11 @@ private static void printLog(int type, String tag, String msg) {
344
362
int index = MAX_LEN ;
345
363
for (int i = 1 ; i < countOfSub ; i ++) {
346
364
sub = msg .substring (index , index + MAX_LEN );
347
- print (type , tag , ( sLogBorderSwitch ? LEFT_BORDER : "" ) + sub );
365
+ print (type , tag , sLogBorderSwitch ? LEFT_BORDER + sub : sub );
348
366
index += MAX_LEN ;
349
367
}
350
368
sub = msg .substring (index , len );
351
- print (type , tag , ( sLogBorderSwitch ? LEFT_BORDER : "" ) + sub );
369
+ print (type , tag , sLogBorderSwitch ? LEFT_BORDER + sub : sub );
352
370
} else {
353
371
print (type , tag , msg );
354
372
}
@@ -383,7 +401,7 @@ private static void print2File(final String tag, final String msg) {
383
401
String format = FORMAT .format (now );
384
402
String date = format .substring (0 , 5 );
385
403
String time = format .substring (6 );
386
- final String fullPath = dir + date + ".txt" ;
404
+ final String fullPath = ( dir == null ? defaultDir : dir ) + date + ".txt" ;
387
405
if (!createOrExistsFile (fullPath )) {
388
406
Log .e (tag , "log to " + fullPath + " failed!" );
389
407
return ;
0 commit comments