22
33import android .os .Environment ;
44import android .support .annotation .IntDef ;
5- import android .support .annotation .NonNull ;
65import android .util .Log ;
76
87import org .json .JSONArray ;
@@ -51,15 +50,15 @@ public final class LogUtils {
5150
5251 @ IntDef ({V , D , I , W , E , A })
5352 @ Retention (RetentionPolicy .SOURCE )
54- public @interface TYPE {
55-
53+ private @interface TYPE {
5654 }
5755
5856 private static final int FILE = 0xF1 ;
5957 private static final int JSON = 0xF2 ;
6058 private static final int XML = 0xF4 ;
61- private static String dir ;// log存储目录
6259 private static ExecutorService executor ;
60+ private static String defaultDir ;// log默认存储目录
61+ private static String dir ; // log存储目录
6362
6463 private static boolean sLogSwitch = true ; // log总开关,默认开
6564 private static String sGlobalTag = null ; // log标签
@@ -75,49 +74,39 @@ public final class LogUtils {
7574 private static final String LEFT_BORDER = "║ " ;
7675 private static final String BOTTOM_BORDER = "╚═══════════════════════════════════════════════════════════════════════════════════════════════════" ;
7776 private static final int MAX_LEN = 4000 ;
77+ private static final Format FORMAT = new SimpleDateFormat ("MM-dd HH:mm:ss.SSS " , Locale .getDefault ());
7878
7979 private static final String NULL_TIPS = "Log with null object." ;
8080 private static final String NULL = "null" ;
8181 private static final String ARGS = "args" ;
82- private static final Format FORMAT = new SimpleDateFormat ("MM-dd HH:mm:ss.SSS " , Locale .getDefault ());
8382
8483 private LogUtils () {
8584 throw new UnsupportedOperationException ("u can't instantiate me..." );
8685 }
8786
8887 public static class Builder {
89-
9088 public Builder () {
89+ if (defaultDir != null ) return ;
9190 if (Environment .MEDIA_MOUNTED .equals (Environment .getExternalStorageState ())
9291 && Utils .getContext ().getExternalCacheDir () != null )
93- dir = Utils .getContext ().getExternalCacheDir () + FILE_SEP + "log" + FILE_SEP ;
92+ defaultDir = Utils .getContext ().getExternalCacheDir () + FILE_SEP + "log" + FILE_SEP ;
9493 else {
95- dir = Utils .getContext ().getCacheDir () + FILE_SEP + "log" + FILE_SEP ;
94+ defaultDir = Utils .getContext ().getCacheDir () + FILE_SEP + "log" + FILE_SEP ;
9695 }
9796 }
9897
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-
10998 public Builder setLogSwitch (boolean logSwitch ) {
11099 LogUtils .sLogSwitch = logSwitch ;
111100 return this ;
112101 }
113102
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 )) {
119105 LogUtils .sGlobalTag = "" ;
120106 sTagIsSpace = true ;
107+ } else {
108+ LogUtils .sGlobalTag = tag ;
109+ sTagIsSpace = false ;
121110 }
122111 return this ;
123112 }
@@ -132,6 +121,20 @@ public Builder setLog2FileSwitch(boolean log2FileSwitch) {
132121 return this ;
133122 }
134123
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+
135138 public Builder setBorderSwitch (boolean borderSwitch ) {
136139 LogUtils .sLogBorderSwitch = borderSwitch ;
137140 return this ;
@@ -141,6 +144,17 @@ public Builder setLogFilter(@TYPE int logFilter) {
141144 LogUtils .sLogFilter = logFilter ;
142145 return this ;
143146 }
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+ }
144158 }
145159
146160 public static void v (Object contents ) {
@@ -229,9 +243,9 @@ private static void log(int type, String tag, Object... contents) {
229243 case A :
230244 if (type >= sLogFilter ) {
231245 printLog (type , tag , msg );
232- }
233- if ( sLog2FileSwitch ) {
234- print2File ( tag , msg );
246+ if ( sLog2FileSwitch ) {
247+ print2File ( tag , msg );
248+ }
235249 }
236250 break ;
237251 case FILE :
@@ -247,28 +261,32 @@ private static void log(int type, String tag, Object... contents) {
247261 }
248262
249263 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 ) {
260266 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+ }
263289 }
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- : "" ;
272290 String body = NULL_TIPS ;
273291 if (contents != null ) {
274292 if (contents .length == 1 ) {
@@ -344,11 +362,11 @@ private static void printLog(int type, String tag, String msg) {
344362 int index = MAX_LEN ;
345363 for (int i = 1 ; i < countOfSub ; i ++) {
346364 sub = msg .substring (index , index + MAX_LEN );
347- print (type , tag , ( sLogBorderSwitch ? LEFT_BORDER : "" ) + sub );
365+ print (type , tag , sLogBorderSwitch ? LEFT_BORDER + sub : sub );
348366 index += MAX_LEN ;
349367 }
350368 sub = msg .substring (index , len );
351- print (type , tag , ( sLogBorderSwitch ? LEFT_BORDER : "" ) + sub );
369+ print (type , tag , sLogBorderSwitch ? LEFT_BORDER + sub : sub );
352370 } else {
353371 print (type , tag , msg );
354372 }
@@ -383,7 +401,7 @@ private static void print2File(final String tag, final String msg) {
383401 String format = FORMAT .format (now );
384402 String date = format .substring (0 , 5 );
385403 String time = format .substring (6 );
386- final String fullPath = dir + date + ".txt" ;
404+ final String fullPath = ( dir == null ? defaultDir : dir ) + date + ".txt" ;
387405 if (!createOrExistsFile (fullPath )) {
388406 Log .e (tag , "log to " + fullPath + " failed!" );
389407 return ;
0 commit comments