package com.tf.reusable.demoapplication.util;

import android.os.Environment;
import android.util.Log;

import com.tf.reusable.demoapplication.MainApplication;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.Calendar;

/**
 * Created by kamran on 16/12/14.
 */
public class Logger {
    private static final String APP_NAME = "DemoApplication";

    public static boolean ENABLED = true;

    public static void i(String tag, String message) {
        if (ENABLED)
            try {
                Log.i(tag, message);
            } catch (Exception e) {
                //e.printStackTrace();
            }
    }

    public static void d(String tag, String message) {
        if (ENABLED)
            try {
                Log.d(tag, message);
            } catch (Exception e) {
                //e.printStackTrace();
            }
    }

    public static void e(String tag, String message) {
        if (ENABLED)
            try {
                Log.e(tag, message);
            } catch (Exception e) {
                //e.printStackTrace();
            }
    }

    public static void v(String tag, String message) {
        if (ENABLED)
            try {
                Log.v(tag, message);
            } catch (Exception e) {
                //e.printStackTrace();
            }
    }

    public static void w(String tag, String message) {
        if (ENABLED)
            try {
                Log.w(tag, message);
            } catch (Exception e) {
                //e.printStackTrace();
            }
    }

    public static void wtf(String tag, String message) {
        if (ENABLED)
            try {
                Log.wtf(tag, message);
            } catch (Exception e) {
                //e.printStackTrace();
            }
    }

    public static Thread.UncaughtExceptionHandler uncaughtExceptionHandler
            = new Thread.UncaughtExceptionHandler() {
        @Override
        public void uncaughtException(Thread thread, Throwable ex) {
            ex.printStackTrace();

            try {
                File file = new File(Environment.getExternalStorageDirectory()
                        +"/" + APP_NAME + "/log-" + Calendar.getInstance().getTime() + ".txt");
                FileOutputStream fileOutputStream = new FileOutputStream(file);

                file.getParentFile().mkdirs();

                StringWriter stringWriter = new StringWriter();
                PrintWriter printWriter = new PrintWriter(stringWriter);
                ex.printStackTrace(printWriter);

                fileOutputStream.write(stringWriter.toString().getBytes());
                fileOutputStream.close();

                if (ENABLED)
                    Log.e("Uncaught", stringWriter.toString());

                MainApplication.defaultExceptionHandler.uncaughtException(thread, ex);
            }
            catch (Exception e) {
                e.printStackTrace();
            }
        }
    };
}

@@ -1821,32 +1821,365 @@
        %7D%0A    %7D%0A%0A
+    public static void printStackTrace(Throwable e) %7B%0A        if (!ENABLED)%0A            return;%0A%0A        StringWriter stringWriter = new StringWriter();%0A        PrintWriter printWriter = new PrintWriter(stringWriter);%0A%0A        e.printStackTrace(printWriter);%0A%0A        Log.wtf(e.getClass().getName(), stringWriter.toString());%0A    %7D%0A%0A
     public stati

Add a method to print stack trace