Skip to content

Production level logging for the plugin. #137

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import android.database.sqlite.SQLiteStatement;

import android.util.Base64;
import android.util.Log;

import com.facebook.common.logging.FLog;

import java.io.File;
import java.lang.IllegalArgumentException;
Expand All @@ -33,8 +34,7 @@
/**
* Android Database helper class
*/
class SQLiteAndroidDatabase
{
class SQLiteAndroidDatabase {
private static final Pattern FIRST_WORD = Pattern.compile("^\\s*(\\S+)",
Pattern.CASE_INSENSITIVE);

Expand Down Expand Up @@ -145,9 +145,8 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
needRawQuery = false;
} catch (SQLiteException ex) {
// Indicate problem & stop this query:
ex.printStackTrace();
errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLiteStatement.executeUpdateDelete(): Error=" + errorMessage);
FLog.e(SQLitePlugin.TAG, "SQLiteStatement.executeUpdateDelete() failed", ex);
needRawQuery = false;
} catch (Exception ex) {
// Assuming SDK_INT was lying & method not found:
Expand Down Expand Up @@ -188,9 +187,8 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
} catch (SQLiteException ex) {
// report error result with the error message
// could be constraint violation or some other error
ex.printStackTrace();
errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLiteDatabase.executeInsert(): Error=" + errorMessage);
FLog.e(SQLitePlugin.TAG, "SQLiteDatabase.executeInsert() failed", ex);
}
}

Expand All @@ -202,9 +200,8 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
queryResult = new JSONObject();
queryResult.put("rowsAffected", 0);
} catch (SQLiteException ex) {
ex.printStackTrace();
errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLiteDatabase.beginTransaction(): Error=" + errorMessage);
FLog.e(SQLitePlugin.TAG, "SQLiteDatabase.beginTransaction() failed", ex);
}
}

Expand All @@ -217,9 +214,8 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
queryResult = new JSONObject();
queryResult.put("rowsAffected", 0);
} catch (SQLiteException ex) {
ex.printStackTrace();
errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLiteDatabase.setTransactionSuccessful/endTransaction(): Error=" + errorMessage);
FLog.e(SQLitePlugin.TAG, "SQLiteDatabase.setTransactionSuccessful/endTransaction() failed", ex);
}
}

Expand All @@ -231,9 +227,8 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
queryResult = new JSONObject();
queryResult.put("rowsAffected", 0);
} catch (SQLiteException ex) {
ex.printStackTrace();
errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLiteDatabase.endTransaction(): Error=" + errorMessage);
FLog.e(SQLitePlugin.TAG, "SQLiteDatabase.endTransaction() failed", ex);
}
}

Expand All @@ -246,9 +241,8 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
}
}
} catch (Exception ex) {
ex.printStackTrace();
errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLiteAndroidDatabase.executeSql[Batch](): Error=" + errorMessage);
FLog.e(SQLitePlugin.TAG, "SQLiteAndroidDatabase.executeSql[Batch]() failed", ex);
}

try {
Expand All @@ -272,9 +266,7 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
batchResults.put(r);
}
} catch (JSONException ex) {
ex.printStackTrace();
Log.v("executeSqlBatch", "SQLiteAndroidDatabase.executeSql[Batch](): Error=" + ex.getMessage());
// TODO what to do?
FLog.e(SQLitePlugin.TAG, "SQLiteAndroidDatabase.executeSql[Batch]() failed", ex);
}
}

Expand Down Expand Up @@ -332,7 +324,7 @@ private int countRowsAffectedCompat(QueryType queryType, String query, JSONArray
return (int)statement.simpleQueryForLong();
} catch (Exception e) {
// assume we couldn't count for whatever reason, keep going
Log.e(SQLiteAndroidDatabase.class.getSimpleName(), "uncaught", e);
FLog.e(SQLitePlugin.TAG, "update query failed", e);
}
}
} else { // delete
Expand All @@ -347,7 +339,7 @@ private int countRowsAffectedCompat(QueryType queryType, String query, JSONArray
return (int)statement.simpleQueryForLong();
} catch (Exception e) {
// assume we couldn't count for whatever reason, keep going
Log.e(SQLiteAndroidDatabase.class.getSimpleName(), "uncaught", e);
FLog.e(SQLitePlugin.TAG, "delete table query failed", e);

}
}
Expand Down Expand Up @@ -395,9 +387,7 @@ private JSONObject executeSqlStatementQuery(SQLiteDatabase mydb,

cur = mydb.rawQuery(query, params);
} catch (Exception ex) {
ex.printStackTrace();
String errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLiteAndroidDatabase.executeSql[Batch](): Error=" + errorMessage);
FLog.e(SQLitePlugin.TAG, "SQLiteAndroidDatabase.executeSql[Batch]() failed", ex);
throw ex;
}

Expand Down Expand Up @@ -430,14 +420,14 @@ private JSONObject executeSqlStatementQuery(SQLiteDatabase mydb,
rowsArrayResult.put(row);

} catch (JSONException e) {
e.printStackTrace();
FLog.w(SQLitePlugin.TAG, e.getMessage(), e);
}
} while (cur.moveToNext());

try {
rowsResult.put("rows", rowsArrayResult);
} catch (JSONException e) {
e.printStackTrace();
FLog.e(SQLitePlugin.TAG, e.getMessage(), e);
}
}

Expand Down
68 changes: 31 additions & 37 deletions src/android-native/src/main/java/io/liteglue/SQLitePlugin.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
package io.liteglue;

import android.content.Context;
import android.util.Log;

import com.facebook.common.logging.FLog;
import com.facebook.react.bridge.Callback;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
Expand Down Expand Up @@ -37,6 +37,8 @@

