From d376679d0033301122f26a0a9571346495f74e96 Mon Sep 17 00:00:00 2001 From: Eric House Date: Fri, 22 Apr 2016 21:14:36 -0700 Subject: [PATCH] add new database table for persisting logs. DEBUG builds only for now. Keep the most recent 5K worth of messages. TODO: add a display activity and the ability to email them. --- .../src/org/eehouse/android/xw4/DBHelper.java | 22 +++++++++++-- .../src/org/eehouse/android/xw4/DBUtils.java | 32 +++++++++++++++++++ .../src/org/eehouse/android/xw4/DbgUtils.java | 21 ++++++++++-- .../src/org/eehouse/android/xw4/XWApp.java | 4 +++ 4 files changed, 73 insertions(+), 6 deletions(-) diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java index 840aeec57..162801c5c 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBHelper.java @@ -43,8 +43,9 @@ public class DBHelper extends SQLiteOpenHelper { public static final String TABLE_NAME_PAIRS = "pairs"; public static final String TABLE_NAME_INVITES = "invites"; public static final String TABLE_NAME_CHAT = "chat"; + public static final String TABLE_NAME_LOGS = "logs"; private static final String DB_NAME = "xwdb"; - private static final int DB_VERSION = 26; + private static final int DB_VERSION = 27; public static final String GAME_NAME = "GAME_NAME"; public static final String VISID = "VISID"; @@ -114,6 +115,7 @@ public class DBHelper extends SQLiteOpenHelper { public static final String SENDER = "SENDER"; public static final String MESSAGE = "MESSAGE"; + public static final String TAG = "TAG"; private Context m_context; @@ -216,6 +218,12 @@ public class DBHelper extends SQLiteOpenHelper { ,{ MESSAGE, "TEXT" } }; + private static final String[][] s_logsSchema = { + { TIMESTAMP, "DATETIME DEFAULT CURRENT_TIMESTAMP" }, + { MESSAGE, "TEXT" }, + { TAG, "TEXT" }, + }; + public DBHelper( Context context ) { super( context, DB_NAME, null, DB_VERSION ); @@ -241,13 +249,14 @@ public class DBHelper extends SQLiteOpenHelper { createPairsTable( db ); createInvitesTable( db ); createChatsTable( db ); + createLogsTable( db ); } @Override @SuppressWarnings("fallthrough") public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) { - DbgUtils.logf( "onUpgrade: old: %d; new: %d", oldVersion, newVersion ); + DbgUtils.logf( false, "onUpgrade: old: %d; new: %d", oldVersion, newVersion ); boolean madeSumTable = false; switch( oldVersion ) { @@ -310,7 +319,9 @@ public class DBHelper extends SQLiteOpenHelper { createInvitesTable( db ); case 25: createChatsTable( db ); - + case 26: + createLogsTable( db ); + break; default: db.execSQL( "DROP TABLE " + TABLE_NAME_SUM + ";" ); @@ -419,6 +430,11 @@ public class DBHelper extends SQLiteOpenHelper { createTable( db, TABLE_NAME_CHAT, s_chatsSchema ); } + private void createLogsTable( SQLiteDatabase db ) + { + createTable( db, TABLE_NAME_LOGS, s_logsSchema ); + } + // Move all existing games to the row previously named "cur games' private void moveToCurGames( SQLiteDatabase db ) { diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java index e806636b2..2c154f928 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DBUtils.java @@ -67,6 +67,9 @@ public class DBUtils { public static final int GROUPID_UNSPEC = -1; public static final String KEY_NEWGAMECOUNT = "DBUtils.newGameCount"; + // how many log rows to keep? + private static final int LOGLIMIT = 5000; + private static final String DICTS_SEP = ","; private static final String ROW_ID = "rowid"; @@ -2405,6 +2408,35 @@ public class DBUtils { return bytes; } + public static void appendLog( String tag, String msg ) + { + Context context = XWApp.getContext(); + if ( null != context ) { + appendLog( context, msg ); + } + } + + private static void appendLog( Context context, String msg ) + { + ContentValues values = new ContentValues(); + values.put( DBHelper.MESSAGE, msg ); + + initDB( context ); + synchronized( s_dbHelper ) { + SQLiteDatabase db = s_dbHelper.getWritableDatabase(); + long rowid = db.insert( DBHelper.TABLE_NAME_LOGS, null, values ); + + if ( 0 == (rowid % (LOGLIMIT / 10)) ) { + String where = + String.format( "not rowid in (select rowid from %s order by TIMESTAMP desc limit %d)", + DBHelper.TABLE_NAME_LOGS, LOGLIMIT ); + int nGone = db.delete( DBHelper.TABLE_NAME_LOGS, where, null ); + DbgUtils.logf( false, "appendLog(): deleted %d rows", nGone ); + } + db.close(); + } + } + private static void copyGameDB( Context context, boolean toSDCard ) { String name = DBHelper.getDBName(); diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DbgUtils.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DbgUtils.java index d27f4cea3..7d3cf337b 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/DbgUtils.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/DbgUtils.java @@ -61,7 +61,12 @@ public class DbgUtils { logEnable( on ); } - public static void logf( String msg ) + public static void logf( String msg ) + { + logf( true, msg ); + } + + public static void logf( boolean persist, String msg ) { if ( s_doLog ) { String time = ""; @@ -71,15 +76,25 @@ public class DbgUtils { time = s_time.format("[%H:%M:%S]-"); } long id = Thread.currentThread().getId(); - Log.d( TAG, time + id + "-" + msg ); + msg = time + id + "-" + msg; + Log.d( TAG, msg ); + + if ( persist && BuildConfig.DEBUG ) { + DBUtils.appendLog( TAG, msg ); + } } } // logf public static void logf( String format, Object... args ) + { + logf( true, format, args ); + } + + public static void logf( boolean persist, String format, Object... args ) { if ( s_doLog ) { Formatter formatter = new Formatter(); - logf( formatter.format( format, args ).toString() ); + logf( persist, formatter.format( format, args ).toString() ); } } // logf diff --git a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java index 5f649d510..2fbe4a02e 100644 --- a/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java +++ b/xwords4/android/XWords4/src/org/eehouse/android/xw4/XWApp.java @@ -54,10 +54,12 @@ public class XWApp extends Application { private static UUID s_UUID = null; private static Boolean s_onEmulator = null; + private static Context s_context = null; @Override public void onCreate() { + s_context = this; super.onCreate(); // This one line should always get logged even if logging is @@ -118,4 +120,6 @@ public class XWApp extends Application { } return s_onEmulator; } + + public static Context getContext() { return s_context; } }