/*
 * Copyright 2016 Johan Walles <johan.walles@gmail.com>
 *
 * 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.gmail.walles.johan.batterylogger;

import android.text.TextUtils;
import android.util.Log;

import com.crashlytics.android.Crashlytics;

import timber.log.Timber;

public class TimberUtil {
    private static Class<Timber> initializedLoggingClass = null;

    private TimberUtil() {
        // Don't let people instantiate this class
    }

    public static void setUpLogging() {
        if (initializedLoggingClass == Timber.class) {
            return;
        }
        initializedLoggingClass = Timber.class;

        Timber.plant(new AndroidLogTree());
        if (!BuildConfig.DEBUG) {
            Timber.plant(new CrashlyticsTree());
        }
    }

    private static class CrashlyticsTree extends Timber.Tree {
        @Override
        protected void log(int priority, String tag, String message, Throwable t) {
            if (t == null && priority < Log.WARN) {
                return;
            }

            if (TextUtils.isEmpty(tag)) {
                tag = "BatteryLogger";
            }

            Crashlytics.log(priority, tag, message);

            if (t != null) {
                Crashlytics.logException(t);
            }
        }
    }

    private static class AndroidLogTree extends Timber.Tree {
        @Override
        protected void log(int priority, String tag, String message, Throwable t) {
            if (TextUtils.isEmpty(tag)) {
                tag = "BatteryLogger";
            }
            if (t != null) {
                message += "\n" + Log.getStackTraceString(t);
            }
            Log.println(priority, tag, message);
        }
    }
}

@@ -1146,90 +1146,8 @@
 s;%0A%0A
-        Timber.plant(new AndroidLogTree());%0A        if (!BuildConfig.DEBUG) %7B%0A    
     
@@ -1187,26 +1187,16 @@
 ree());%0A
-        %7D%0A
     %7D%0A%0A 
@@ -1374,16 +1374,148 @@
      if 
+(BuildConfig.DEBUG) %7B%0A                // This block intentionally left blank; fall through and log everything%0A            %7D else if 
 (t == nu
@@ -1589,32 +1589,107 @@
 %0A            if 
+(BuildConfig.DEBUG) %7B%0A                tag = %22DEBUG%22;%0A            %7D else if 
 (TextUtils.isEmp
@@ -1745,32 +1745,95 @@
 %0A            %7D%0A%0A
+            // This call logs to *both* Crashlytics and LogCat%0A
             Cras
@@ -1960,330 +1960,38 @@
     
-%7D%0A
     
-    %7D%0A    %7D%0A%0A    private static class AndroidLogTree extends Timber.Tree %7B%0A        @Override%0A        protected void log(int priority, String tag, String message, Throwable t) %7B%0A            if (TextUtils.isEmpty(tag)) %7B%0A                tag = %22BatteryLogger%22;%0A            %7D%0A            if (t != null) %7B%0A               
+Log.println(priority, tag,
  mes
@@ -1996,17 +1996,16 @@
 essage +
-=
  %22%5Cn%22 + 
@@ -2030,16 +2030,17 @@
 tring(t)
+)
 ;%0A      
@@ -2051,57 +2051,8 @@
   %7D%0A
-            Log.println(priority, tag, message);%0A
     

Simplify logging destination