Skip to content

Commit e558bcd

Browse files
dryganetsandpor
authored andcommitted
Production level logging for the plugin. (andpor#137)
We print logs through Facebook logger only. This way application could customize if logs need to be saved on disk or have some custom processing. Instead of printing stacktraces we let FLog class to figure out how to handle it. Log levels are changed in the way that important logs wouldn't be filtered out by default log policy in release.
1 parent 4b22adf commit e558bcd

File tree

6 files changed

+137
-157
lines changed

6 files changed

+137
-157
lines changed

src/android-native/src/main/java/io/liteglue/SQLiteAndroidDatabase.java

Lines changed: 15 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import android.database.sqlite.SQLiteStatement;
1818

1919
import android.util.Base64;
20-
import android.util.Log;
20+
21+
import com.facebook.common.logging.FLog;
2122

2223
import java.io.File;
2324
import java.lang.IllegalArgumentException;
@@ -33,8 +34,7 @@
3334
/**
3435
* Android Database helper class
3536
*/
36-
class SQLiteAndroidDatabase
37-
{
37+
class SQLiteAndroidDatabase {
3838
private static final Pattern FIRST_WORD = Pattern.compile("^\\s*(\\S+)",
3939
Pattern.CASE_INSENSITIVE);
4040

@@ -145,9 +145,8 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
145145
needRawQuery = false;
146146
} catch (SQLiteException ex) {
147147
// Indicate problem & stop this query:
148-
ex.printStackTrace();
149148
errorMessage = ex.getMessage();
150-
Log.v("executeSqlBatch", "SQLiteStatement.executeUpdateDelete(): Error=" + errorMessage);
149+
FLog.e(SQLitePlugin.TAG, "SQLiteStatement.executeUpdateDelete() failed", ex);
151150
needRawQuery = false;
152151
} catch (Exception ex) {
153152
// Assuming SDK_INT was lying & method not found:
@@ -188,9 +187,8 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
188187
} catch (SQLiteException ex) {
189188
// report error result with the error message
190189
// could be constraint violation or some other error
191-
ex.printStackTrace();
192190
errorMessage = ex.getMessage();
193-
Log.v("executeSqlBatch", "SQLiteDatabase.executeInsert(): Error=" + errorMessage);
191+
FLog.e(SQLitePlugin.TAG, "SQLiteDatabase.executeInsert() failed", ex);
194192
}
195193
}
196194

@@ -202,9 +200,8 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
202200
queryResult = new JSONObject();
203201
queryResult.put("rowsAffected", 0);
204202
} catch (SQLiteException ex) {
205-
ex.printStackTrace();
206203
errorMessage = ex.getMessage();
207-
Log.v("executeSqlBatch", "SQLiteDatabase.beginTransaction(): Error=" + errorMessage);
204+
FLog.e(SQLitePlugin.TAG, "SQLiteDatabase.beginTransaction() failed", ex);
208205
}
209206
}
210207

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

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

@@ -246,9 +241,8 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
246241
}
247242
}
248243
} catch (Exception ex) {
249-
ex.printStackTrace();
250244
errorMessage = ex.getMessage();
251-
Log.v("executeSqlBatch", "SQLiteAndroidDatabase.executeSql[Batch](): Error=" + errorMessage);
245+
FLog.e(SQLitePlugin.TAG, "SQLiteAndroidDatabase.executeSql[Batch]() failed", ex);
252246
}
253247

254248
try {
@@ -272,9 +266,7 @@ void executeSqlBatch(String[] queryarr, JSONArray[] jsonparams,
272266
batchResults.put(r);
273267
}
274268
} catch (JSONException ex) {
275-
ex.printStackTrace();
276-
Log.v("executeSqlBatch", "SQLiteAndroidDatabase.executeSql[Batch](): Error=" + ex.getMessage());
277-
// TODO what to do?
269+
FLog.e(SQLitePlugin.TAG, "SQLiteAndroidDatabase.executeSql[Batch]() failed", ex);
278270
}
279271
}
280272

