package com.github.liaoheng.common.util;

import android.os.Environment;
import android.support.annotation.StringDef;
import android.text.TextUtils;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import org.apache.commons.io.IOUtils;
import org.joda.time.DateTime;

/**
 * 不带system log
 * @author liaoheng
 * @version 2016-09-22 16:26
 */
public class LogFileUtils {
    private static final String TAG = LogFileUtils.class.getSimpleName();

    public final static String LEVEL_VERBOSE = " VERBOSE ";
    public final static String LEVEL_DEBUG   = " DEBUG ";
    public final static String LEVEL_INFO    = " INFO ";
    public final static String LEVEL_WARN    = " WARN ";
    public final static String LEVEL_ERROR   = " ERROR ";

    @StringDef({ LEVEL_VERBOSE, LEVEL_ERROR, LEVEL_WARN, LEVEL_DEBUG,
                 LEVEL_INFO })
    @Retention(RetentionPolicy.SOURCE)
    public @interface LevelFlags {}

    private static final String DEFAULT_FILE_NAME = "debug_log.txt";
    private              File   mLogFile          = new File(
            Environment.getExternalStorageDirectory(), DEFAULT_FILE_NAME);
    private static LogFileUtils instance;

    private LogFileUtils() {
    }

    public static synchronized LogFileUtils get() {
        if (instance == null) {
            synchronized (LogFileUtils.class) {
                if (instance == null) {
                    instance = new LogFileUtils();
                }
            }
        }
        return instance;
    }

    public void init() {
        init("");
    }

    public void init(File logFile) {
        if (logFile == null) {
            return;
        }
        mLogFile = logFile;
        L.Log.d(TAG, "init log file : %s ", mLogFile.getAbsoluteFile());
    }

    public void init(String fileName) {
        try {
            File log = FileUtils.createCacheSDAndroidDirectory("Log");
            if (TextUtils.isEmpty(fileName)) {
                fileName = DEFAULT_FILE_NAME;
            }
            mLogFile = FileUtils.createFile(log, fileName);
            L.Log.d(TAG, "init log file : %s ", mLogFile.getAbsoluteFile());
        } catch (SystemException ignored) {
        }
    }

    private OutputStream mFileOutputStream;

    public synchronized void open() {
        try {
            mFileOutputStream = FileUtils.openOutputStream(mLogFile,true);//不覆盖
        } catch (IOException ignored) {
        }
    }

    public synchronized void close() {
        IOUtils.closeQuietly(mFileOutputStream);
    }

    public void clearFile() {
        close();
        FileUtils.delete(mLogFile);
    }

    public synchronized void w(String tag, String logEntry, Object... o) {
        log(LEVEL_WARN, tag, getLog(logEntry, o));
    }

    public synchronized void w(String tag, Throwable throwable) {
        log(LEVEL_WARN, tag, throwable, throwable.getMessage());
    }

    public synchronized void w(String tag, Throwable throwable, String logEntry, Object... o) {
        log(LEVEL_WARN, tag, throwable, getLog(logEntry, o));
    }

    public synchronized void e(String tag, String logEntry, Object... o) {
        log(LEVEL_ERROR, tag, getLog(logEntry, o));
    }

    public synchronized void e(String tag, Throwable throwable) {
        log(LEVEL_ERROR, tag, throwable, throwable.getMessage());
    }

    public synchronized void e(String tag, Throwable throwable, String logEntry, Object... o) {
        log(LEVEL_ERROR, tag, throwable, getLog(logEntry, o));
    }

    public synchronized void d(String tag, String logEntry, Object... o) {
        log(LEVEL_DEBUG, tag, getLog(logEntry, o));
    }

    public synchronized void i(String tag, String logEntry, Object... o) {
        log(LEVEL_INFO, tag, getLog(logEntry, o));
    }

    public synchronized String getLog(String logEntry, Object... o) {
        return String.format(logEntry, o);
    }

    public synchronized void log(@LevelFlags String level, String tag, String logEntry) {
        writeLog(level, tag, null, logEntry);
    }

    public synchronized void log(@LevelFlags String level, String tag, Throwable throwable,
                                 String logEntry) {
        writeLog(level, tag, throwable, logEntry);
    }

    private synchronized void writeLog(String severityLevel, String tag, Throwable throwable,
                                       String logEntry) {
        String currentDateTime = DateTime.now().toString("yyyy-MM-dd HH:mm:ss.SSS");
        String stencil = currentDateTime + "   |" + severityLevel + "|   " + tag + " : " + logEntry;
        try {
            IOUtils.write(stencil, mFileOutputStream);
            if (throwable != null) {
                IOUtils.write("\n", mFileOutputStream);
                throwable.printStackTrace(new PrintStream(mFileOutputStream));
                mFileOutputStream.flush();
            }
            IOUtils.write("\n", mFileOutputStream);
        } catch (IOException ignored) {
        }
    }

}

@@ -138,24 +138,95 @@
 .TextUtils;%0A
+%0Aimport org.apache.commons.io.IOUtils;%0Aimport org.joda.time.DateTime;%0A%0A
 import java.
@@ -406,77 +406,8 @@
 icy;
-%0Aimport org.apache.commons.io.IOUtils;%0Aimport org.joda.time.DateTime;
 %0A%0A/*
@@ -424,16 +424,19 @@
 tem log%0A
+ *%0A
  * @auth
@@ -686,18 +686,16 @@
 L_DEBUG 
-  
 = %22 DEBU
@@ -741,19 +741,16 @@
 EL_INFO 
-   
 = %22 INFO
@@ -795,19 +795,16 @@
 EL_WARN 
-   
 = %22 WARN
@@ -850,18 +850,16 @@
 L_ERROR 
-  
 = %22 ERRO
@@ -934,21 +934,16 @@
 _DEBUG,%0A
-     
         
@@ -960,16 +960,16 @@
 INFO %7D)%0A
+
     @Ret
@@ -1121,28 +1121,13 @@
 ate 
-             
 File 
-  
 mLog
@@ -1131,25 +1131,16 @@
 LogFile 
-         
 = new Fi
@@ -2445,16 +2445,17 @@
 LogFile,
+ 
 true);//
@@ -2599,24 +2599,58 @@
 putStream);%0A
+        mFileOutputStream = null;%0A
     %7D%0A%0A    p
@@ -4231,37 +4231,16 @@
 owable,%0A
-                     
         
@@ -4254,32 +4254,32 @@
 ing logEntry) %7B%0A
+
         writeLog
@@ -4418,35 +4418,8 @@
 le,%0A
-                           
     
@@ -4437,32 +4437,103 @@
 ing logEntry) %7B%0A
+        if (mFileOutputStream == null) %7B%0A            return;%0A        %7D%0A
         String c

fix logfile