package com.drmod;

import android.util.Log;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

public class DLog {
    private static final String FILE = "/data/user/0/com.lavalabs.dungeonrush/files/drmod.log";
    private static final SimpleDateFormat FMT =
        new SimpleDateFormat("MM-dd HH:mm:ss.SSS", Locale.US);
    private static final Object LOCK = new Object();

    public static void log(String tag, String msg) {
        Log.i(tag, msg);
        synchronized (LOCK) {
            PrintWriter pw = null;
            try {
                java.io.File f = new java.io.File(FILE);
                if (!f.getParentFile().exists()) {
                    f.getParentFile().mkdirs();
                }
                pw = new PrintWriter(new FileOutputStream(FILE, true), true);
                pw.println(FMT.format(new Date()) + " " + tag + ": " + msg);
            } catch (Throwable t) {
                Log.e(tag, "DLog file write failed: " + t.getMessage());
            } finally {
                if (pw != null) {
                    try { pw.close(); } catch (Throwable ignored) {}
                }
            }
        }
    }
}