From 73a469a428371d668a564531197e064c5a3b401e Mon Sep 17 00:00:00 2001 From: Eric House Date: Mon, 14 Jan 2019 16:58:00 -0800 Subject: [PATCH] make logging respect force-on preference --- .../org/eehouse/android/xw4/DbgUtils.java | 52 ++----------------- .../java/org/eehouse/android/xw4/Log.java | 39 +++++++++++--- .../eehouse/android/xw4/PrefsDelegate.java | 2 +- .../java/org/eehouse/android/xw4/XWApp.java | 8 ++- 4 files changed, 39 insertions(+), 62 deletions(-) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DbgUtils.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DbgUtils.java index d124f66de..64907e806 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DbgUtils.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/DbgUtils.java @@ -39,49 +39,9 @@ import org.eehouse.android.xw4.loc.LocUtils; public class DbgUtils { private static final String TAG = DbgUtils.class.getSimpleName(); - private static boolean s_doLog = BuildConfig.DEBUG; - - private enum LogType { ERROR, WARN, DEBUG, INFO }; private static Time s_time = new Time(); - public static void logEnable( boolean enable ) - { - s_doLog = enable; - } - - public static void logEnable( Context context ) - { - boolean on = BuildConfig.DEBUG || - XWPrefs.getPrefsBoolean( context, R.string.key_logging_on, false ); - logEnable( on ); - } - - private static void callLog( LogType lt, String tag, String fmt, - Object... args ) - { - if ( s_doLog ) { - String msg = new Formatter().format( fmt, args ).toString(); - switch( lt ) { - case DEBUG: - Log.d( tag, msg ); - break; - case WARN: - Log.w( tag, msg ); - break; - case INFO: - Log.i( tag, msg ); - break; - case ERROR: - Log.e( tag, msg ); - break; - default: - Assert.fail(); - break; - } - } - } - public static void showf( String format, Object... args ) { showf( XWApp.getContext(), format, args ); @@ -121,7 +81,7 @@ public class DbgUtils { public static void printStack( String tag, StackTraceElement[] trace ) { - if ( s_doLog && null != trace ) { + if ( null != trace ) { // 1: skip printStack etc. for ( int ii = 1; ii < trace.length; ++ii ) { Log.d( tag, "ste %d: %s", ii, trace[ii].toString() ); @@ -131,9 +91,7 @@ public class DbgUtils { public static void printStack( String tag ) { - if ( s_doLog ) { - printStack( tag, Thread.currentThread().getStackTrace() ); - } + printStack( tag, Thread.currentThread().getStackTrace() ); } public static void printStack( String tag, Exception ex ) @@ -156,10 +114,8 @@ public class DbgUtils { public static void dumpCursor( Cursor cursor ) { - if ( s_doLog ) { - String dump = DatabaseUtils.dumpCursorToString( cursor ); - Log.i( TAG, "cursor: %s", dump ); - } + String dump = DatabaseUtils.dumpCursorToString( cursor ); + Log.i( TAG, "cursor: %s", dump ); } // public static String secondsToDateStr( long seconds ) diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Log.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Log.java index 02ac43220..f01ab673e 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Log.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/Log.java @@ -20,6 +20,8 @@ package org.eehouse.android.xw4; +import android.content.Context; + import java.util.Formatter; public class Log { @@ -27,27 +29,48 @@ public class Log { private static final boolean LOGGING_ENABLED = BuildConfig.DEBUG || !BuildConfig.IS_TAGGED_BUILD; private static final boolean ERROR_LOGGING_ENABLED = true; + private static boolean sEnabled = BuildConfig.DEBUG; - public static void d( String tag, String fmt, Object... args ) { - if ( LOGGING_ENABLED ) { + public static void enable( boolean newVal ) + { + sEnabled = newVal; + } + + public static void enable( Context context ) + { + boolean on = LOGGING_ENABLED || + XWPrefs.getPrefsBoolean( context, R.string.key_logging_on, + LOGGING_ENABLED ); + enable( on ); + } + + public static void d( String tag, String fmt, Object... args ) + { + if ( sEnabled ) { String str = new Formatter().format( fmt, args ).toString(); android.util.Log.d( PRE_TAG + tag, str ); } } - public static void w( String tag, String fmt, Object... args ) { - if ( LOGGING_ENABLED ) { + + public static void w( String tag, String fmt, Object... args ) + { + if ( sEnabled ) { String str = new Formatter().format( fmt, args ).toString(); android.util.Log.w( PRE_TAG + tag, str ); } } - public static void e( String tag, String fmt, Object... args ) { + + public static void e( String tag, String fmt, Object... args ) + { if ( ERROR_LOGGING_ENABLED ) { String str = new Formatter().format( fmt, args ).toString(); android.util.Log.e( PRE_TAG + tag, str ); } } - public static void i( String tag, String fmt, Object... args ) { - if ( LOGGING_ENABLED ) { + + public static void i( String tag, String fmt, Object... args ) + { + if ( sEnabled ) { String str = new Formatter().format( fmt, args ).toString(); android.util.Log.i( PRE_TAG + tag, str ); } @@ -55,7 +78,7 @@ public class Log { public static void ex( String tag, Exception exception ) { - if ( LOGGING_ENABLED ) { + if ( sEnabled ) { w( tag, "Exception: %s", exception.toString() ); DbgUtils.printStack( tag, exception.getStackTrace() ); } diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/PrefsDelegate.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/PrefsDelegate.java index faef1f88b..bc5e5e277 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/PrefsDelegate.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/PrefsDelegate.java @@ -185,7 +185,7 @@ public class PrefsDelegate extends DelegateBase if ( s_keysHash.containsKey( key ) ) { switch( s_keysHash.get( key ) ) { case R.string.key_logging_on: - DbgUtils.logEnable( sp.getBoolean( key, false ) ); + Log.enable( sp.getBoolean( key, false ) ); break; case R.string.key_show_sms: SMSService.smsToastEnable( sp.getBoolean( key, false ) ); diff --git a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWApp.java b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWApp.java index 06890e6fe..7b4a7e75b 100644 --- a/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWApp.java +++ b/xwords4/android/app/src/main/java/org/eehouse/android/xw4/XWApp.java @@ -71,11 +71,9 @@ public class XWApp extends Application implements LifecycleObserver { ProcessLifecycleOwner.get().getLifecycle().addObserver(this); - // This one line should always get logged even if logging is - // off -- because logging is on by default until logEnable is - // called. - Log.i( TAG, "onCreate(); git_rev=%s", getString( R.string.git_rev ) ); - DbgUtils.logEnable( this ); + android.util.Log.i( TAG, "onCreate(); git_rev=" + + getString( R.string.git_rev ) ); + Log.enable( this ); OnBootReceiver.startTimers( this );