@@ -332,7 +324,7 @@ private int countRowsAffectedCompat(QueryType queryType, String query, JSONArray
332324
return (int)statement.simpleQueryForLong();
333325
} catch (Exception e) {
334326
// assume we couldn't count for whatever reason, keep going
335-
Log.e(SQLiteAndroidDatabase.class.getSimpleName(), "uncaught", e);
327+
FLog.e(SQLitePlugin.TAG, "update query failed", e);
336328
}
337329
}
338330
} else { // delete
@@ -347,7 +339,7 @@ private int countRowsAffectedCompat(QueryType queryType, String query, JSONArray
347339
return (int)statement.simpleQueryForLong();
348340
} catch (Exception e) {
349341
// assume we couldn't count for whatever reason, keep going
350-
Log.e(SQLiteAndroidDatabase.class.getSimpleName(), "uncaught", e);
342+
FLog.e(SQLitePlugin.TAG, "delete table query failed", e);
351343

352344
}
353345
}
@@ -395,9 +387,7 @@ private JSONObject executeSqlStatementQuery(SQLiteDatabase mydb,
395387

396388
cur = mydb.rawQuery(query, params);
397389
} catch (Exception ex) {
398-
ex.printStackTrace();
399-
String errorMessage = ex.getMessage();
400-
Log.v("executeSqlBatch", "SQLiteAndroidDatabase.executeSql[Batch](): Error=" + errorMessage);
390+
FLog.e(SQLitePlugin.TAG, "SQLiteAndroidDatabase.executeSql[Batch]() failed", ex);
401391
throw ex;
402392
}
403393

@@ -430,14 +420,14 @@ private JSONObject executeSqlStatementQuery(SQLiteDatabase mydb,
430420
rowsArrayResult.put(row);
431421

432422
} catch (JSONException e) {
433-
e.printStackTrace();
423+
FLog.w(SQLitePlugin.TAG, e.getMessage(), e);
434424
}
435425
} while (cur.moveToNext());
436426

437427
try {
438428
rowsResult.put("rows", rowsArrayResult);
439429
} catch (JSONException e) {
440-
e.printStackTrace();
430+
FLog.e(SQLitePlugin.TAG, e.getMessage(), e);
441431
}
442432
}
443433

src/android-native/src/main/java/io/liteglue/SQLitePlugin.java

Lines changed: 31 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@
88
package io.liteglue;
99

1010
import android.content.Context;
11-
import android.util.Log;
1211

12+
import com.facebook.common.logging.FLog;
1313
import com.facebook.react.bridge.Callback;
1414
import com.facebook.react.bridge.ReactApplicationContext;
1515
import com.facebook.react.bridge.ReactContextBaseJavaModule;
@@ -37,6 +37,8 @@
3737

