/*
 * Copyright 2014 Jacob Klinker
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.klinker.android.logger;

import android.os.Environment;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class Log {

    private static final String TAG = "Log";

    private static boolean DEBUG = false;
    private static String PATH = "ApplicationLog.txt";
    private static OnLogListener logListener;

    public static void setDebug(boolean debug) {
        DEBUG = debug;
    }

    public static void setPath(String path) {
        if (path.endsWith("/")) {
            PATH = path + "ApplicationLog.txt";
        } else if (!path.endsWith(".txt")) {
            PATH  = path + ".txt";
        } else {
            PATH = path;
        }
    }

    public static void setLogListener(OnLogListener listener) {
        logListener = listener;
    }

    public static void e(String tag, String message) {
        if (DEBUG) {
            int logResult = android.util.Log.e(tag, message);
            if (logResult > 0)
                logToFile(tag, message);
        }
    }

    public static void e(String tag, String message, Throwable error) {
        if (DEBUG) {
            int logResult = android.util.Log.e(tag, message, error);
            if (logResult > 0)
                logToFile(tag, message + "\r\n" + android.util.Log.getStackTraceString(error));
        }
    }

    public static void v(String tag, String message) {
        if (DEBUG) {
            int logResult = android.util.Log.v(tag, message);
            if (logResult > 0)
                logToFile(tag, message);
        }
    }

    public static void v(String tag, String message, Throwable error) {
        if (DEBUG) {
            int logResult = android.util.Log.v(tag, message, error);
            if (logResult > 0)
                logToFile(tag, message + "\r\n" + android.util.Log.getStackTraceString(error));
        }
    }

    public static void d(String tag, String message) {
        if (DEBUG) {
            int logResult = android.util.Log.d(tag, message);
            if (logResult > 0)
                logToFile(tag, message);
        }
    }

    public static void d(String tag, String message, Throwable error) {
        if (DEBUG) {
            int logResult = android.util.Log.d(tag, message, error);
            if (logResult > 0)
                logToFile(tag, message + "\r\n" + android.util.Log.getStackTraceString(error));
        }
    }

    public static void i(String tag, String message) {
        if (DEBUG) {
            int logResult = android.util.Log.i(tag, message);
            if (logResult > 0)
                logToFile(tag, message);
        }
    }

    public static void i(String tag, String message, Throwable error) {
        if (DEBUG) {
            int logResult = android.util.Log.i(tag, message, error);
            if (logResult > 0)
                logToFile(tag, message + "\r\n" + android.util.Log.getStackTraceString(error));
        }
    }

    public static void w(String tag, String message) {
        if (DEBUG) {
            int logResult = android.util.Log.w(tag, message);
            if (logResult > 0)
                logToFile(tag, message);
        }
    }

    public static void w(String tag, String message, Throwable error) {
        if (DEBUG) {
            int logResult = android.util.Log.w(tag, message, error);
            if (logResult > 0)
                logToFile(tag, message + "\r\n" + android.util.Log.getStackTraceString(error));
        }
    }

    private static String getDateTimeStamp() {
        Date dateNow = Calendar.getInstance().getTime();
        return (DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, Locale.US).format(dateNow));
    }

    private static void logToFile(String tag, String message) {
        try {
            File logFile = new File(Environment.getExternalStorageDirectory(), PATH);
            if (!logFile.exists()) {
                logFile.getParentFile().mkdirs();
                logFile.createNewFile();
            }
            if (logFile.length() > 2097152) { // 2 MB
                logFile.delete();
                logFile.createNewFile();
            }
            BufferedWriter writer = new BufferedWriter(new FileWriter(logFile, true));
            writer.write(String.format("%1s [%2s]:%3s\r\n", getDateTimeStamp(), tag, message));
            writer.close();

            if (logListener != null) {
                logListener.onLogged(tag, message);
            }
        } catch (IOException e) {
            android.util.Log.e(TAG, "Unable to log exception to file.", e);
        }
    }
}
@@ -893,16 +893,252 @@
  Log %7B%0A%0A
+    public static final int VERBOSE = 2;%0A    public static final int DEBUG = 3;%0A    public static final int INFO = 4;%0A    public static final int WARN = 5;%0A    public static final int ERROR = 6;%0A    public static final int ASSERT = 7;%0A%0A
     priv
@@ -1207,16 +1207,24 @@
 an DEBUG
+_ENABLED
  = false
@@ -1389,16 +1389,24 @@
    DEBUG
+_ENABLED
  = debug
@@ -1848,32 +1848,40 @@
        if (DEBUG
+_ENABLED
 ) %7B%0A            
@@ -2100,32 +2100,40 @@
        if (DEBUG
+_ENABLED
 ) %7B%0A            
@@ -2397,32 +2397,40 @@
        if (DEBUG
+_ENABLED
 ) %7B%0A            
@@ -2649,32 +2649,40 @@
        if (DEBUG
+_ENABLED
 ) %7B%0A            
@@ -2946,32 +2946,40 @@
        if (DEBUG
+_ENABLED
 ) %7B%0A            
@@ -3198,32 +3198,40 @@
        if (DEBUG
+_ENABLED
 ) %7B%0A            
@@ -3495,32 +3495,40 @@
        if (DEBUG
+_ENABLED
 ) %7B%0A            
@@ -3747,32 +3747,40 @@
        if (DEBUG
+_ENABLED
 ) %7B%0A            
@@ -4044,32 +4044,40 @@
        if (DEBUG
+_ENABLED
 ) %7B%0A            
@@ -4304,16 +4304,24 @@
 f (DEBUG
+_ENABLED
 ) %7B%0A    
@@ -4521,32 +4521,123 @@
        %7D%0A    %7D%0A%0A
+    public static boolean isLoggable(String string, int num) %7B%0A        return true;%0A    %7D%0A%0A
     private stat

add is loggable method