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.
This commit is contained in:
Eric House 2016-04-22 21:14:36 -07:00
parent 1216ec49d7
commit d376679d00
4 changed files with 73 additions and 6 deletions

View file

@ -43,8 +43,9 @@ public class DBHelper extends SQLiteOpenHelper {
public static final String TABLE_NAME_PAIRS = "pairs"; public static final String TABLE_NAME_PAIRS = "pairs";
public static final String TABLE_NAME_INVITES = "invites"; public static final String TABLE_NAME_INVITES = "invites";
public static final String TABLE_NAME_CHAT = "chat"; 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 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 GAME_NAME = "GAME_NAME";
public static final String VISID = "VISID"; 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 SENDER = "SENDER";
public static final String MESSAGE = "MESSAGE"; public static final String MESSAGE = "MESSAGE";
public static final String TAG = "TAG";
private Context m_context; private Context m_context;
@ -216,6 +218,12 @@ public class DBHelper extends SQLiteOpenHelper {
,{ MESSAGE, "TEXT" } ,{ MESSAGE, "TEXT" }
}; };
private static final String[][] s_logsSchema = {
{ TIMESTAMP, "DATETIME DEFAULT CURRENT_TIMESTAMP" },
{ MESSAGE, "TEXT" },
{ TAG, "TEXT" },
};
public DBHelper( Context context ) public DBHelper( Context context )
{ {
super( context, DB_NAME, null, DB_VERSION ); super( context, DB_NAME, null, DB_VERSION );
@ -241,13 +249,14 @@ public class DBHelper extends SQLiteOpenHelper {
createPairsTable( db ); createPairsTable( db );
createInvitesTable( db ); createInvitesTable( db );
createChatsTable( db ); createChatsTable( db );
createLogsTable( db );
} }
@Override @Override
@SuppressWarnings("fallthrough") @SuppressWarnings("fallthrough")
public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion ) 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; boolean madeSumTable = false;
switch( oldVersion ) { switch( oldVersion ) {
@ -310,7 +319,9 @@ public class DBHelper extends SQLiteOpenHelper {
createInvitesTable( db ); createInvitesTable( db );
case 25: case 25:
createChatsTable( db ); createChatsTable( db );
case 26:
createLogsTable( db );
break; break;
default: default:
db.execSQL( "DROP TABLE " + TABLE_NAME_SUM + ";" ); db.execSQL( "DROP TABLE " + TABLE_NAME_SUM + ";" );
@ -419,6 +430,11 @@ public class DBHelper extends SQLiteOpenHelper {
createTable( db, TABLE_NAME_CHAT, s_chatsSchema ); 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' // Move all existing games to the row previously named "cur games'
private void moveToCurGames( SQLiteDatabase db ) private void moveToCurGames( SQLiteDatabase db )
{ {

View file

@ -67,6 +67,9 @@ public class DBUtils {
public static final int GROUPID_UNSPEC = -1; public static final int GROUPID_UNSPEC = -1;
public static final String KEY_NEWGAMECOUNT = "DBUtils.newGameCount"; 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 DICTS_SEP = ",";
private static final String ROW_ID = "rowid"; private static final String ROW_ID = "rowid";
@ -2405,6 +2408,35 @@ public class DBUtils {
return bytes; 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 ) private static void copyGameDB( Context context, boolean toSDCard )
{ {
String name = DBHelper.getDBName(); String name = DBHelper.getDBName();

View file

@ -61,7 +61,12 @@ public class DbgUtils {
logEnable( on ); 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 ) { if ( s_doLog ) {
String time = ""; String time = "";
@ -71,15 +76,25 @@ public class DbgUtils {
time = s_time.format("[%H:%M:%S]-"); time = s_time.format("[%H:%M:%S]-");
} }
long id = Thread.currentThread().getId(); 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 } // logf
public static void logf( String format, Object... args ) 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 ) { if ( s_doLog ) {
Formatter formatter = new Formatter(); Formatter formatter = new Formatter();
logf( formatter.format( format, args ).toString() ); logf( persist, formatter.format( format, args ).toString() );
} }
} // logf } // logf

View file

@ -54,10 +54,12 @@ public class XWApp extends Application {
private static UUID s_UUID = null; private static UUID s_UUID = null;
private static Boolean s_onEmulator = null; private static Boolean s_onEmulator = null;
private static Context s_context = null;
@Override @Override
public void onCreate() public void onCreate()
{ {
s_context = this;
super.onCreate(); super.onCreate();
// This one line should always get logged even if logging is // This one line should always get logged even if logging is
@ -118,4 +120,6 @@ public class XWApp extends Application {
} }
return s_onEmulator; return s_onEmulator;
} }
public static Context getContext() { return s_context; }
} }