mirror of
git://xwords.git.sourceforge.net/gitroot/xwords/xwords
synced 2025-01-09 05:24:44 +01:00
do log db stuff on background thread
This commit is contained in:
parent
5b23b40c4f
commit
ddf0f185d4
2 changed files with 133 additions and 68 deletions
|
@ -76,7 +76,7 @@ public class GamesListDelegate extends ListDelegateBase
|
||||||
implements OnItemLongClickListener,
|
implements OnItemLongClickListener,
|
||||||
DBUtils.DBChangeListener, SelectableItem,
|
DBUtils.DBChangeListener, SelectableItem,
|
||||||
DownloadFinishedListener, DlgDelegate.HasDlgDelegate,
|
DownloadFinishedListener, DlgDelegate.HasDlgDelegate,
|
||||||
GroupStateListener {
|
GroupStateListener, Log.ResultProcs {
|
||||||
private static final String TAG = GamesListDelegate.class.getSimpleName();
|
private static final String TAG = GamesListDelegate.class.getSimpleName();
|
||||||
|
|
||||||
|
|
||||||
|
@ -1306,6 +1306,38 @@ public class GamesListDelegate extends ListDelegateBase
|
||||||
return selected;
|
return selected;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Log.ResultProcs interface
|
||||||
|
@Override
|
||||||
|
public void onDumped( final File logLoc )
|
||||||
|
{
|
||||||
|
runOnUiThread( new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
String dumpMsg;
|
||||||
|
if ( null == logLoc ) {
|
||||||
|
dumpMsg = LocUtils.getString( m_activity,
|
||||||
|
R.string.logstore_notdumped );
|
||||||
|
} else {
|
||||||
|
dumpMsg = LocUtils
|
||||||
|
.getString( m_activity, R.string.logstore_dumped_fmt,
|
||||||
|
logLoc.getPath() );
|
||||||
|
}
|
||||||
|
makeOkOnlyBuilder( dumpMsg ).show();
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onCleared( final int nDumped )
|
||||||
|
{
|
||||||
|
runOnUiThread( new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
Utils.showToast( m_activity, R.string.logstore_cleared_fmt, nDumped );
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
// DlgDelegate.DlgClickNotify interface
|
// DlgDelegate.DlgClickNotify interface
|
||||||
@Override
|
@Override
|
||||||
public boolean onPosButton( Action action, Object[] params )
|
public boolean onPosButton( Action action, Object[] params )
|
||||||
|
@ -1413,27 +1445,11 @@ public class GamesListDelegate extends ListDelegateBase
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case WRITE_LOG_DB:
|
case WRITE_LOG_DB:
|
||||||
final File logLoc = Log.dumpStored();
|
Log.dumpStored( this );
|
||||||
post( new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
String dumpMsg;
|
|
||||||
if ( null == logLoc ) {
|
|
||||||
dumpMsg = LocUtils.getString( m_activity,
|
|
||||||
R.string.logstore_notdumped );
|
|
||||||
} else {
|
|
||||||
dumpMsg = LocUtils
|
|
||||||
.getString( m_activity, R.string.logstore_dumped_fmt,
|
|
||||||
logLoc.getPath() );
|
|
||||||
}
|
|
||||||
makeOkOnlyBuilder( dumpMsg ).show();
|
|
||||||
}
|
|
||||||
} );
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case CLEAR_LOG_DB:
|
case CLEAR_LOG_DB:
|
||||||
int nDumped = Log.clearStored();
|
Log.clearStored( this );
|
||||||
Utils.showToast( m_activity, R.string.logstore_cleared_fmt, nDumped );
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case ASKED_PHONE_STATE:
|
case ASKED_PHONE_STATE:
|
||||||
|
|
|
@ -35,6 +35,7 @@ import java.io.OutputStream;
|
||||||
import java.io.OutputStreamWriter;
|
import java.io.OutputStreamWriter;
|
||||||
import java.lang.ref.WeakReference;
|
import java.lang.ref.WeakReference;
|
||||||
import java.util.Formatter;
|
import java.util.Formatter;
|
||||||
|
import java.util.concurrent.LinkedBlockingQueue;
|
||||||
|
|
||||||
public class Log {
|
public class Log {
|
||||||
private static final String TAG = Log.class.getSimpleName();
|
private static final String TAG = Log.class.getSimpleName();
|
||||||
|
@ -42,7 +43,7 @@ public class Log {
|
||||||
private static final String KEY_USE_DB = TAG + "/useDB";
|
private static final String KEY_USE_DB = TAG + "/useDB";
|
||||||
private static final boolean LOGGING_ENABLED = BuildConfig.NON_RELEASE;
|
private static final boolean LOGGING_ENABLED = BuildConfig.NON_RELEASE;
|
||||||
private static final boolean ERROR_LOGGING_ENABLED = true;
|
private static final boolean ERROR_LOGGING_ENABLED = true;
|
||||||
private static final String LOGS_FILE_NAME = BuildConfig.FLAVOR + "_logsDB.txt";
|
private static final String LOGS_FILE_NAME_FMT = BuildConfig.FLAVOR + "_logsDB_%d.txt";
|
||||||
private static final String LOGS_DB_NAME = "xwlogs_db";
|
private static final String LOGS_DB_NAME = "xwlogs_db";
|
||||||
private static final String LOGS_TABLE_NAME = "logs";
|
private static final String LOGS_TABLE_NAME = "logs";
|
||||||
private static final String COL_ENTRY = "entry";
|
private static final String COL_ENTRY = "entry";
|
||||||
|
@ -89,24 +90,27 @@ public class Log {
|
||||||
sEnabled = newVal;
|
sEnabled = newVal;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static int clearStored()
|
interface ResultProcs {
|
||||||
|
void onDumped( File db );
|
||||||
|
void onCleared( int nCleared );
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void clearStored( ResultProcs procs )
|
||||||
{
|
{
|
||||||
int result = 0;
|
int result = 0;
|
||||||
LogDBHelper helper = initDB();
|
LogDBHelper helper = initDB();
|
||||||
if ( null != helper ) {
|
if ( null != helper ) {
|
||||||
result = helper.clear();
|
helper.clear( procs );
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static File dumpStored()
|
public static void dumpStored( ResultProcs procs )
|
||||||
{
|
{
|
||||||
File result = null;
|
File result = null;
|
||||||
LogDBHelper helper = initDB();
|
LogDBHelper helper = initDB();
|
||||||
if ( null != helper ) {
|
if ( null != helper ) {
|
||||||
result = helper.dumpToFile();
|
helper.dumpToFile( procs );
|
||||||
}
|
}
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void enable( Context context )
|
public static void enable( Context context )
|
||||||
|
@ -251,62 +255,107 @@ public class Log {
|
||||||
int tid = Process.myTid();
|
int tid = Process.myTid();
|
||||||
int pid = Process.myPid();
|
int pid = Process.myPid();
|
||||||
|
|
||||||
ContentValues values = new ContentValues();
|
final ContentValues values = new ContentValues();
|
||||||
values.put( COL_ENTRY, msg );
|
values.put( COL_ENTRY, msg );
|
||||||
values.put( COL_THREAD, tid );
|
values.put( COL_THREAD, tid );
|
||||||
values.put( COL_PID, pid );
|
values.put( COL_PID, pid );
|
||||||
values.put( COL_TAG, tag );
|
values.put( COL_TAG, tag );
|
||||||
values.put( COL_LEVEL, level.ordinal() );
|
values.put( COL_LEVEL, level.ordinal() );
|
||||||
long res = getWritableDatabase().insert( LOGS_TABLE_NAME, null, values );
|
enqueue( new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
getWritableDatabase().insert( LOGS_TABLE_NAME, null, values );
|
||||||
|
}
|
||||||
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
File dumpToFile()
|
void dumpToFile(final ResultProcs procs)
|
||||||
{
|
{
|
||||||
File dir = Environment.getExternalStorageDirectory();
|
enqueue( new Runnable() {
|
||||||
dir = new File( dir, Environment.DIRECTORY_DOWNLOADS );
|
@Override
|
||||||
File db = new File( dir, LOGS_FILE_NAME );
|
public void run() {
|
||||||
|
File dir = Environment.getExternalStorageDirectory();
|
||||||
|
dir = new File( dir, Environment.DIRECTORY_DOWNLOADS );
|
||||||
|
File db;
|
||||||
|
for ( int ii = 1; ; ++ii ) {
|
||||||
|
db = new File( dir, String.format( LOGS_FILE_NAME_FMT, ii ) );
|
||||||
|
if ( !db.exists() ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
OutputStream os = new FileOutputStream( db );
|
OutputStream os = new FileOutputStream( db );
|
||||||
OutputStreamWriter osw = new OutputStreamWriter(os);
|
OutputStreamWriter osw = new OutputStreamWriter(os);
|
||||||
|
|
||||||
String[] columns = { COL_ENTRY, COL_TAG, COL_THREAD, COL_PID };
|
String[] columns = { COL_ENTRY, COL_TAG, COL_THREAD, COL_PID };
|
||||||
String selection = null;
|
String selection = null;
|
||||||
String orderBy = COL_ROWID;
|
String orderBy = COL_ROWID;
|
||||||
Cursor cursor = getReadableDatabase().query( LOGS_TABLE_NAME, columns,
|
Cursor cursor = getReadableDatabase().query( LOGS_TABLE_NAME, columns,
|
||||||
selection, null, null, null,
|
selection, null, null, null,
|
||||||
orderBy );
|
orderBy );
|
||||||
llog( "dumpToFile(): got %d results", cursor.getCount() );
|
llog( "dumpToFile(): got %d results", cursor.getCount() );
|
||||||
int indx0 = cursor.getColumnIndex( columns[0] );
|
int indx0 = cursor.getColumnIndex( columns[0] );
|
||||||
int indx1 = cursor.getColumnIndex( columns[1] );
|
int indx1 = cursor.getColumnIndex( columns[1] );
|
||||||
int indx2 = cursor.getColumnIndex( columns[2] );
|
int indx2 = cursor.getColumnIndex( columns[2] );
|
||||||
int indx3 = cursor.getColumnIndex( columns[3] );
|
int indx3 = cursor.getColumnIndex( columns[3] );
|
||||||
while ( cursor.moveToNext() ) {
|
while ( cursor.moveToNext() ) {
|
||||||
String data = cursor.getString(indx0);
|
String data = cursor.getString(indx0);
|
||||||
String tag = cursor.getString(indx1);
|
String tag = cursor.getString(indx1);
|
||||||
int tid = cursor.getInt(indx2);
|
int tid = cursor.getInt(indx2);
|
||||||
int pid = cursor.getInt(indx3);
|
int pid = cursor.getInt(indx3);
|
||||||
StringBuilder builder = new StringBuilder()
|
StringBuilder builder = new StringBuilder()
|
||||||
.append(String.format("% 5d % 5d", pid, tid)).append(":")
|
.append(String.format("% 5d % 5d", pid, tid)).append(":")
|
||||||
.append(tag).append(":")
|
.append(tag).append(":")
|
||||||
.append(data).append("\n")
|
.append(data).append("\n")
|
||||||
;
|
;
|
||||||
osw.write( builder.toString() );
|
osw.write( builder.toString() );
|
||||||
}
|
}
|
||||||
osw.close();
|
osw.close();
|
||||||
} catch ( IOException ioe ) {
|
} catch ( IOException ioe ) {
|
||||||
llog( "dumpToFile(): ioe: %s", ioe );
|
llog( "dumpToFile(): ioe: %s", ioe );
|
||||||
db = null;
|
db = null;
|
||||||
}
|
}
|
||||||
return db;
|
if ( null != db ) {
|
||||||
|
procs.onDumped(db);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return the number of rows
|
// Return the number of rows
|
||||||
int clear()
|
void clear( final ResultProcs procs )
|
||||||
{
|
{
|
||||||
int result = getWritableDatabase()
|
enqueue( new Runnable() {
|
||||||
.delete( LOGS_TABLE_NAME, "1", null );
|
@Override
|
||||||
return result;
|
public void run() {
|
||||||
|
int result = getWritableDatabase()
|
||||||
|
.delete( LOGS_TABLE_NAME, "1", null );
|
||||||
|
procs.onCleared( result );
|
||||||
|
}
|
||||||
|
} );
|
||||||
|
}
|
||||||
|
|
||||||
|
private LinkedBlockingQueue<Runnable> mQueue;
|
||||||
|
private void enqueue( Runnable runnable )
|
||||||
|
{
|
||||||
|
if ( null == mQueue ) {
|
||||||
|
mQueue = new LinkedBlockingQueue<>();
|
||||||
|
new Thread( new Runnable() {
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
for ( ; ; ) {
|
||||||
|
try {
|
||||||
|
mQueue.take().run();
|
||||||
|
} catch ( InterruptedException ie ) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}).start();
|
||||||
|
}
|
||||||
|
|
||||||
|
mQueue.add( runnable );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue