Skip to content

Commit d9f65aa

Browse files
committed
see 05/09 log
1 parent 5cfa240 commit d9f65aa

File tree

12 files changed

+348
-158
lines changed

12 files changed

+348
-158
lines changed

README-CN.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ Builder.setLogSwitch : 设置log总开关
317317
Builder.setGlobalTag : 设置log全局tag
318318
Builder.setLogHeadSwitch : 设置log头开关
319319
Builder.setLog2FileSwitch: 设置log文件开关
320+
Builder.setDir : 设置log文件存储目录
320321
Builder.setBorderSwitch : 设置log边框开关
321322
Builder.setLogFilter : 设置log过滤器
322323
v : Verbose日志
@@ -571,9 +572,11 @@ getDate : 获取与给定时间等于时间差的Date
571572
getMillisByNow : 获取与当前时间等于时间差的时间戳
572573
getStringByNow : 获取与当前时间等于时间差的时间字符串
573574
getDateByNow : 获取与当前时间等于时间差的Date
574-
isSameDay : 判断是否同一天
575+
isToday : 判断是否今天
575576
isLeapYear : 判断是否闰年
576-
getWeek, getWeekIndex : 获取星期
577+
getChineseWeek : 获取中式星期
578+
getUSWeek : 获取美式式星期
579+
getWeekIndex : 获取星期索引
577580
getWeekOfMonth : 获取月份中的第几周
578581
getWeekOfYear : 获取年份中的第几周
579582
getChineseZodiac : 获取生肖

README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ Builder.setLogSwitch
317317
Builder.setGlobalTag
318318
Builder.setLogHeadSwitch
319319
Builder.setLog2FileSwitch
320+
Builder.setDir
320321
Builder.setBorderSwitch
321322
Builder.setLogFilter
322323
v
@@ -571,9 +572,11 @@ getDate
571572
getMillisByNow
572573
getStringByNow
573574
getDateByNow
574-
isSameDay
575+
isToday
575576
isLeapYear
576-
getWeek, getWeekIndex
577+
getChineseWeek
578+
getUSWeek
579+
getWeekIndex
577580
getWeekOfMonth
578581
getWeekOfYear
579582
getChineseZodiac

app/src/main/res/anim/fragment_slide_left_enter.xml renamed to app/src/main/res/animator/fragment_slide_left_enter.xml

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!--suppress ALL -->
32
<set xmlns:android="http://schemas.android.com/apk/res/android">
43
<objectAnimator
54
android:interpolator="@android:interpolator/decelerate_quint"

app/src/main/res/anim/fragment_slide_left_exit.xml renamed to app/src/main/res/animator/fragment_slide_left_exit.xml

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!--suppress ALL -->
32
<set xmlns:android="http://schemas.android.com/apk/res/android">
43
<objectAnimator
54
android:interpolator="@android:interpolator/decelerate_quint"

app/src/main/res/anim/fragment_slide_right_enter.xml renamed to app/src/main/res/animator/fragment_slide_right_enter.xml

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!--suppress ALL -->
32
<set xmlns:android="http://schemas.android.com/apk/res/android">
43
<objectAnimator
54
android:interpolator="@android:interpolator/decelerate_quint"

app/src/main/res/anim/fragment_slide_right_exit.xml renamed to app/src/main/res/animator/fragment_slide_right_exit.xml

-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?xml version="1.0" encoding="utf-8"?>
2-
<!--suppress ALL -->
32
<set xmlns:android="http://schemas.android.com/apk/res/android">
43
<objectAnimator
54
android:interpolator="@android:interpolator/decelerate_quint"

update_log.md

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
* 17/05/09 TimeUtils完善单元测试
12
* 17/05/08 更新BarUtils,LogUtils新增配置文件,TimeUtils将pattern改为format,发布1.5.0
23
* 17/05/04 新增签名
34
* 17/05/03 对齐头部日期

utilcode/build.gradle

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,5 @@ dependencies {
6565
testCompile "com.google.truth:truth:$TRUTH_VERSION"
6666
testCompile "org.robolectric:robolectric:$ROBOLECTRIC_VERSION"
6767
}
68-
apply from: "https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle"
68+
//apply from: "https://raw.githubusercontent.com/xiaopansky/android-library-publish-to-jcenter/master/bintrayUpload.gradle"
6969
//gradlew bintrayUpload

utilcode/src/main/java/com/blankj/utilcode/util/LogUtils.java

+67-49
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import android.os.Environment;
44
import android.support.annotation.IntDef;
5-
import android.support.annotation.NonNull;
65
import android.util.Log;
76

87
import 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

Comments
 (0)