Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
//License from project: Apache License 

import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;

public class Main {
    private final static boolean LOG_TO_FILE = false;
    private final static String LOG_FILE_PATH = Environment.getExternalStorageDirectory().getPath()
            + "/mengdd_debug_log.txt";
    private static File LOG_FILE;

    public static int w(String tag, String msg) {
        return println(Log.WARN, tag, msg);
    }

    public static int w(String tag, String msg, Throwable tr) {
        return println(Log.WARN, tag, msg + '\n' + getStackTraceString(tr));
    }

    public static int w(String tag, Throwable tr) {
        return println(Log.WARN, tag, getStackTraceString(tr));
    }

    public static int println(int priority, String tag, String msg) {
        if (TextUtils.isEmpty(msg)) {
            msg = "";
        }
        if (LOG_TO_FILE) {
            logToFile(tag, msg);
            return 0;
        } else {
            return Log.println(priority, tag, msg);
        }
    }

    public static String getStackTraceString(Throwable tr) {
        return Log.getStackTraceString(tr);
    }

    private static synchronized void logToFile(String tag, String msg) {
        try {
            if (LOG_FILE == null) {
                LOG_FILE = new File(LOG_FILE_PATH);
                if (LOG_FILE.exists()) {
                    LOG_FILE.delete();
                }
                LOG_FILE.createNewFile();
            }

            FileOutputStream outputStream = new FileOutputStream(LOG_FILE, true);

            StringBuilder builder = new StringBuilder();
            builder.append(tag).append("  :  ").append(msg).append("\n");

            outputStream.write(builder.toString().getBytes());
            outputStream.flush();
            outputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}