DLog.java 36 lines ยท 1.2 KB
Raw
1package com.drmod;
2
3import android.util.Log;
4import java.io.FileOutputStream;
5import java.io.PrintWriter;
6import java.text.SimpleDateFormat;
7import java.util.Date;
8import java.util.Locale;
9
10public class DLog {
11 private static final String FILE = "/data/user/0/com.lavalabs.dungeonrush/files/drmod.log";
12 private static final SimpleDateFormat FMT =
13 new SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.US);
14 private static final Object LOCK = new Object();
15
16 public static void log(String tag, String msg) {
17 Log.i(tag, msg);
18 synchronized (LOCK) {
19 PrintWriter pw = null;
20 try {
21 java.io.File f = new java.io.File(FILE);
22 if (!f.getParentFile().exists()) {
23 f.getParentFile().mkdirs();
24 }
25 pw = new PrintWriter(new FileOutputStream(FILE, true), true);
26 pw.println(FMT.format(new Date()) + " " + tag + ": " + msg);
27 } catch (Throwable t) {
28 Log.e(tag, "DLog file write failed: " + t.getMessage());
29 } finally {
30 if (pw != null) {
31 try { pw.close(); } catch (Throwable ignored) {}
32 }
33 }
34 }
35 }
36}