/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package info.zamojski.soft.towercollector.logging;

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

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import info.zamojski.soft.towercollector.MyApplication;
import info.zamojski.soft.towercollector.utils.ApkUtils;
import info.zamojski.soft.towercollector.utils.FileUtils;
import timber.log.Timber;

public class FileLoggingTree extends Timber.DebugTree {

    private static final String TAG = FileLoggingTree.class.getSimpleName();

    private static final String[] LEVELS = new String[]{"disabled", "undefined", "V", "D", "I", "W", "E"};
    private static final int DISABLED = 0;

    private final static SimpleDateFormat shortDateFormat = new SimpleDateFormat("MM-dd-HH:mm:ss.SSS", Locale.getDefault());

    private OutputStreamWriter osw;
    private boolean firstRun = true;
    private int priority = DISABLED;

    public final static FileLoggingTree INSTANCE = new FileLoggingTree();

    private FileLoggingTree() {
        // Avoid instantiation
    }

    public FileLoggingTree setPriority(int priority) {
        this.priority = priority;
        return this;
    }

    @Override
    protected void log(int priority, String tag, String message, Throwable t) {
        if (firstRun) {
            Class clazz = FileLoggingTree.class;
            synchronized (clazz) {
                if (firstRun) {
                    initialize();
                    firstRun = false;
                    // write identification data once initialization finished
                    log(Log.DEBUG, ApkUtils.getDeviceName());
                    log(Log.DEBUG, ApkUtils.getApkVersionName(MyApplication.getApplication()));
                }
            }
        }
        // Skip if storage is unavailable or initialization failed
        if (osw == null) {
            return;
        }
        try {
            osw.write(String.format(Locale.ENGLISH, "%s %s/%s(% 5d): %s\r\n", shortDateFormat.format(new Date()), LEVELS[priority], tag, Process.myPid(), message));
            osw.flush();
        } catch (Exception ex) {
            Timber.e(ex, "Failed to write log file!");
        }
    }

    @Override
    protected boolean isLoggable(String tag, int priority) {
        return (this.priority > 0 && this.priority <= priority);
    }

    private void initialize() {
        try {
            File logDir = new File(Environment.getExternalStorageDirectory().getPath(), "TowerCollector");
            if (!logDir.exists()) {
                logDir.mkdirs();
            }
            File logFile = new File(logDir, FileUtils.getCurrentDateFilename("log"));
            FileOutputStream fis = new FileOutputStream(logFile);
            osw = new OutputStreamWriter(fis);
        } catch (Exception ex) {
            Timber.e(ex, "Failed to open log file!");
        }
    }
}

@@ -247,24 +247,64 @@
 r.logging;%0A%0A
+import android.annotation.SuppressLint;%0A
 import andro
@@ -349,16 +349,59 @@
 rocess;%0A
+import android.support.annotation.NonNull;%0A
 import a
@@ -1566,32 +1566,66 @@
 rn this;%0A    %7D%0A%0A
+    @SuppressLint(%22LogNotTimber%22)%0A
     @Override%0A  
@@ -1670,16 +1670,25 @@
 ing tag,
+ @NonNull
  String 
@@ -1751,22 +1751,22 @@
     
-Class clazz = 
+synchronized (
 File
@@ -1786,41 +1786,8 @@
 lass
-;%0A            synchronized (clazz
 ) %7B%0A
@@ -2537,35 +2537,33 @@
             
-Timber.e(ex
+Log.e(TAG
 , %22Failed to
@@ -2571,32 +2571,36 @@
 write log file!%22
+, ex
 );%0A        %7D%0A   
@@ -2746,24 +2746,58 @@
 ty);%0A    %7D%0A%0A
+    @SuppressLint(%22LogNotTimber%22)%0A
     private 
@@ -3268,19 +3268,17 @@
     
-Timber.e(ex
+Log.e(TAG
 , %22F
@@ -3301,16 +3301,20 @@
 g file!%22
+, ex
 );%0A     

Fix exception when logging to file.