88package io .liteglue ;
99
1010import android .content .Context ;
11- import android .util .Log ;
1211
12+ import com .facebook .common .logging .FLog ;
1313import com .facebook .react .bridge .Callback ;
1414import com .facebook .react .bridge .ReactApplicationContext ;
1515import com .facebook .react .bridge .ReactContextBaseJavaModule ;
3737
3838public 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