Skip to content

Commit 0cd49df

Browse files
committed
see 09/21 log
1 parent be7de40 commit 0cd49df

File tree

4 files changed

+350
-73
lines changed

4 files changed

+350
-73
lines changed

utilcode/src/main/java/com/blankj/utilcode/utils/CameraUtils.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,8 @@
88
import android.net.Uri;
99
import android.os.Bundle;
1010
import android.provider.MediaStore;
11-
import android.util.Log;
1211

1312
import java.io.File;
14-
import java.io.FileOutputStream;
1513
import java.io.IOException;
1614

1715
/**
Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
package com.blankj.utilcode.utils;
2+
3+
import android.os.Environment;
4+
import android.util.Log;
5+
6+
import java.io.File;
7+
import java.io.FileWriter;
8+
import java.io.IOException;
9+
import java.io.PrintWriter;
10+
import java.io.StringWriter;
11+
import java.net.UnknownHostException;
12+
import java.text.SimpleDateFormat;
13+
import java.util.Date;
14+
import java.util.Locale;
15+
16+
/**
17+
* <pre>
18+
* author: Blankj
19+
* blog : http://blankj.com
20+
* time : 2016/9/21
21+
* desc :
22+
* </pre>
23+
*/
24+
public class LogUtils {
25+
26+
public static final String CACHE_DIR_NAME = "MyLog";
27+
public static final String TAG = "Frame";
28+
public static boolean isDebugModel = true;// 是否输出日志
29+
public static boolean isSaveDebugInfo = true;// 是否保存调试日志
30+
public static boolean isSaveCrashInfo = true;// 是否保存报错日志
31+
32+
public static void v(final String tag, final String msg) {
33+
if (isDebugModel) {
34+
Log.v(tag, "--> " + msg);
35+
}
36+
}
37+
38+
public static void d(final String tag, final String msg) {
39+
if (isDebugModel) {
40+
Log.d(tag, "--> " + msg);
41+
}
42+
if (isSaveDebugInfo) {
43+
new Thread() {
44+
public void run() {
45+
write(time() + tag + " --> " + msg + "\n");
46+
}
47+
}.start();
48+
}
49+
}
50+
51+
public static void i(final String tag, final String msg) {
52+
if (isDebugModel) {
53+
Log.i(tag, "--> " + msg);
54+
}
55+
}
56+
57+
public static void w(final String tag, final String msg) {
58+
if (isDebugModel) {
59+
Log.w(tag, "--> " + msg);
60+
}
61+
}
62+
63+
/**
64+
* 调试日志,便于开发跟踪。
65+
*
66+
* @param tag
67+
* @param msg
68+
*/
69+
public static void e(final String tag, final String msg) {
70+
if (isDebugModel) {
71+
Log.e(tag, " [CRASH] --> " + msg);
72+
}
73+
74+
if (isSaveCrashInfo) {
75+
new Thread() {
76+
public void run() {
77+
write(time() + tag + " [CRASH] --> " + msg + "\n");
78+
}
79+
}.start();
80+
}
81+
}
82+
83+
/**
84+
* try catch 时使用,上线产品可上传反馈。
85+
*
86+
* @param tag
87+
* @param tr
88+
*/
89+
public static void e(final String tag, final Throwable tr) {
90+
if (isSaveCrashInfo) {
91+
new Thread() {
92+
public void run() {
93+
write(time() + tag + " [CRASH] --> " + getStackTraceString(tr) + "\n");
94+
}
95+
}.start();
96+
}
97+
}
98+
99+
public static void v(String msg) {
100+
if (isDebugModel) {
101+
Log.v(TAG, "--> " + msg);
102+
}
103+
}
104+
105+
public static void d(final String msg) {
106+
if (isDebugModel) {
107+
Log.d(TAG, "--> " + msg);
108+
}
109+
if (isSaveDebugInfo) {
110+
new Thread() {
111+
public void run() {
112+
write(time() + TAG + " --> " + msg + "\n");
113+
}
114+
}.start();
115+
}
116+
}
117+
118+
public static void i(String msg) {
119+
if (isDebugModel) {
120+
Log.i(TAG, "--> " + msg);
121+
}
122+
}
123+
124+
public static void w(String msg) {
125+
if (isDebugModel) {
126+
Log.w(TAG, "--> " + msg);
127+
}
128+
}
129+
130+
/**
131+
* 调试日志,便于开发跟踪。
132+
*
133+
* @param msg
134+
*/
135+
public static void e(final String msg) {
136+
if (isDebugModel) {
137+
Log.e(TAG, " [CRASH] --> " + msg);
138+
}
139+
140+
if (isSaveCrashInfo) {
141+
new Thread() {
142+
public void run() {
143+
write(time() + TAG + "[CRASH] --> " + msg + "\n");
144+
}
145+
}.start();
146+
}
147+
}
148+
149+
/**
150+
* try catch 时使用,上线产品可上传反馈。
151+
*
152+
* @param tr
153+
*/
154+
public static void e(final Throwable tr) {
155+
if (isSaveCrashInfo) {
156+
new Thread() {
157+
public void run() {
158+
write(time() + TAG + " [CRASH] --> " + getStackTraceString(tr) + "\n");
159+
}
160+
}.start();
161+
}
162+
}
163+
164+
/**
165+
* 获取捕捉到的异常的字符串
166+
*
167+
* @param tr
168+
* @return
169+
*/
170+
public static String getStackTraceString(Throwable tr) {
171+
if (tr == null) {
172+
return "";
173+
}
174+
175+
Throwable t = tr;
176+
while (t != null) {
177+
if (t instanceof UnknownHostException) {
178+
return "";
179+
}
180+
t = t.getCause();
181+
}
182+
183+
StringWriter sw = new StringWriter();
184+
PrintWriter pw = new PrintWriter(sw);
185+
tr.printStackTrace(pw);
186+
return sw.toString();
187+
}
188+
189+
/**
190+
* 标识每条日志产生的时间
191+
*
192+
* @return
193+
*/
194+
private static String time() {
195+
return "[" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss", Locale.getDefault()).format(new Date(System.currentTimeMillis())) + "] ";
196+
}
197+
198+
/**
199+
* 以年月日作为日志文件名称
200+
*
201+
* @return
202+
*/
203+
private static String date() {
204+
return new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault()).format(new Date(System.currentTimeMillis()));
205+
}
206+
207+
/**
208+
* 保存到日志文件
209+
*
210+
* @param content
211+
*/
212+
public static synchronized void write(String content) {
213+
try {
214+
FileWriter writer = new FileWriter(getFile(), true);
215+
writer.write(content);
216+
writer.close();
217+
} catch (IOException e) {
218+
e.printStackTrace();
219+
}
220+
}
221+
222+
/**
223+
* 获取日志文件路径
224+
*
225+
* @return
226+
*/
227+
public static String getFile() {
228+
File sdDir = null;
229+
230+
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED))
231+
sdDir = Environment.getExternalStorageDirectory();
232+
233+
File cacheDir = new File(sdDir + File.separator + CACHE_DIR_NAME);
234+
if (!cacheDir.exists())
235+
cacheDir.mkdir();
236+
237+
File filePath = new File(cacheDir + File.separator + date() + ".log");
238+
239+
return filePath.toString();
240+
}
241+
}

0 commit comments

Comments
 (0)