3838
public class SQLitePlugin extends ReactContextBaseJavaModule {
3939

40+
public static final String TAG = SQLitePlugin.class.getSimpleName();
41+
4042
/**
4143
* Multiple database runner map (static).
4244
* NOTE: no public static accessor to db (runner) map since it would not work with db threading.
@@ -49,7 +51,6 @@ public class SQLitePlugin extends ReactContextBaseJavaModule {
4951
*/
5052
static SQLiteConnector connector = new SQLiteConnector();
5153

52-
static String LOG_TAG = SQLitePlugin.class.getSimpleName();
5354
protected ExecutorService threadPool;
5455
private Context context;
5556

@@ -174,15 +175,15 @@ protected boolean execute(String actionAsString, JSONArray args, CallbackContext
174175
action = Action.valueOf(actionAsString);
175176
} catch (IllegalArgumentException e) {
176177
// shouldn't ever happen
177-
Log.e(LOG_TAG, "unexpected error", e);
178+
FLog.e(TAG, "unexpected error", e);
178179
throw(e);
179180
}
180181

181182
try {
182183
return executeAndPossiblyThrow(action, args, cbc);
183184
} catch (JSONException e) {
184185
// TODO: signal JSON problem to JS
185-
Log.e(LOG_TAG, "unexpected error", e);
186+
FLog.e(TAG, "unexpected error", e);
186187
throw(e);
187188
}
188189
}
@@ -268,7 +269,7 @@ private boolean executeAndPossiblyThrow(Action action, JSONArray args, CallbackC
268269
try {
269270
r.q.put(q);
270271
} catch(Exception e) {
271-
Log.e(LOG_TAG, "couldn't add to queue", e);
272+
FLog.e(TAG, "couldn't add to queue", e);
272273
cbc.error("couldn't add to queue");
273274
}
274275
} else {
@@ -295,7 +296,7 @@ public void onDestroy() {
295296
// stop the db runner thread:
296297
r.q.put(new DBQuery());
297298
} catch(Exception e) {
298-
Log.e(LOG_TAG, "couldn't stop db thread", e);
299+
FLog.e(TAG, "couldn't stop db thread", e);
299300
}
300301
dbrmap.remove(dbname);
301302
}
@@ -335,20 +336,20 @@ private SQLiteAndroidDatabase openDatabase(String dbname, String assetFilePath,
335336
if (assetFilePath.compareTo("1") == 0) {
336337
assetFilePath = "www/" + dbname;
337338
in = this.getContext().getAssets().open(assetFilePath);
338-
Log.v("info", "Located pre-populated DB asset in app bundle www subdirectory: " + assetFilePath);
339+
FLog.v(TAG, "Located pre-populated DB asset in app bundle www subdirectory: " + assetFilePath);
339340
} else if (assetFilePath.charAt(0) == '~') {
340341
assetFilePath = assetFilePath.startsWith("~/") ? assetFilePath.substring(2) : assetFilePath.substring(1);
341342
in = this.getContext().getAssets().open(assetFilePath);
342-
Log.v("info", "Located pre-populated DB asset in app bundle subdirectory: " + assetFilePath);
343+
FLog.v(TAG, "Located pre-populated DB asset in app bundle subdirectory: " + assetFilePath);
343344
} else {
344345
File filesDir = this.getContext().getFilesDir();
345346
assetFilePath = assetFilePath.startsWith("/") ? assetFilePath.substring(1) : assetFilePath;
346347
File assetFile = new File(filesDir, assetFilePath);
347348
in = new FileInputStream(assetFile);
348-
Log.v("info", "Located pre-populated DB asset in Files subdirectory: " + assetFile.getCanonicalPath());
349+
FLog.v(TAG, "Located pre-populated DB asset in Files subdirectory: " + assetFile.getCanonicalPath());
349350
if (openFlags == SQLiteOpenFlags.READONLY) {
350351
dbfile = assetFile;
351-
Log.v("info", "Detected read-only mode request for external asset.");
352+
FLog.v(TAG, "Detected read-only mode request for external asset.");
352353
}
353354
}
354355
}
@@ -358,7 +359,7 @@ private SQLiteAndroidDatabase openDatabase(String dbname, String assetFilePath,
358359
dbfile = this.getContext().getDatabasePath(dbname);
359360

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

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

437-
Log.v("info", "Copied pre-populated DB content to: " + newDbFile.getAbsolutePath());
438+
FLog.v(TAG, "Copied pre-populated DB content to: " + newDbFile.getAbsolutePath());
438439
} catch (IOException ex) {
439-
Log.v("createFromAssets", "No pre-populated DB found, error=" + ex.getMessage());
440+
FLog.e(TAG, "No pre-populated DB found.", ex);
440441
} finally {
441442
if (out != null) {
442443
try {
@@ -461,7 +462,7 @@ private void closeDatabase(String dbname, CallbackContext cbc) {
461462
if (cbc != null) {
462463
cbc.error("couldn't close database" + e);
463464
}
464-
Log.e(LOG_TAG, "couldn't close database", e);
465+
FLog.e(TAG, "couldn't close database", e);
465466
}
466467
} else {
467468
if (cbc != null) {
@@ -521,7 +522,7 @@ private void deleteDatabase(String dbname, CallbackContext cbc) {
521522
if (cbc != null) {
522523
cbc.error("couldn't close database" + e);
523524
}
524-
Log.e(LOG_TAG, "couldn't close database", e);
525+
FLog.e(TAG, "couldn't close database", e);
525526
}
526527
} else {
527528
boolean deleteResult = this.deleteDatabaseNow(dbname);
@@ -546,7 +547,7 @@ private boolean deleteDatabaseNow(String dbname) {
546547
try {
547548
return this.getContext().deleteDatabase(dbfile.getAbsolutePath());
548549
} catch (Exception e) {
549-
Log.e(LOG_TAG, "couldn't delete database", e);
550+
FLog.e(TAG, "couldn't delete database", e);
550551
return false;
551552
}
552553
}
@@ -585,7 +586,7 @@ void closeDatabaseNow() {
585586
if (mydb != null)
586587
mydb.dispose();
587588
} catch (Exception e) {
588-
Log.e(LOG_TAG, "couldn't close database, ignoring", e);
589+
FLog.e(TAG, "couldn't close database, ignoring", e);
589590
}
590591
}
591592

@@ -638,9 +639,8 @@ void executeSqlBatch( String[] queryarr, JSONArray[] jsonparams,
638639
}
639640
}
640641
} catch (Exception ex) {
641-
ex.printStackTrace();
642642
errorMessage = ex.getMessage();
643-
Log.v("executeSqlBatch", "SQLitePlugin.executeSql[Batch](): Error=" + errorMessage);
643+
FLog.e(TAG, "SQLitePlugin.executeSql[Batch]() failed", ex);
644644
}
645645

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

@@ -704,11 +702,7 @@ else if (p instanceof Number)
704702

705703
hasRows = myStatement.step();
706704
} catch (Exception ex) {
707-
ex.printStackTrace();
708-
String errorMessage = ex.getMessage();
709-
Log.v("executeSqlBatch", "SQLitePlugin.executeSql[Batch](): Error=" + errorMessage);
710-
711-
705+
FLog.e(TAG, "SQLitePlugin.executeSql[Batch]() failed", ex);
712706
throw ex;
713707
}
714708

