LLog.java
9.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
package com.glsx.commonres.util;
import android.os.Environment;
import android.util.Log;
import com.blankj.utilcode.util.LogUtils;
import com.glsx.commonres.CommonResApp;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.LinkedList;
import java.util.Queue;
/**
* Created by: lixingzhou
* Created Date: 2017/5/31 11:00
* Description: 日志工具
*/
@SuppressWarnings("deprecation")
public class LLog implements Runnable {
private static final String TAG = "AiCar";
private static final int ALL = 0;
private static final int VERBOSE = 1;
private static final int DEBUG = 2;
private static final int INFO = 3;
private static final int WARN = 4;
private static final int ERROR = 5;
//日志输出等级:大于此等级的日志才会输出
private static final int LOG_LEVEL = ALL;
//日志写文件等级:大于此等级的日志才会写入文件
private static final int WRITE_LOG_LEVEL = VERBOSE;
private static boolean isPrintLog = CommonResApp.sIsPrintLog;//默认不打印Log
private static boolean isWriteToFile = false;//默认不写日志文件
private String LOG_PATH_SDCARD_DIR = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
+ "Android" + File.separator + CommonResApp.getPackageName() + File.separator;// 日志文件在sdcard中的路径
private static final String LOG_FILENAME = ".txt";// 输出的日志文件名称
private static LogUtils instance = null;
private boolean isInitialized = false;//初始化标识
private boolean isExited = false;//禁用标识
private BufferedWriter output = null;
private Queue<String> queue = null;
private Thread thread = null;
private LLog() {
queue = new LinkedList<String>();
}
//单例实现及优化
private static class Holder{
private static final LLog INS = new LLog();
}
public static LLog getInstance(){
return Holder.INS;
}
// public synchronized static LogUtils getInstance() {
// if (instance == null) {
// instance = new LogUtils();
// }
// return instance;
// }
/**
* 是否输出日志
*
* @param printLog
*/
public static void setIsPrintLog(boolean printLog) {
isPrintLog = printLog;
}
/**
* 是否把日志写到文件中
*
* @param writeToFile
*/
public static void setIsWriteToFile(boolean writeToFile) {
isWriteToFile = writeToFile;
}
public static boolean isShow() {
return isPrintLog;
}
/**
*
*
*
*
* 初始化日志工具写文件(写日志必须调用 否则无法写日志到文件)
*
* @return 状态标识
*/
@SuppressWarnings("ResultOfMethodCallIgnored")
public synchronized boolean init() {
if (isInitialized) {
return true;
}
try {
String needWriteFileName = DateUtils.getTimestampStrOfDay();
/*File f = new File(LOG_PATH_SDCARD_DIR, needWriteFileName + LOG_FILENAME);
if (!f.exists()) {
f.getParentFile().mkdirs();
f.createNewFile();
}*/
//Android10版本特性(targetSdkVersion 29)需修改
File appDocFile = CommonResApp.getContext().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS);
if (appDocFile == null || !appDocFile.exists()) {
e("系统文件夹不存在,请检查设备状态");
return false;
}
File f = new File(appDocFile.getAbsolutePath() + "/Log/", needWriteFileName + LOG_FILENAME);
if (!f.exists()) {
f.getParentFile().mkdirs();
f.createNewFile();
}
output = new BufferedWriter(new FileWriter(f, true));
isExited = false;
startThread();
isInitialized = true;
} catch (Exception e) {
e.printStackTrace();
}
return isInitialized;
}
/**
* 禁用日志输出
*/
public synchronized void dispose() {
if (!isInitialized) {
return;
}
isExited = true;
isInitialized = false;
synchronized (thread) {
if (thread.isAlive()) {
try {
thread.notifyAll();
} catch (Exception e) {
e.printStackTrace();
}
}
}
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
if (output != null) {
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
output = null;
}
thread = null;
}
/**
* 添加日志到写入队列中
*
* @param log 日志内容
*/
private void add(String log) {
if (!isInitialized) {
return;
}
synchronized (queue) {
queue.add(log);
}
synchronized (thread) {
thread.notifyAll();
}
}
/**
* 获取队列中的一条日志
*
* @return poll() 方法在用空集合调用时不是抛出异常,只是返回 null
*/
private String get() {
String result = null;
synchronized (queue) {
if (!queue.isEmpty()) {
result = queue.poll();
}
}
return result;
}
private boolean isEmpty() {
boolean result = true;
synchronized (queue) {
result = queue.isEmpty();
}
return result;
}
@Override
public void run() {
while (!isExited) {
if (isEmpty()) {
synchronized (thread) {
try {
thread.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
while (!isEmpty()) {
String log = get();
try {
output.write(log);
output.newLine();
output.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void startThread() {
thread = new Thread(this);
thread.start();
}
/**
* 添加日志
*
* @param level 日志等级
* @param mylogtype 日志等级类型
* @param tag 日志tag
* @param text 日志信息
*/
private static void addLog(int level, final String mylogtype, final String tag, final String text) {
if (!isWriteToFile || (WRITE_LOG_LEVEL >= level)) {
return;
}
StringBuilder needWriteMessage = new StringBuilder().append(DateUtils.getTimestampStr())
.append(" ").append(mylogtype).append(" ").append(tag).append(" ").append(text);
getInstance().add(needWriteMessage.toString());
needWriteMessage = null;
}
public static void v(String msg) {
if (VERBOSE > LOG_LEVEL) {
if (isPrintLog) {
Log.v(TAG, msg);
}
addLog(VERBOSE, "v", TAG, msg);
}
}
public static void v(String tag, String msg) {
if (VERBOSE > LOG_LEVEL) {
if (isPrintLog) {
Log.v(tag, msg);
}
addLog(VERBOSE, "v", tag, msg);
}
}
public static void d(String msg) {
if (DEBUG > LOG_LEVEL) {
if (isPrintLog) {
Log.d(TAG, msg);
}
addLog(DEBUG, "d", TAG, msg);
}
}
public static void d(String tag, String msg) {
if (DEBUG > LOG_LEVEL) {
if (isPrintLog) {
Log.d(tag, msg);
}
addLog(DEBUG, "d", tag, msg);
}
}
public static void i(String msg) {
if (INFO > LOG_LEVEL) {
if (isPrintLog) {
Log.i(TAG, msg);
}
addLog(INFO, "i", TAG, msg);
}
}
public static void i(String tag, String msg) {
if (INFO > LOG_LEVEL) {
if (isPrintLog) {
Log.i(tag, msg);
}
addLog(INFO, "i", tag, msg);
}
}
public static void w(String msg) {
if (WARN > LOG_LEVEL) {
if (isPrintLog) {
Log.w(TAG, msg);
}
addLog(WARN, "w", TAG, msg);
}
}
public static void w(String tag, String msg) {
if (WARN > LOG_LEVEL) {
if (isPrintLog) {
Log.w(tag, msg);
}
addLog(WARN, "w", tag, msg);
}
}
public static void e(String msg) {
if (ERROR > LOG_LEVEL) {
if (isPrintLog) {
// Log.e(TAG, msg);
}
addLog(ERROR, "e", TAG, msg);
}
}
public static void e(String tag, String msg) {
if (ERROR > LOG_LEVEL) {
if (isPrintLog) {
Log.e(tag, msg);
}
addLog(ERROR, "e", tag, msg);
}
}
public static void e(String tag, String msg, Throwable r) {
if (ERROR > LOG_LEVEL) {
if (isPrintLog) {
Log.e(tag, msg, r);
}
addLog(ERROR, "e", tag, msg);
}
}
}