-
Notifications
You must be signed in to change notification settings - Fork 8.3k
/
Copy pathFileLog.java
609 lines (551 loc) · 23.7 KB
/
FileLog.java
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
/*
* This is the source code of Telegram for Android v. 5.x.x.
* It is licensed under GNU GPL v. 2 or later.
* You should have received a copy of the license in this archive (see LICENSE).
*
* Copyright Nikolai Kudashov, 2013-2018.
*/
package org.telegram.messenger;
import android.content.Context;
import android.content.res.ColorStateList;
import android.os.Debug;
import android.os.Handler;
import android.os.Looper;
import android.util.Log;
import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import org.telegram.messenger.time.FastDateFormat;
import org.telegram.messenger.video.MediaCodecVideoConvertor;
import org.telegram.tgnet.TLObject;
import org.telegram.tgnet.TLRPC;
import org.telegram.ui.Components.AnimatedFileDrawable;
import org.telegram.ui.LaunchActivity;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.lang.reflect.Field;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
public class FileLog {
private OutputStreamWriter streamWriter = null;
private FastDateFormat dateFormat = null;
private FastDateFormat fileDateFormat = null;
private DispatchQueue logQueue = null;
private File currentFile = null;
private File networkFile = null;
private File tonlibFile = null;
private boolean initied;
public static boolean databaseIsMalformed = false;
public static final boolean LOG_ANRS = BuildVars.DEBUG_VERSION;
private OutputStreamWriter tlStreamWriter = null;
private File tlRequestsFile = null;
private final static String tag = "tmessages";
private final static String mtproto_tag = "MTProto";
private static volatile FileLog Instance = null;
public static FileLog getInstance() {
FileLog localInstance = Instance;
if (localInstance == null) {
synchronized (FileLog.class) {
localInstance = Instance;
if (localInstance == null) {
Instance = localInstance = new FileLog();
}
}
}
return localInstance;
}
public FileLog() {
if (!BuildVars.LOGS_ENABLED) {
return;
}
init();
}
private static Gson gson;
private static ExclusionStrategy exclusionStrategy;
private static HashSet<String> excludeRequests;
public static void dumpResponseAndRequest(int account, TLObject request, TLObject response, TLRPC.TL_error error, long requestMsgId, long startRequestTimeInMillis, int requestToken) {
if (/*!BuildVars.DEBUG_PRIVATE_VERSION || !BuildVars.LOGS_ENABLED || */request == null) {
return;
}
String requestSimpleName = request.getClass().getSimpleName();
checkGson();
if (excludeRequests.contains(requestSimpleName) && error == null) {
return;
}
try {
String req = "req -> " + requestSimpleName + " : " + gson.toJson(request);
String res = "null";
if (response != null) {
res = "res -> " + response.getClass().getSimpleName() + " : " + gson.toJson(response);
} else if (error != null) {
res = "err -> " + error.getClass().getSimpleName() + " : " + gson.toJson(error);
}
String finalRes = res;
long time = System.currentTimeMillis();
FileLog.getInstance().logQueue.postRunnable(() -> {
try {
String metadata = "requestMsgId=" + requestMsgId + " requestingTime=" + (System.currentTimeMillis() - startRequestTimeInMillis) + " request_token=" + requestToken + " account=" + account;
FileLog.getInstance().tlStreamWriter.write(getInstance().dateFormat.format(time) + " " + metadata);
FileLog.getInstance().tlStreamWriter.write("\n");
FileLog.getInstance().tlStreamWriter.write(req);
FileLog.getInstance().tlStreamWriter.write("\n");
FileLog.getInstance().tlStreamWriter.write(finalRes);
FileLog.getInstance().tlStreamWriter.write("\n\n");
FileLog.getInstance().tlStreamWriter.flush();
Log.d(mtproto_tag, metadata);
Log.d(mtproto_tag, req);
Log.d(mtproto_tag, finalRes);
Log.d(mtproto_tag, " ");
} catch (Exception e) {
e.printStackTrace();
}
});
} catch (Throwable e) {
FileLog.e(e, BuildVars.DEBUG_PRIVATE_VERSION);
}
}
public static void dumpUnparsedMessage(TLObject message, long messageId, int account) {
if (/*!BuildVars.DEBUG_PRIVATE_VERSION || !BuildVars.LOGS_ENABLED || */message == null) {
return;
}
try {
checkGson();
getInstance().dateFormat.format(System.currentTimeMillis());
String messageStr = "receive message -> " + message.getClass().getSimpleName() + " : " + (gsonDisabled ? message : gson.toJson(message));
String res = "null";
long time = System.currentTimeMillis();
FileLog.getInstance().logQueue.postRunnable(() -> {
try {
String metadata = getInstance().dateFormat.format(time) + " msgId=" + messageId + " account=" + account;
FileLog.getInstance().tlStreamWriter.write(metadata);
FileLog.getInstance().tlStreamWriter.write("\n");
FileLog.getInstance().tlStreamWriter.write(messageStr);
FileLog.getInstance().tlStreamWriter.write("\n\n");
FileLog.getInstance().tlStreamWriter.flush();
Log.d(mtproto_tag, "msgId=" + messageId + " account=" + account);
Log.d(mtproto_tag, messageStr);
Log.d(mtproto_tag, " ");
} catch (Exception e) {
e.printStackTrace();
}
});
} catch (Throwable e) {
}
}
private static boolean gsonDisabled;
public static void disableGson(boolean disable) {
gsonDisabled = disable;
}
private static HashSet<String> privateFields;
private static void checkGson() {
if (gson == null) {
privateFields = new HashSet<>();
// privateFields.add("message");
privateFields.add("phone");
privateFields.add("about");
privateFields.add("status_text");
privateFields.add("bytes");
privateFields.add("secret");
privateFields.add("stripped_thumb");
privateFields.add("strippedBitmap");
privateFields.add("networkType");
privateFields.add("disableFree");
privateFields.add("mContext");
privateFields.add("priority");
privateFields.add("constructor");
//exclude file loading
excludeRequests = new HashSet<>();
excludeRequests.add("TL_upload_getFile");
excludeRequests.add("TL_upload_getWebFile");
exclusionStrategy = new ExclusionStrategy() {
@Override
public boolean shouldSkipField(FieldAttributes f) {
if (privateFields.contains(f.getName()) || "message".equalsIgnoreCase(f.getName()) && String.class.equals(f.getDeclaredType())) {
return true;
}
return false;
}
@Override
public boolean shouldSkipClass(Class<?> clazz) {
return clazz.isInstance(DispatchQueue.class) || clazz.isInstance(AnimatedFileDrawable.class) || clazz.isInstance(ColorStateList.class) || clazz.isInstance(Context.class);
}
};
gson = new GsonBuilder()
.addSerializationExclusionStrategy(exclusionStrategy)
.registerTypeAdapterFactory(RuntimeClassNameTypeAdapterFactory.of(TLObject.class, "type_", exclusionStrategy))
.registerTypeHierarchyAdapter(TLObject.class, new TLObjectDeserializer())
.create();
}
}
private static class TLObjectDeserializer implements JsonSerializer<TLObject> {
@Override
public JsonElement serialize(TLObject src, Type typeOfSrc, JsonSerializationContext context) {
JsonObject jsonObj = new JsonObject();
String className = src.getClass().getName();
final String usualPrefix = "org.telegram.tgnet.";
if (className.startsWith(usualPrefix)) {
className = className.substring(usualPrefix.length());
}
jsonObj.addProperty("_", className);
try {
Field[] fields = src.getClass().getFields();
for (Field field : fields) {
if (privateFields != null && privateFields.contains(field.getName())) continue;
field.setAccessible(true);
try {
Object value = field.get(src);
if (value != null) {
Class clazz = value.getClass();
if (clazz.isInstance(DispatchQueue.class) || clazz.isInstance(AnimatedFileDrawable.class) || clazz.isInstance(ColorStateList.class) || clazz.isInstance(Context.class)) {
continue;
}
}
JsonElement jsonElement = context.serialize(value);
jsonObj.add(field.getName(), jsonElement);
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
} catch (Exception e) {
e.printStackTrace();
}
return jsonObj;
}
}
public void init() {
if (initied) {
return;
}
dateFormat = FastDateFormat.getInstance("dd_MM_yyyy_HH_mm_ss.SSS", Locale.US);
fileDateFormat = FastDateFormat.getInstance("dd_MM_yyyy_HH_mm_ss", Locale.US);
String date = fileDateFormat.format(System.currentTimeMillis());
try {
File dir = AndroidUtilities.getLogsDir();
if (dir == null) {
return;
}
currentFile = new File(dir, date + ".txt");
tlRequestsFile = new File(dir, date + "_mtproto.txt");
} catch (Exception e) {
e.printStackTrace();
}
try {
logQueue = new DispatchQueue("logQueue");
currentFile.createNewFile();
FileOutputStream stream = new FileOutputStream(currentFile);
streamWriter = new OutputStreamWriter(stream);
streamWriter.write("-----start log " + date + "-----\n");
streamWriter.flush();
FileOutputStream tlStream = new FileOutputStream(tlRequestsFile);
tlStreamWriter = new OutputStreamWriter(tlStream);
tlStreamWriter.write("-----start log " + date + "-----\n");
tlStreamWriter.flush();
} catch (Exception e) {
e.printStackTrace();
}
if (LOG_ANRS) {
new ANRDetector(this::dumpANR);
}
initied = true;
}
public static void ensureInitied() {
getInstance().init();
}
public static String getNetworkLogPath() {
if (!BuildVars.LOGS_ENABLED) {
return "";
}
try {
File dir = AndroidUtilities.getLogsDir();
if (dir == null) {
return "";
}
getInstance().networkFile = new File(dir, getInstance().fileDateFormat.format(System.currentTimeMillis()) + "_net.txt");
return getInstance().networkFile.getAbsolutePath();
} catch (Throwable e) {
e.printStackTrace();
}
return "";
}
public static String getTonlibLogPath() {
if (!BuildVars.LOGS_ENABLED) {
return "";
}
try {
File dir = AndroidUtilities.getLogsDir();
if (dir == null) {
return "";
}
getInstance().tonlibFile = new File(dir, getInstance().dateFormat.format(System.currentTimeMillis()) + "_tonlib.txt");
return getInstance().tonlibFile.getAbsolutePath();
} catch (Throwable e) {
e.printStackTrace();
}
return "";
}
public static void e(final String message, final Throwable exception) {
if (!BuildVars.LOGS_ENABLED) {
return;
}
ensureInitied();
Log.e(tag, message, exception);
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: " + message + "\n");
getInstance().streamWriter.write(exception.toString());
StackTraceElement[] stack = exception.getStackTrace();
for (int a = 0; a < stack.length; a++) {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: \tat " + stack[a] + "\n");
}
getInstance().streamWriter.flush();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
public static void e(final String message) {
if (!BuildVars.LOGS_ENABLED) {
return;
}
ensureInitied();
Log.e(tag, message);
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: " + message + "\n");
getInstance().streamWriter.flush();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
public static void e(final Throwable e) {
e(e, true);
}
public static void e(final Throwable e, boolean logToAppCenter) {
if (!BuildVars.LOGS_ENABLED) {
return;
}
if (BuildVars.DEBUG_VERSION && needSent(e) && logToAppCenter) {
AndroidUtilities.appCenterLog(e);
}
if (BuildVars.DEBUG_VERSION && e.getMessage() != null && e.getMessage().contains("disk image is malformed") && !databaseIsMalformed) {
FileLog.d("copy malformed files");
databaseIsMalformed = true;
File filesDir = ApplicationLoader.getFilesDirFixed();
filesDir = new File(filesDir, "malformed_database/");
filesDir.mkdirs();
ArrayList<File> malformedFiles = MessagesStorage.getInstance(UserConfig.selectedAccount).getDatabaseFiles();
for (int i = 0; i < malformedFiles.size(); i++) {
try {
AndroidUtilities.copyFile(malformedFiles.get(i), new File(filesDir, malformedFiles.get(i).getName()));
} catch (IOException ex) {
FileLog.e(ex);
}
}
}
ensureInitied();
e.printStackTrace();
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: " + e + "\n");
StackTraceElement[] stack = e.getStackTrace();
for (int a = 0; a < stack.length; a++) {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: \tat " + stack[a] + "\n");
}
Throwable cause = e.getCause();
if (cause != null) {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: Caused by " + cause + "\n");
stack = cause.getStackTrace();
for (int a = 0; a < stack.length; a++) {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: \tat " + stack[a] + "\n");
}
}
getInstance().streamWriter.flush();
} catch (Exception e1) {
e1.printStackTrace();
}
});
} else {
e.printStackTrace();
}
}
public static void fatal(final Throwable e) {
fatal(e, true);
}
private static long dumpedHeap;
private void dumpMemory() {
if (System.currentTimeMillis() - dumpedHeap < 30_000) return;
dumpedHeap = System.currentTimeMillis();
try {
Debug.dumpHprofData(new File(AndroidUtilities.getLogsDir(), getInstance().dateFormat.format(System.currentTimeMillis()) + "_heap.hprof").getAbsolutePath());
} catch (Exception e2) {
FileLog.e(e2);
}
}
private void dumpANR() {
StringBuilder sb = new StringBuilder();
Map<Thread, StackTraceElement[]> allThreads = Thread.getAllStackTraces();
for (Map.Entry<Thread, StackTraceElement[]> entry : allThreads.entrySet()) {
Thread thread = entry.getKey();
StackTraceElement[] stackTrace = entry.getValue();
sb.append("Thread: ").append(thread.getName()).append("\n");
for (StackTraceElement element : stackTrace) {
sb.append("\tat ").append(element).append("\n");
}
sb.append("\n\n");
}
FileLog.e("ANR thread dump\n" + sb.toString());
dumpMemory();
}
public static void fatal(final Throwable e, boolean logToAppCenter) {
if (!BuildVars.LOGS_ENABLED) {
return;
}
if (e instanceof OutOfMemoryError) {
getInstance().dumpMemory();
}
if (logToAppCenter && BuildVars.DEBUG_VERSION && needSent(e)) {
AndroidUtilities.appCenterLog(e);
}
ensureInitied();
e.printStackTrace();
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " FATAL/tmessages: " + e + "\n");
StackTraceElement[] stack = e.getStackTrace();
for (int a = 0; a < stack.length; a++) {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " FATAL/tmessages: \tat " + stack[a] + "\n");
}
Throwable cause = e.getCause();
if (cause != null) {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: Caused by " + cause + "\n");
stack = cause.getStackTrace();
for (int a = 0; a < stack.length; a++) {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/tmessages: \tat " + stack[a] + "\n");
}
}
getInstance().streamWriter.flush();
} catch (Exception e1) {
e1.printStackTrace();
}
if (BuildVars.DEBUG_PRIVATE_VERSION) {
System.exit(2);
}
});
} else {
e.printStackTrace();
if (BuildVars.DEBUG_PRIVATE_VERSION) {
System.exit(2);
}
}
}
private static boolean needSent(Throwable e) {
if (e instanceof InterruptedException || e instanceof MediaCodecVideoConvertor.ConversionCanceledException || e instanceof IgnoreSentException) {
return false;
}
return true;
}
public static void d(final String message) {
if (!BuildVars.LOGS_ENABLED) {
return;
}
ensureInitied();
Log.d(tag, message);
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " D/tmessages: " + message + "\n");
getInstance().streamWriter.flush();
} catch (Exception e) {
e.printStackTrace();
if (AndroidUtilities.isENOSPC(e)) {
LaunchActivity.checkFreeDiscSpaceStatic(1);
}
}
});
}
}
public static void w(final String message) {
if (!BuildVars.LOGS_ENABLED) {
return;
}
ensureInitied();
Log.w(tag, message);
if (getInstance().streamWriter != null) {
getInstance().logQueue.postRunnable(() -> {
try {
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " W/tmessages: " + message + "\n");
getInstance().streamWriter.flush();
} catch (Exception e) {
e.printStackTrace();
}
});
}
}
public static void cleanupLogs() {
ensureInitied();
File dir = AndroidUtilities.getLogsDir();
if (dir == null) {
return;
}
File[] files = dir.listFiles();
if (files != null) {
for (int a = 0; a < files.length; a++) {
File file = files[a];
if (getInstance().currentFile != null && file.getAbsolutePath().equals(getInstance().currentFile.getAbsolutePath())) {
continue;
}
if (getInstance().networkFile != null && file.getAbsolutePath().equals(getInstance().networkFile.getAbsolutePath())) {
continue;
}
if (getInstance().tonlibFile != null && file.getAbsolutePath().equals(getInstance().tonlibFile.getAbsolutePath())) {
continue;
}
file.delete();
}
}
}
public static class IgnoreSentException extends Exception{
public IgnoreSentException(String e) {
super(e);
}
}
public class ANRDetector {
private final long TIMEOUT_MS = 5000; // ANR threshold (5 seconds)
private final Handler mainHandler = new Handler(Looper.getMainLooper());
private boolean isUIThreadResponsive = true;
public ANRDetector(Runnable anrDetected) {
new Thread(() -> {
while (true) {
isUIThreadResponsive = false;
// Post a task to the main thread
mainHandler.post(() -> isUIThreadResponsive = true);
try {
Thread.sleep(TIMEOUT_MS);
} catch (InterruptedException e) {
e.printStackTrace();
}
if (!isUIThreadResponsive) {
anrDetected.run();
}
}
}).start();
}
}
}