public class SQLitePlugin extends ReactContextBaseJavaModule {

public static final String TAG = SQLitePlugin.class.getSimpleName();

/**
* Multiple database runner map (static).
* NOTE: no public static accessor to db (runner) map since it would not work with db threading.
Expand All @@ -49,7 +51,6 @@ public class SQLitePlugin extends ReactContextBaseJavaModule {
*/
static SQLiteConnector connector = new SQLiteConnector();

static String LOG_TAG = SQLitePlugin.class.getSimpleName();
protected ExecutorService threadPool;
private Context context;

Expand Down Expand Up @@ -174,15 +175,15 @@ protected boolean execute(String actionAsString, JSONArray args, CallbackContext
action = Action.valueOf(actionAsString);
} catch (IllegalArgumentException e) {
// shouldn't ever happen
Log.e(LOG_TAG, "unexpected error", e);
FLog.e(TAG, "unexpected error", e);
throw(e);
}

try {
return executeAndPossiblyThrow(action, args, cbc);
} catch (JSONException e) {
// TODO: signal JSON problem to JS
Log.e(LOG_TAG, "unexpected error", e);
FLog.e(TAG, "unexpected error", e);
throw(e);
}
}
Expand Down Expand Up @@ -268,7 +269,7 @@ private boolean executeAndPossiblyThrow(Action action, JSONArray args, CallbackC
try {
r.q.put(q);
} catch(Exception e) {
Log.e(LOG_TAG, "couldn't add to queue", e);
FLog.e(TAG, "couldn't add to queue", e);
cbc.error("couldn't add to queue");
}
} else {
Expand All @@ -295,7 +296,7 @@ public void onDestroy() {
// stop the db runner thread:
r.q.put(new DBQuery());
} catch(Exception e) {
Log.e(LOG_TAG, "couldn't stop db thread", e);
FLog.e(TAG, "couldn't stop db thread", e);
}
dbrmap.remove(dbname);
}
Expand Down Expand Up @@ -335,20 +336,20 @@ private SQLiteAndroidDatabase openDatabase(String dbname, String assetFilePath,
if (assetFilePath.compareTo("1") == 0) {
assetFilePath = "www/" + dbname;
in = this.getContext().getAssets().open(assetFilePath);
Log.v("info", "Located pre-populated DB asset in app bundle www subdirectory: " + assetFilePath);
FLog.v(TAG, "Located pre-populated DB asset in app bundle www subdirectory: " + assetFilePath);
} else if (assetFilePath.charAt(0) == '~') {
assetFilePath = assetFilePath.startsWith("~/") ? assetFilePath.substring(2) : assetFilePath.substring(1);
in = this.getContext().getAssets().open(assetFilePath);
Log.v("info", "Located pre-populated DB asset in app bundle subdirectory: " + assetFilePath);
FLog.v(TAG, "Located pre-populated DB asset in app bundle subdirectory: " + assetFilePath);
} else {
File filesDir = this.getContext().getFilesDir();
assetFilePath = assetFilePath.startsWith("/") ? assetFilePath.substring(1) : assetFilePath;
File assetFile = new File(filesDir, assetFilePath);
in = new FileInputStream(assetFile);
Log.v("info", "Located pre-populated DB asset in Files subdirectory: " + assetFile.getCanonicalPath());
FLog.v(TAG, "Located pre-populated DB asset in Files subdirectory: " + assetFile.getCanonicalPath());
if (openFlags == SQLiteOpenFlags.READONLY) {
dbfile = assetFile;
Log.v("info", "Detected read-only mode request for external asset.");
FLog.v(TAG, "Detected read-only mode request for external asset.");
}
}
}
Expand All @@ -358,7 +359,7 @@ private SQLiteAndroidDatabase openDatabase(String dbname, String assetFilePath,
dbfile = this.getContext().getDatabasePath(dbname);

if (!dbfile.exists() && in != null) {
Log.v("info", "Copying pre-populated db asset to destination");
FLog.v(TAG, "Copying pre-populated db asset to destination");
this.createFromAssets(dbname, dbfile, in);
}

Expand Down Expand Up @@ -403,7 +404,7 @@ public void closeAllOpenDatabases() {
// stop the db runner thread:
r.q.put(new DBQuery());
} catch(Exception ex) {
Log.e(LOG_TAG, "couldn't stop db thread for db: " + dbname,ex);
FLog.e(TAG, "couldn't stop db thread for db: " + dbname, ex);
}
dbrmap.remove(dbname);
}
Expand Down Expand Up @@ -434,9 +435,9 @@ private void createFromAssets(String myDBName, File dbfile, InputStream assetFil
while ((len = assetFileInputStream.read(buf)) > 0)
out.write(buf, 0, len);

Log.v("info", "Copied pre-populated DB content to: " + newDbFile.getAbsolutePath());
FLog.v(TAG, "Copied pre-populated DB content to: " + newDbFile.getAbsolutePath());
} catch (IOException ex) {
Log.v("createFromAssets", "No pre-populated DB found, error=" + ex.getMessage());
FLog.e(TAG, "No pre-populated DB found.", ex);
} finally {
if (out != null) {
try {
Expand All @@ -461,7 +462,7 @@ private void closeDatabase(String dbname, CallbackContext cbc) {
if (cbc != null) {
cbc.error("couldn't close database" + e);
}
Log.e(LOG_TAG, "couldn't close database", e);
FLog.e(TAG, "couldn't close database", e);
}
} else {
if (cbc != null) {
Expand Down Expand Up @@ -521,7 +522,7 @@ private void deleteDatabase(String dbname, CallbackContext cbc) {
if (cbc != null) {
cbc.error("couldn't close database" + e);
}
Log.e(LOG_TAG, "couldn't close database", e);
FLog.e(TAG, "couldn't close database", e);
}
} else {
boolean deleteResult = this.deleteDatabaseNow(dbname);
Expand All @@ -546,7 +547,7 @@ private boolean deleteDatabaseNow(String dbname) {
try {
return this.getContext().deleteDatabase(dbfile.getAbsolutePath());
} catch (Exception e) {
Log.e(LOG_TAG, "couldn't delete database", e);
FLog.e(TAG, "couldn't delete database", e);
return false;
}
}
Expand Down Expand Up @@ -585,7 +586,7 @@ void closeDatabaseNow() {
if (mydb != null)
mydb.dispose();
} catch (Exception e) {
Log.e(LOG_TAG, "couldn't close database, ignoring", e);
FLog.e(TAG, "couldn't close database, ignoring", e);
}
}

Expand Down Expand Up @@ -638,9 +639,8 @@ void executeSqlBatch( String[] queryarr, JSONArray[] jsonparams,
}
}
} catch (Exception ex) {
ex.printStackTrace();
errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLitePlugin.executeSql[Batch](): Error=" + errorMessage);
FLog.e(TAG, "SQLitePlugin.executeSql[Batch]() failed", ex);
}

try {
Expand All @@ -664,9 +664,7 @@ void executeSqlBatch( String[] queryarr, JSONArray[] jsonparams,
batchResults.put(r);
}
} catch (JSONException ex) {
ex.printStackTrace();
Log.v("executeSqlBatch", "SQLitePlugin.executeSql[Batch](): Error=" + ex.getMessage());
// TODO what to do?
FLog.e(TAG, "SQLitePlugin.executeSql[Batch]() failed", ex);
}
}