@@ -749,14 +743,14 @@ else if (p instanceof Number)
749743
rowsArrayResult.put(row);
750744

751745
} catch (JSONException e) {
752-
e.printStackTrace();
746+
FLog.w(SQLitePlugin.TAG, e.getMessage(), e);
753747
}
754748
} while (myStatement.step());
755749

756750
try {
757751
rowsResult.put("rows", rowsArrayResult);
758752
} catch (JSONException e) {
759-
e.printStackTrace();
753+
FLog.e(TAG, e.getMessage(), e);
760754
}
761755
}
762756
} finally {
@@ -791,14 +785,14 @@ private class DBRunner implements Runnable {
791785
}
792786

793787
} catch (JSONException ex){
794-
Log.v(LOG_TAG,"Error retrieving assetFilename from options:",ex);
788+
FLog.e(TAG,"Error retrieving assetFilename from options.", ex);
795789
}
796790
this.openFlags = openFlags;
797791
this.oldImpl = options.has("androidOldDatabaseImplementation");
798-
Log.v(LOG_TAG, "Android db implementation: " + (oldImpl ? "OLD" : "sqlite4java (NDK)"));
792+
FLog.v(TAG, "Android db implementation: " + (oldImpl ? "OLD" : "sqlite4java (NDK)"));
799793
this.bugWorkaround = this.oldImpl && options.has("androidBugWorkaround");
800794
if (this.bugWorkaround)
801-
Log.v(LOG_TAG, "Android db closing/locking workaround applied");
795+
FLog.i(TAG, "Android db closing/locking workaround applied");
802796

803797
this.q = new LinkedBlockingQueue<>();
804798
this.openCbc = cbc;
@@ -808,7 +802,7 @@ public void run() {
808802
try {
809803
this.mydb = openDatabase(dbname, this.assetFilename, this.openFlags, this.openCbc, this.oldImpl);
810804
} catch (Exception e) {
811-
Log.e(LOG_TAG, "unexpected error, stopping db thread", e);
805+
FLog.e(TAG, "unexpected error, stopping db thread", e);
812806
dbrmap.remove(dbname);
813807
return;
814808
}
@@ -828,7 +822,7 @@ public void run() {
828822
dbq = q.take();
829823
}
830824
} catch (Exception e) {
831-
Log.e(LOG_TAG, "unexpected error", e);
825+
FLog.e(TAG, "unexpected error", e);
832826
}
833827

834828
if (dbq != null && dbq.close) {
@@ -848,12 +842,12 @@ public void run() {
848842
dbq.cbc.error("couldn't delete database");
849843
}
850844
} catch (Exception e) {
851-
Log.e(LOG_TAG, "couldn't delete database", e);
845+
FLog.e(TAG, "couldn't delete database", e);
852846
dbq.cbc.error("couldn't delete database: " + e);
853847
}
854848
}
855849
} catch (Exception e) {
856-
Log.e(LOG_TAG, "couldn't close database", e);
850+
FLog.e(TAG, "couldn't close database", e);
857851
if (dbq.cbc != null) {
858852
dbq.cbc.error("couldn't close database: " + e);
859853
}

0 commit comments

Comments
 (0)