TelinkLog.java
4.26 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
/*
* Copyright (C) 2015 The Telink Bluetooth Light Project
*
*/
package com.telink.bluetooth;
import android.os.Environment;
import android.util.Log;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
public class TelinkLog {
public final static String TAG = "TelinkBluetoothSDK";
public static boolean ENABLE = true;
public static boolean LOG2FILE_ENABLE = false;
public static String FILE_PREFIX = "TelinkBluetoothSDKLogger";
private static BufferedWriter mWriter;
public static boolean isLoggable(int level) {
if (ENABLE)
return Log.isLoggable(TAG, level);
return false;
}
public static String getStackTraceString(Throwable th) {
if (ENABLE)
return Log.getStackTraceString(th);
return th.getMessage();
}
public static int println(int level, String msg) {
if (ENABLE)
return Log.println(level, TAG, msg);
return 0;
}
public static int v(String msg) {
write2File(msg);
if (ENABLE)
return Log.v(TAG, msg);
return 0;
}
public static int v(String msg, Throwable th) {
write2File(msg);
write2File(getStackTraceString(th));
if (ENABLE)
return Log.v(TAG, msg, th);
return 0;
}
public static int d(String msg) {
write2File(msg);
if (ENABLE)
return Log.d(TAG, msg);
return 0;
}
public static int d(String msg, Throwable th) {
write2File(msg);
write2File(getStackTraceString(th));
if (ENABLE)
return Log.d(TAG, msg, th);
return 0;
}
public static int i(String msg) {
write2File(msg);
if (ENABLE)
return Log.i(TAG, msg);
return 0;
}
public static int i(String msg, Throwable th) {
write2File(msg);
write2File(getStackTraceString(th));
if (ENABLE)
return Log.i(TAG, msg, th);
return 0;
}
public static int w(String msg) {
write2File(msg);
if (ENABLE)
return Log.w(TAG, msg);
return 0;
}
public static int w(String msg, Throwable th) {
write2File(msg);
write2File(getStackTraceString(th));
if (ENABLE)
return Log.w(TAG, msg, th);
return 0;
}
public static int w(Throwable th) {
write2File(getStackTraceString(th));
if (ENABLE)
return Log.w(TAG, th);
return 0;
}
public static int e(String msg) {
write2File(msg);
if (ENABLE)
return Log.w(TAG, msg);
return 0;
}
public static int e(String msg, Throwable th) {
write2File(msg);
write2File(getStackTraceString(th));
if (ENABLE)
return Log.e(TAG, msg, th);
return 0;
}
private static void write2File(String log) {
if (LOG2FILE_ENABLE) {
try {
mWriter.write(log);
mWriter.newLine();
mWriter.flush();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public static void onCreate(String fileName) {
File root = Environment.getExternalStorageDirectory();
File dir = new File(root.getPath() + "/TelinkLog");
try {
if (!dir.exists())
dir.mkdir();
File file = new File(dir, fileName);
if (!file.exists())
file.createNewFile();
FileOutputStream fos = new FileOutputStream(file);
OutputStreamWriter osw = new OutputStreamWriter(fos);
mWriter = new BufferedWriter(osw, 1024);
mWriter.write(TAG + " begin : ");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void onDestroy() {
try {
if (mWriter != null) {
mWriter.write(TAG + " end : ");
mWriter.close();
}
} catch (Exception e) {
}
}
}