package com.novoda.notils.logger.simple;

/**
 * Wrapper around Android Log that will print
 * <p/>
 * 'TAG [Thread Name][Class name][Method name:line number] message'
 */
public final class Log {

    /**
     * Flag to enable or disable logs
     * Recommended you set this to BuildConfig.DEBUG in your class that extends Application
     */
    public static boolean SHOW_LOGS = false;
    /**
     * Log tag to use - Default is 'NoTils'
     */
    public static String TAG = "NoTils";
    /**
     * How deep to log stack traces - Default is 5
     */
    public static int STACK_DEPTH = 5;

    private static final int DOT_CLASS = 5;

    /**
     * Separator for the concatenation of the vaargs
     */
    private static String separator = " ";

    private Log() {
    }

    public static void setSeparator(String separator) {
        Log.separator = separator;
    }

    public static void v(Object... msg) {
        try {
            if (SHOW_LOGS) {
                android.util.Log.v(TAG, getDetailedLog(formatString(msg)));
            }
        } catch (RuntimeException ignore) {
            logError(ignore);
        }
    }

    public static void i(Object... msg) {
        try {
            if (SHOW_LOGS) {
                android.util.Log.i(TAG, getDetailedLog(formatString(msg)));
            }
        } catch (RuntimeException ignore) {
            logError(ignore);
        }
    }

    public static void d(Object... msg) {
        try {
            if (SHOW_LOGS) {
                android.util.Log.d(TAG, getDetailedLog(formatString(msg)));
            }
        } catch (RuntimeException ignore) {
            logError(ignore);
        }
    }

    public static void w(Object... msg) {
        try {
            if (SHOW_LOGS) {
                android.util.Log.w(TAG, getDetailedLog(formatString(msg)));
            }
        } catch (RuntimeException ignore) {
            logError(ignore);
        }
    }

    public static void e(Object... msg) {
        try {
            if (SHOW_LOGS) {
                android.util.Log.e(TAG, getDetailedLog(formatString(msg)));
            }
        } catch (RuntimeException ignore) {
            logError(ignore);
        }
    }

    @Deprecated
    /**
     * @deprecated use (Throwable t, Object... msg) 
     */
    public static void w(String msg, Throwable t) {
        w(t,msg);
    }

    public static void w(Throwable t, Object... msg) {
        try {
            if (SHOW_LOGS) {
                android.util.Log.w(TAG, getDetailedLog(formatString(msg)), t);
            }
        } catch (RuntimeException ignore) {
            logError(ignore);
        }
    }

    @Deprecated
    /**
     * @deprecated use (Throwable t, Object... msg)
     */
    public static void d(String msg, Throwable t) {
        d(t, msg);
    }

    public static void d(Throwable t, Object... msg) {
        try {
            if (SHOW_LOGS) {
                android.util.Log.d(TAG, getDetailedLog(formatString(msg)), t);
            }
        } catch (RuntimeException ignore) {
            logError(ignore);
        }
    }

    @Deprecated
    /**
     * @deprecated use (Throwable t, Object... msg) 
     */
    public static void e(String msg, Throwable t) {
        e(t, msg);
    }

    public static void e(Throwable t, Object... msg) {
        try {
            if (SHOW_LOGS) {
                android.util.Log.e(TAG, getDetailedLog(formatString(msg)), t);
            }
        } catch (RuntimeException ignore) {
            logError(ignore);
        }
    }

    @Deprecated
    /**
     * @deprecated use (Throwable t, Object... msg)
     */
    public static void wtf(String msg, Throwable t) {
        wtf(t, msg);
    }

    public static void wtf(Throwable t, Object... msg) {
        try {
            if (SHOW_LOGS) {
                android.util.Log.wtf(TAG, getDetailedLog(formatString(msg)), t);
            }
        } catch (RuntimeException ignore) {
            logError(ignore);
        }
    }

    private static String getDetailedLog(String msg) {
        return getDetailedLog(msg, STACK_DEPTH);
    }

    private static String getDetailedLog(String msg, int depth) {
        if (!SHOW_LOGS) {
            return msg;
        }
        Thread current = Thread.currentThread();
        final StackTraceElement trace = current.getStackTrace()[depth];
        final String filename = trace.getFileName();
        return "[" + current.getName() + "][" + filename.substring(0, filename.length() - DOT_CLASS) + "."
                + trace.getMethodName() + ":" + trace.getLineNumber() + "] " + msg;
    }

    private static void logError(Throwable ignore) {
        android.util.Log.e(TAG, "Error", ignore);
    }

    private static String formatString(Object... msg) {
        StringBuilder stringBuilder = new StringBuilder();
        for (Object o : msg) {
            stringBuilder.append(String.valueOf(o)).append(separator);
        }
        return stringBuilder.toString();
    }
}

@@ -970,37 +970,24 @@
             
-android.util.
 Log.v(TAG, g
@@ -1013,32 +1013,32 @@
 tString(msg)));%0A
+
             %7D%0A  
@@ -1223,37 +1223,24 @@
             
-android.util.
 Log.i(TAG, g
@@ -1476,37 +1476,24 @@
             
-android.util.
 Log.d(TAG, g
@@ -1729,37 +1729,24 @@
             
-android.util.
 Log.w(TAG, g
@@ -1982,37 +1982,24 @@
             
-android.util.
 Log.e(TAG, g
@@ -2287,16 +2287,17 @@
     w(t,
+ 
 msg);%0A  
@@ -2411,37 +2411,24 @@
             
-android.util.
 Log.w(TAG, g
@@ -2842,37 +2842,24 @@
             
-android.util.
 Log.d(TAG, g
@@ -3274,37 +3274,24 @@
             
-android.util.
 Log.e(TAG, g
@@ -3711,37 +3711,24 @@
             
-android.util.
 Log.wtf(TAG,
@@ -4546,21 +4546,8 @@
     
-android.util.
 Log.
@@ -4797,32 +4797,32 @@
 tor);%0A        %7D%0A
-
         return s
@@ -4851,11 +4851,308 @@
 );%0A    %7D
+%0A%0A    /**%0A     * Toggle if logs should be output or not. Recommended to bind against BuildConfig.DEBUG%0A     * @param showLogs %3Ccode%3Etrue%3C/code%3E for showing logs, %3Ccode%3Efalse%3C/code%3E to deactivate%0A     */%0A    public static void setShowLogs(boolean showLogs) %7B%0A        Log.SHOW_LOGS = showLogs;%0A    %7D
 %0A%7D%0A

Add setter for flag SHOW_LOGS to avoid findbugs warning