2013-12-20 19:25:49 +00:00
|
|
|
/*
|
|
|
|
* This is the source code of Telegram for Android v. 1.3.2.
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package org.telegram.messenger;
|
|
|
|
|
|
|
|
import android.net.Uri;
|
|
|
|
import android.util.Log;
|
|
|
|
|
|
|
|
import org.telegram.ui.ApplicationLoader;
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileOutputStream;
|
|
|
|
import java.io.OutputStreamWriter;
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Locale;
|
|
|
|
|
|
|
|
public class FileLog {
|
|
|
|
private OutputStreamWriter streamWriter = null;
|
|
|
|
private FastDateFormat dateFormat = null;
|
|
|
|
private DispatchQueue logQueue = null;
|
|
|
|
private File currentFile = null;
|
|
|
|
|
2014-03-22 22:31:55 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-12-20 19:25:49 +00:00
|
|
|
public FileLog() {
|
2013-12-26 11:43:37 +00:00
|
|
|
if (!ConnectionsManager.DEBUG_VERSION) {
|
2013-12-20 19:25:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
dateFormat = FastDateFormat.getInstance("dd_MM_yyyy_HH_mm_ss", Locale.US);
|
|
|
|
File sdCard = ApplicationLoader.applicationContext.getExternalFilesDir(null);
|
|
|
|
if (sdCard == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
File dir = new File(sdCard.getAbsolutePath() + "/logs");
|
|
|
|
if (dir == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
dir.mkdirs();
|
|
|
|
currentFile = new File(dir, dateFormat.format(System.currentTimeMillis()) + ".txt");
|
|
|
|
if (currentFile == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
currentFile.createNewFile();
|
|
|
|
FileOutputStream stream = new FileOutputStream(currentFile);
|
|
|
|
streamWriter = new OutputStreamWriter(stream);
|
|
|
|
streamWriter.write("-----start log " + dateFormat.format(System.currentTimeMillis()) + "-----\n");
|
|
|
|
streamWriter.flush();
|
|
|
|
logQueue = new DispatchQueue("logQueue");
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void e(final String tag, final String message, final Throwable exception) {
|
2013-12-26 11:43:37 +00:00
|
|
|
if (!ConnectionsManager.DEBUG_VERSION) {
|
2013-12-20 19:25:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log.e(tag, message, exception);
|
2014-03-22 22:31:55 +00:00
|
|
|
if (getInstance().streamWriter != null) {
|
|
|
|
getInstance().logQueue.postRunnable(new Runnable() {
|
2013-12-20 19:25:49 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2014-03-22 22:31:55 +00:00
|
|
|
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/" + tag + "﹕ " + message + "\n");
|
|
|
|
getInstance().streamWriter.write(exception.toString());
|
|
|
|
getInstance().streamWriter.flush();
|
2013-12-20 19:25:49 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void e(final String tag, final String message) {
|
2013-12-26 11:43:37 +00:00
|
|
|
if (!ConnectionsManager.DEBUG_VERSION) {
|
2013-12-20 19:25:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log.e(tag, message);
|
2014-03-22 22:31:55 +00:00
|
|
|
if (getInstance().streamWriter != null) {
|
|
|
|
getInstance().logQueue.postRunnable(new Runnable() {
|
2013-12-20 19:25:49 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2014-03-22 22:31:55 +00:00
|
|
|
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/" + tag + "﹕ " + message + "\n");
|
|
|
|
getInstance().streamWriter.flush();
|
2013-12-20 19:25:49 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void e(final String tag, final Exception e) {
|
2013-12-26 11:43:37 +00:00
|
|
|
if (!ConnectionsManager.DEBUG_VERSION) {
|
2013-12-20 19:25:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
e.printStackTrace();
|
2014-03-22 22:31:55 +00:00
|
|
|
if (getInstance().streamWriter != null) {
|
|
|
|
getInstance().logQueue.postRunnable(new Runnable() {
|
2013-12-20 19:25:49 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2014-03-22 22:31:55 +00:00
|
|
|
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/" + tag + "﹕ " + e + "\n");
|
2013-12-20 19:25:49 +00:00
|
|
|
StackTraceElement[] stack = e.getStackTrace();
|
|
|
|
for (StackTraceElement el : stack) {
|
2014-03-22 22:31:55 +00:00
|
|
|
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " E/" + tag + "﹕ " + el + "\n");
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
2014-03-22 22:31:55 +00:00
|
|
|
getInstance().streamWriter.flush();
|
2013-12-20 19:25:49 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2014-02-04 18:36:55 +00:00
|
|
|
} else {
|
|
|
|
e.printStackTrace();
|
2013-12-20 19:25:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void d(final String tag, final String message) {
|
2013-12-26 11:43:37 +00:00
|
|
|
if (!ConnectionsManager.DEBUG_VERSION) {
|
2013-12-20 19:25:49 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
Log.d(tag, message);
|
2014-03-22 22:31:55 +00:00
|
|
|
if (getInstance().streamWriter != null) {
|
|
|
|
getInstance().logQueue.postRunnable(new Runnable() {
|
2013-12-20 19:25:49 +00:00
|
|
|
@Override
|
|
|
|
public void run() {
|
|
|
|
try {
|
2014-03-22 22:31:55 +00:00
|
|
|
getInstance().streamWriter.write(getInstance().dateFormat.format(System.currentTimeMillis()) + " D/" + tag + "﹕ " + message + "\n");
|
|
|
|
getInstance().streamWriter.flush();
|
2013-12-20 19:25:49 +00:00
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void cleanupLogs() {
|
|
|
|
ArrayList<Uri> uris = new ArrayList<Uri>();
|
|
|
|
File sdCard = ApplicationLoader.applicationContext.getExternalFilesDir(null);
|
|
|
|
File dir = new File (sdCard.getAbsolutePath() + "/logs");
|
|
|
|
File[] files = dir.listFiles();
|
|
|
|
for (File file : files) {
|
2014-03-22 22:31:55 +00:00
|
|
|
if (getInstance().currentFile != null && file.getAbsolutePath().equals(getInstance().currentFile.getAbsolutePath())) {
|
2013-12-20 19:25:49 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
file.delete();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|