Expand Down Expand Up @@ -704,11 +702,7 @@ else if (p instanceof Number)

hasRows = myStatement.step();
} catch (Exception ex) {
ex.printStackTrace();
String errorMessage = ex.getMessage();
Log.v("executeSqlBatch", "SQLitePlugin.executeSql[Batch](): Error=" + errorMessage);


FLog.e(TAG, "SQLitePlugin.executeSql[Batch]() failed", ex);
throw ex;
}

Expand Down Expand Up @@ -749,14 +743,14 @@ else if (p instanceof Number)
rowsArrayResult.put(row);

} catch (JSONException e) {
e.printStackTrace();
FLog.w(SQLitePlugin.TAG, e.getMessage(), e);
}
} while (myStatement.step());

try {
rowsResult.put("rows", rowsArrayResult);
} catch (JSONException e) {
e.printStackTrace();
FLog.e(TAG, e.getMessage(), e);
}
}
} finally {
Expand Down Expand Up @@ -791,14 +785,14 @@ private class DBRunner implements Runnable {
}

} catch (JSONException ex){
Log.v(LOG_TAG,"Error retrieving assetFilename from options:",ex);
FLog.e(TAG,"Error retrieving assetFilename from options.", ex);
}
this.openFlags = openFlags;
this.oldImpl = options.has("androidOldDatabaseImplementation");
Log.v(LOG_TAG, "Android db implementation: " + (oldImpl ? "OLD" : "sqlite4java (NDK)"));
FLog.v(TAG, "Android db implementation: " + (oldImpl ? "OLD" : "sqlite4java (NDK)"));
this.bugWorkaround = this.oldImpl && options.has("androidBugWorkaround");
if (this.bugWorkaround)
Log.v(LOG_TAG, "Android db closing/locking workaround applied");
FLog.i(TAG, "Android db closing/locking workaround applied");

this.q = new LinkedBlockingQueue<>();
this.openCbc = cbc;
Expand All @@ -808,7 +802,7 @@ public void run() {
try {
this.mydb = openDatabase(dbname, this.assetFilename, this.openFlags, this.openCbc, this.oldImpl);
} catch (Exception e) {
Log.e(LOG_TAG, "unexpected error, stopping db thread", e);
FLog.e(TAG, "unexpected error, stopping db thread", e);
dbrmap.remove(dbname);
return;
}
Expand All @@ -828,7 +822,7 @@ public void run() {
dbq = q.take();
}
} catch (Exception e) {
Log.e(LOG_TAG, "unexpected error", e);
FLog.e(TAG, "unexpected error", e);
}

if (dbq != null && dbq.close) {
Expand All @@ -848,12 +842,12 @@ public void run() {
dbq.cbc.error("couldn't delete database");
}
} catch (Exception e) {
Log.e(LOG_TAG, "couldn't delete database", e);
FLog.e(TAG, "couldn't delete database", e);
dbq.cbc.error("couldn't delete database: " + e);
}
}
} catch (Exception e) {
Log.e(LOG_TAG, "couldn't close database", e);
FLog.e(TAG, "couldn't close database", e);
if (dbq.cbc != null) {
dbq.cbc.error("couldn't close database: " + e);
}
Expand Down
Loading