make logging respect force-on preference

This commit is contained in:
Eric House 2019-01-14 16:58:00 -08:00
parent 788113ee28
commit 73a469a428
4 changed files with 39 additions and 62 deletions

View file

@ -39,49 +39,9 @@ import org.eehouse.android.xw4.loc.LocUtils;
public class DbgUtils { public class DbgUtils {
private static final String TAG = DbgUtils.class.getSimpleName(); 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(); 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 ) public static void showf( String format, Object... args )
{ {
showf( XWApp.getContext(), format, args ); showf( XWApp.getContext(), format, args );
@ -121,7 +81,7 @@ public class DbgUtils {
public static void printStack( String tag, StackTraceElement[] trace ) public static void printStack( String tag, StackTraceElement[] trace )
{ {
if ( s_doLog && null != trace ) { if ( null != trace ) {
// 1: skip printStack etc. // 1: skip printStack etc.
for ( int ii = 1; ii < trace.length; ++ii ) { for ( int ii = 1; ii < trace.length; ++ii ) {
Log.d( tag, "ste %d: %s", ii, trace[ii].toString() ); Log.d( tag, "ste %d: %s", ii, trace[ii].toString() );
@ -131,9 +91,7 @@ public class DbgUtils {
public static void printStack( String tag ) 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 ) public static void printStack( String tag, Exception ex )
@ -156,10 +114,8 @@ public class DbgUtils {
public static void dumpCursor( Cursor cursor ) public static void dumpCursor( Cursor cursor )
{ {
if ( s_doLog ) { String dump = DatabaseUtils.dumpCursorToString( cursor );
String dump = DatabaseUtils.dumpCursorToString( cursor ); Log.i( TAG, "cursor: %s", dump );
Log.i( TAG, "cursor: %s", dump );
}
} }
// public static String secondsToDateStr( long seconds ) // public static String secondsToDateStr( long seconds )

View file

@ -20,6 +20,8 @@
package org.eehouse.android.xw4; package org.eehouse.android.xw4;
import android.content.Context;
import java.util.Formatter; import java.util.Formatter;
public class Log { public class Log {
@ -27,27 +29,48 @@ public class Log {
private static final boolean LOGGING_ENABLED private static final boolean LOGGING_ENABLED
= BuildConfig.DEBUG || !BuildConfig.IS_TAGGED_BUILD; = BuildConfig.DEBUG || !BuildConfig.IS_TAGGED_BUILD;
private static final boolean ERROR_LOGGING_ENABLED = true; private static final boolean ERROR_LOGGING_ENABLED = true;
private static boolean sEnabled = BuildConfig.DEBUG;
public static void d( String tag, String fmt, Object... args ) { public static void enable( boolean newVal )
if ( LOGGING_ENABLED ) { {
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(); String str = new Formatter().format( fmt, args ).toString();
android.util.Log.d( PRE_TAG + tag, str ); 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(); String str = new Formatter().format( fmt, args ).toString();
android.util.Log.w( PRE_TAG + tag, str ); 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 ) { if ( ERROR_LOGGING_ENABLED ) {
String str = new Formatter().format( fmt, args ).toString(); String str = new Formatter().format( fmt, args ).toString();
android.util.Log.e( PRE_TAG + tag, str ); 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(); String str = new Formatter().format( fmt, args ).toString();
android.util.Log.i( PRE_TAG + tag, str ); android.util.Log.i( PRE_TAG + tag, str );
} }
@ -55,7 +78,7 @@ public class Log {
public static void ex( String tag, Exception exception ) public static void ex( String tag, Exception exception )
{ {
if ( LOGGING_ENABLED ) { if ( sEnabled ) {
w( tag, "Exception: %s", exception.toString() ); w( tag, "Exception: %s", exception.toString() );
DbgUtils.printStack( tag, exception.getStackTrace() ); DbgUtils.printStack( tag, exception.getStackTrace() );
} }

View file

@ -185,7 +185,7 @@ public class PrefsDelegate extends DelegateBase
if ( s_keysHash.containsKey( key ) ) { if ( s_keysHash.containsKey( key ) ) {
switch( s_keysHash.get( key ) ) { switch( s_keysHash.get( key ) ) {
case R.string.key_logging_on: case R.string.key_logging_on:
DbgUtils.logEnable( sp.getBoolean( key, false ) ); Log.enable( sp.getBoolean( key, false ) );
break; break;
case R.string.key_show_sms: case R.string.key_show_sms:
SMSService.smsToastEnable( sp.getBoolean( key, false ) ); SMSService.smsToastEnable( sp.getBoolean( key, false ) );

View file

@ -71,11 +71,9 @@ public class XWApp extends Application implements LifecycleObserver {
ProcessLifecycleOwner.get().getLifecycle().addObserver(this); ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
// This one line should always get logged even if logging is android.util.Log.i( TAG, "onCreate(); git_rev="
// off -- because logging is on by default until logEnable is + getString( R.string.git_rev ) );
// called. Log.enable( this );
Log.i( TAG, "onCreate(); git_rev=%s", getString( R.string.git_rev ) );
DbgUtils.logEnable( this );
OnBootReceiver.startTimers( this ); OnBootReceiver.startTimers( this );