Repository navigation
Expand file tree
/
Copy pathLogger.java
More file actions
166 lines (153 loc) · 4.52 KB
/
Copy pathLogger.java
File metadata and controls
166 lines (153 loc) · 4.52 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
package ru.meefik.linuxdeploy;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import android.content.Context;
public class Logger {
private static volatile List<String> protocol = new ArrayList<>();
private static boolean fragment = false;
private static String logFile;
/**
* Generate timestamp
*
* @return timestamp
*/
private static String getTimeStamp() {
return "["
+ new SimpleDateFormat("HH:mm:ss", Locale.ENGLISH)
.format(new Date()) + "] ";
}
/**
* Append the message to protocol and show
*
* @param c context
* @param msg message
*/
private static synchronized void appendMessage(final Context c,
final String msg) {
final int msgLength = msg.length();
if (msgLength > 0) {
final boolean timestamp = PrefStore.isTimestamp(c);
String[] tokens = msg.split("\\n");
int lastIndex = protocol.size() - 1;
for (int i = 0, l = tokens.length; i < l; i++) {
// update last record from List if fragment
if (i == 0 && fragment && lastIndex >= 0) {
String last = protocol.get(lastIndex);
protocol.set(lastIndex, last + tokens[i]);
continue;
}
// add the message to List
if (timestamp)
protocol.add(getTimeStamp() + tokens[i]);
else protocol.add(tokens[i]);
// remove first line if overflow
if (protocol.size() > PrefStore.getMaxLines(c)) {
protocol.remove(0);
}
}
// set fragment
fragment = (msg.charAt(msgLength - 1) != '\n');
// show log
MainActivity.showLog(get());
// save the message to file
if (PrefStore.isLogger(c)) {
saveToFile(c, msg);
}
}
}
/**
* Append message to file
*
* @param c context
* @param msg message
*/
private static void saveToFile(Context c, String msg) {
byte[] data = msg.getBytes();
FileOutputStream fos = null;
if (logFile == null) logFile = PrefStore.getLogFile(c);
try {
fos = new FileOutputStream(logFile, true);
fos.write(data);
fos.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* Set log filename
*
* @param c context
*/
public static void setLogFile(Context c) {
long timestamp = System.currentTimeMillis() / 1000;
logFile = PrefStore.getLogFile(c).replace("${TIMESTAMP}", String.valueOf(timestamp));
}
/**
* Clear protocol
*/
public static void clear() {
protocol.clear();
fragment = false;
}
/**
* Get protocol
*
* @return protocol as text
*/
public static String get() {
return android.text.TextUtils.join("\n", protocol);
}
/**
* Append message to protocol
*
* @param c context
* @param msg message
*/
public static void log(Context c, String msg) {
appendMessage(c, msg);
}
/**
* Append stream messages to protocol
*
* @param c context
* @param stream stream
*/
public static void log(Context c, InputStream stream) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(stream));
int n;
char[] buffer = new char[1024];
while ((n = reader.read(buffer)) != -1) {
String msg = String.valueOf(buffer, 0, n);
appendMessage(c, msg);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}