Skip to content

Commit df83245

Browse files
committed
see 01/03 log
1 parent 0dd56ce commit df83245

File tree

3 files changed

+200
-25
lines changed

3 files changed

+200
-25
lines changed

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

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,18 @@
3333
import java.io.StringWriter;
3434
import java.lang.annotation.Retention;
3535
import java.lang.annotation.RetentionPolicy;
36+
import java.lang.reflect.Array;
3637
import java.lang.reflect.ParameterizedType;
3738
import java.lang.reflect.Type;
3839
import java.net.UnknownHostException;
3940
import java.text.ParseException;
4041
import java.text.SimpleDateFormat;
42+
import java.util.Collection;
4143
import java.util.Date;
4244
import java.util.Formatter;
4345
import java.util.Iterator;
4446
import java.util.Locale;
47+
import java.util.Map;
4548
import java.util.Set;
4649
import java.util.concurrent.ExecutorService;
4750
import java.util.concurrent.Executors;
@@ -181,19 +184,19 @@ public static void file(@TYPE final int type, final String tag, final Object con
181184
log(FILE | type, tag, content);
182185
}
183186

184-
public static void json(final String content) {
187+
public static void json(final Object content) {
185188
log(JSON | D, CONFIG.mGlobalTag, content);
186189
}
187190

188-
public static void json(@TYPE final int type, final String content) {
191+
public static void json(@TYPE final int type, final Object content) {
189192
log(JSON | type, CONFIG.mGlobalTag, content);
190193
}
191194

192-
public static void json(final String tag, final String content) {
195+
public static void json(final String tag, final Object content) {
193196
log(JSON | D, tag, content);
194197
}
195198

196-
public static void json(@TYPE final int type, final String tag, final String content) {
199+
public static void json(@TYPE final int type, final String tag, final Object content) {
197200
log(JSON | type, tag, content);
198201
}
199202

@@ -330,7 +333,7 @@ private static String processBody(final int type, final Object... contents) {
330333

331334
private static String formatObject(int type, Object object) {
332335
if (object == null) return NULL;
333-
if (type == JSON) return LogFormatter.formatJson(object.toString());
336+
if (type == JSON) return LogFormatter.object2Json(object);
334337
if (type == XML) return LogFormatter.formatXml(object.toString());
335338
return formatObject(object);
336339
}
@@ -344,9 +347,6 @@ private static String formatObject(Object object) {
344347
return iFormatter.format(object);
345348
}
346349
}
347-
if (object instanceof Throwable) return LogFormatter.throwable2String((Throwable) object);
348-
if (object instanceof Bundle) return LogFormatter.bundle2String((Bundle) object);
349-
if (object instanceof Intent) return LogFormatter.intent2String((Intent) object);
350350
return LogFormatter.object2String(object);
351351
}
352352

@@ -782,17 +782,19 @@ private static class TagHead {
782782
}
783783

784784
private static class LogFormatter {
785-
static String formatJson(String json) {
786-
try {
787-
if (json.startsWith("{")) {
788-
json = new JSONObject(json).toString(4);
789-
} else if (json.startsWith("[")) {
790-
json = new JSONArray(json).toString(4);
791-
}
792-
} catch (JSONException e) {
793-
e.printStackTrace();
794-
}
795-
return json;
785+
786+
static String object2String(Object object) {
787+
if (object.getClass().isArray()) return object2Json(object);
788+
if (object instanceof Collection) return object2Json(object);
789+
if (object instanceof Map) return object2Json(object);
790+
if (object instanceof Throwable) return throwable2String((Throwable) object);
791+
if (object instanceof Bundle) return bundle2String((Bundle) object);
792+
if (object instanceof Intent) return intent2String((Intent) object);
793+
return object.toString();
794+
}
795+
796+
static String object2Json(Object object) {
797+
return formatJson(GSON.toJson(object));
796798
}
797799

798800
static String formatXml(String xml) {
@@ -810,7 +812,7 @@ static String formatXml(String xml) {
810812
return xml;
811813
}
812814

813-
static String throwable2String(final Throwable e) {
815+
private static String throwable2String(final Throwable e) {
814816
Throwable t = e;
815817
while (t != null) {
816818
if (t instanceof UnknownHostException) {
@@ -830,7 +832,7 @@ static String throwable2String(final Throwable e) {
830832
return sw.toString();
831833
}
832834

833-
static String bundle2String(Bundle bundle) {
835+
private static String bundle2String(Bundle bundle) {
834836
Iterator<String> iterator = bundle.keySet().iterator();
835837
if (!iterator.hasNext()) {
836838
return "Bundle {}";
@@ -851,7 +853,7 @@ static String bundle2String(Bundle bundle) {
851853
}
852854
}
853855

854-
static String intent2String(Intent intent) {
856+
private static String intent2String(Intent intent) {
855857
StringBuilder sb = new StringBuilder(128);
856858
sb.append("Intent { ");
857859
boolean first = true;
@@ -961,8 +963,17 @@ static String intent2String(Intent intent) {
961963
return sb.toString();
962964
}
963965

964-
static String object2String(Object object) {
965-
return formatJson(GSON.toJson(object));
966+
private static String formatJson(String json) {
967+
try {
968+
if (json.startsWith("{")) {
969+
json = new JSONObject(json).toString(4);
970+
} else if (json.startsWith("[")) {
971+
json = new JSONArray(json).toString(4);
972+
}
973+
} catch (JSONException e) {
974+
e.printStackTrace();
975+
}
976+
return json;
966977
}
967978

968979
@RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)

utilcode/lib/src/test/java/com/blankj/utilcode/util/BaseTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
package com.blankj.utilcode.util;
22

3+
4+
import android.util.Log;
5+
36
import org.junit.Test;
47
import org.junit.runner.RunWith;
58
import org.robolectric.RobolectricTestRunner;
69
import org.robolectric.RuntimeEnvironment;
710
import org.robolectric.annotation.Config;
11+
import org.robolectric.shadows.ShadowLog;
812

913
/**
1014
* <pre>
@@ -15,16 +19,19 @@
1519
* </pre>
1620
*/
1721
@RunWith(RobolectricTestRunner.class)
18-
@Config(manifest = Config.NONE)
22+
@Config(manifest = Config.NONE, shadows = {ShadowLog.class})
1923
public class BaseTest {
2024

2125
public BaseTest() {
26+
ShadowLog.stream = System.out;
2227
Utils.init(RuntimeEnvironment.application);
2328
}
2429

2530
@Test
2631
public void test() throws Exception {
2732

33+
Log.e("haha", "test: ");
34+
2835
// final CountDownLatch countDownLatch = new CountDownLatch(1);
2936
// final Scanner scanner = new Scanner(System.in);
3037
// ExecutorService singlePool = ThreadUtils.getSinglePool();
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
package com.blankj.utilcode.util;
2+
3+
import org.junit.Test;
4+
5+
/**
6+
* <pre>
7+
* author: Blankj
8+
* blog : http://blankj.com
9+
* time : 2016/09/26
10+
* desc : test LogUtils
11+
* </pre>
12+
*/
13+
public class LogUtilsTest extends BaseTest {
14+
15+
private static final String JSON = "{\"tools\": [{ \"name\":\"css format\" , \"site\":\"http://tools.w3cschool.cn/code/css\" },{ \"name\":\"JSON format\" , \"site\":\"http://tools.w3cschool.cn/code/JSON\" },{ \"name\":\"pwd check\" , \"site\":\"http://tools.w3cschool.cn/password/my_password_safe\" }]}";
16+
17+
@Test
18+
public void testV() {
19+
LogUtils.v("");
20+
LogUtils.v(null);
21+
LogUtils.v("hello");
22+
LogUtils.v("hello\nworld");
23+
LogUtils.v("hello", "world");
24+
}
25+
26+
@Test
27+
public void testVTag() {
28+
LogUtils.vTag("", "");
29+
LogUtils.vTag("TAG", null);
30+
LogUtils.vTag("TAG", "hello");
31+
LogUtils.vTag("TAG", "hello\nworld");
32+
LogUtils.vTag("TAG", "hello", "world");
33+
}
34+
35+
@Test
36+
public void testD() {
37+
LogUtils.d("");
38+
LogUtils.d(null);
39+
LogUtils.d("hello");
40+
LogUtils.d("hello\nworld");
41+
LogUtils.d("hello", "world");
42+
}
43+
44+
@Test
45+
public void testDTag() {
46+
LogUtils.dTag("", "");
47+
LogUtils.dTag("TAG", null);
48+
LogUtils.dTag("TAG", "hello");
49+
LogUtils.dTag("TAG", "hello\nworld");
50+
LogUtils.dTag("TAG", "hello", "world");
51+
}
52+
53+
@Test
54+
public void testI() {
55+
LogUtils.i("");
56+
LogUtils.i(null);
57+
LogUtils.i("hello");
58+
LogUtils.i("hello\nworld");
59+
LogUtils.i("hello", "world");
60+
}
61+
62+
@Test
63+
public void testITag() {
64+
LogUtils.iTag("", "");
65+
LogUtils.iTag("TAG", null);
66+
LogUtils.iTag("TAG", "hello");
67+
LogUtils.iTag("TAG", "hello\nworld");
68+
LogUtils.iTag("TAG", "hello", "world");
69+
}
70+
71+
@Test
72+
public void testW() {
73+
LogUtils.w("");
74+
LogUtils.w(null);
75+
LogUtils.w("hello");
76+
LogUtils.w("hello\nworld");
77+
LogUtils.w("hello", "world");
78+
}
79+
80+
@Test
81+
public void testWTag() {
82+
LogUtils.wTag("", "");
83+
LogUtils.wTag("TAG", null);
84+
LogUtils.wTag("TAG", "hello");
85+
LogUtils.wTag("TAG", "hello\nworld");
86+
LogUtils.wTag("TAG", "hello", "world");
87+
}
88+
89+
@Test
90+
public void testE() {
91+
LogUtils.e("");
92+
LogUtils.e(null);
93+
LogUtils.e("hello");
94+
LogUtils.e("hello\nworld");
95+
LogUtils.e("hello", "world");
96+
}
97+
98+
@Test
99+
public void testETag() {
100+
LogUtils.eTag("", "");
101+
LogUtils.eTag("TAG", null);
102+
LogUtils.eTag("TAG", "hello");
103+
LogUtils.eTag("TAG", "hello\nworld");
104+
LogUtils.eTag("TAG", "hello", "world");
105+
}
106+
107+
@Test
108+
public void testA() {
109+
LogUtils.a("");
110+
LogUtils.a(null);
111+
LogUtils.a("hello");
112+
LogUtils.a("hello\nworld");
113+
LogUtils.a("hello", "world");
114+
}
115+
116+
@Test
117+
public void testATag() {
118+
LogUtils.aTag("", "");
119+
LogUtils.aTag("TAG", null);
120+
LogUtils.aTag("TAG", "hello");
121+
LogUtils.aTag("TAG", "hello\nworld");
122+
LogUtils.aTag("TAG", "hello", "world");
123+
}
124+
125+
@Test
126+
public void testJson() {
127+
// LogUtils.json(JSON);
128+
LogUtils.json(new Person("B\nlankj"));
129+
}
130+
131+
@Test
132+
public void testXml() {
133+
}
134+
135+
static class Person {
136+
137+
String name;
138+
int gender;
139+
String address;
140+
141+
public Person(String name) {
142+
this.name = name;
143+
}
144+
145+
@Override
146+
public boolean equals(Object obj) {
147+
if (obj == this) return true;
148+
if (!(obj instanceof Person)) return false;
149+
Person p = (Person) obj;
150+
return equals(name, p.name) && p.gender == gender && equals(address, p.address);
151+
}
152+
153+
private static boolean equals(final Object o1, final Object o2) {
154+
return o1 == o2 || (o1 != null && o1.equals(o2));
155+
}
156+
}
157+
}

0 commit comments

Comments
 (0)