8
8
package io .liteglue ;
9
9
10
10
import android .content .Context ;
11
- import android .util .Log ;
12
11
12
+ import com .facebook .common .logging .FLog ;
13
13
import com .facebook .react .bridge .Callback ;
14
14
import com .facebook .react .bridge .ReactApplicationContext ;
15
15
import com .facebook .react .bridge .ReactContextBaseJavaModule ;
37
37
38
38
public class SQLitePlugin extends ReactContextBaseJavaModule {
39
39
40
+ public static final String TAG = SQLitePlugin .class .getSimpleName ();
41
+
40
42
/**
41
43
* Multiple database runner map (static).
42
44
* 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 {
49
51
*/
50
52
static SQLiteConnector connector = new SQLiteConnector ();
51
53
52
- static String LOG_TAG = SQLitePlugin .class .getSimpleName ();
53
54
protected ExecutorService threadPool ;
54
55
private Context context ;
55
56
@@ -174,15 +175,15 @@ protected boolean execute(String actionAsString, JSONArray args, CallbackContext
174
175
action = Action .valueOf (actionAsString );
175
176
} catch (IllegalArgumentException e ) {
176
177
// shouldn't ever happen
177
- Log .e (LOG_TAG , "unexpected error" , e );
178
+ FLog .e (TAG , "unexpected error" , e );
178
179
throw (e );
179
180
}
180
181
181
182
try {
182
183
return executeAndPossiblyThrow (action , args , cbc );
183
184
} catch (JSONException e ) {
184
185
// TODO: signal JSON problem to JS
185
- Log .e (LOG_TAG , "unexpected error" , e );
186
+ FLog .e (TAG , "unexpected error" , e );
186
187
throw (e );
187
188
}
188
189
}
@@ -268,7 +269,7 @@ private boolean executeAndPossiblyThrow(Action action, JSONArray args, CallbackC
268
269
try {
269
270
r .q .put (q );
270
271
} catch (Exception e ) {
271
- Log .e (LOG_TAG , "couldn't add to queue" , e );
272
+ FLog .e (TAG , "couldn't add to queue" , e );
272
273
cbc .error ("couldn't add to queue" );
273
274
}
274
275
} else {
@@ -295,7 +296,7 @@ public void onDestroy() {
295
296
// stop the db runner thread:
296
297
r .q .put (new DBQuery ());
297
298
} catch (Exception e ) {
298
- Log .e (LOG_TAG , "couldn't stop db thread" , e );
299
+ FLog .e (TAG , "couldn't stop db thread" , e );
299
300
}
300
301
dbrmap .remove (dbname );
301
302
}
@@ -335,20 +336,20 @@ private SQLiteAndroidDatabase openDatabase(String dbname, String assetFilePath,
335
336
if (assetFilePath .compareTo ("1" ) == 0 ) {
336
337
assetFilePath = "www/" + dbname ;
337
338
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 );
339
340
} else if (assetFilePath .charAt (0 ) == '~' ) {
340
341
assetFilePath = assetFilePath .startsWith ("~/" ) ? assetFilePath .substring (2 ) : assetFilePath .substring (1 );
341
342
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 );
343
344
} else {
344
345
File filesDir = this .getContext ().getFilesDir ();
345
346
assetFilePath = assetFilePath .startsWith ("/" ) ? assetFilePath .substring (1 ) : assetFilePath ;
346
347
File assetFile = new File (filesDir , assetFilePath );
347
348
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 ());
349
350
if (openFlags == SQLiteOpenFlags .READONLY ) {
350
351
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." );
352
353
}
353
354
}
354
355
}
@@ -358,7 +359,7 @@ private SQLiteAndroidDatabase openDatabase(String dbname, String assetFilePath,
358
359
dbfile = this .getContext ().getDatabasePath (dbname );
359
360
360
361
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" );
362
363
this .createFromAssets (dbname , dbfile , in );
363
364
}
364
365
@@ -403,7 +404,7 @@ public void closeAllOpenDatabases() {
403
404
// stop the db runner thread:
404
405
r .q .put (new DBQuery ());
405
406
} 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 );
407
408
}
408
409
dbrmap .remove (dbname );
409
410
}
@@ -434,9 +435,9 @@ private void createFromAssets(String myDBName, File dbfile, InputStream assetFil
434
435
while ((len = assetFileInputStream .read (buf )) > 0 )
435
436
out .write (buf , 0 , len );
436
437
437
- Log .v ("info" , "Copied pre-populated DB content to: " + newDbFile .getAbsolutePath ());
438
+ FLog .v (TAG , "Copied pre-populated DB content to: " + newDbFile .getAbsolutePath ());
438
439
} 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 );
440
441
} finally {
441
442
if (out != null ) {
442
443
try {
@@ -461,7 +462,7 @@ private void closeDatabase(String dbname, CallbackContext cbc) {
461
462
if (cbc != null ) {
462
463
cbc .error ("couldn't close database" + e );
463
464
}
464
- Log .e (LOG_TAG , "couldn't close database" , e );
465
+ FLog .e (TAG , "couldn't close database" , e );
465
466
}
466
467
} else {
467
468
if (cbc != null ) {
@@ -521,7 +522,7 @@ private void deleteDatabase(String dbname, CallbackContext cbc) {
521
522
if (cbc != null ) {
522
523
cbc .error ("couldn't close database" + e );
523
524
}
524
- Log .e (LOG_TAG , "couldn't close database" , e );
525
+ FLog .e (TAG , "couldn't close database" , e );
525
526
}
526
527
} else {
527
528
boolean deleteResult = this .deleteDatabaseNow (dbname );
@@ -546,7 +547,7 @@ private boolean deleteDatabaseNow(String dbname) {
546
547
try {
547
548
return this .getContext ().deleteDatabase (dbfile .getAbsolutePath ());
548
549
} catch (Exception e ) {
549
- Log .e (LOG_TAG , "couldn't delete database" , e );
550
+ FLog .e (TAG , "couldn't delete database" , e );
550
551
return false ;
551
552
}
552
553
}
@@ -585,7 +586,7 @@ void closeDatabaseNow() {
585
586
if (mydb != null )
586
587
mydb .dispose ();
587
588
} catch (Exception e ) {
588
- Log .e (LOG_TAG , "couldn't close database, ignoring" , e );
589
+ FLog .e (TAG , "couldn't close database, ignoring" , e );
589
590
}
590
591
}
591
592
@@ -638,9 +639,8 @@ void executeSqlBatch( String[] queryarr, JSONArray[] jsonparams,
638
639
}
639
640
}
640
641
} catch (Exception ex ) {
641
- ex .printStackTrace ();
642
642
errorMessage = ex .getMessage ();
643
- Log . v ( "executeSqlBatch" , "SQLitePlugin.executeSql[Batch](): Error=" + errorMessage );
643
+ FLog . e ( TAG , "SQLitePlugin.executeSql[Batch]() failed" , ex );
644
644
}
645
645
646
646
try {
@@ -664,9 +664,7 @@ void executeSqlBatch( String[] queryarr, JSONArray[] jsonparams,
664
664
batchResults .put (r );
665
665
}
666
666
} 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 );
670
668
}
671
669
}
672
670
@@ -704,11 +702,7 @@ else if (p instanceof Number)
704
702
705
703
hasRows = myStatement .step ();
706
704
} 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 );
712
706
throw ex ;
713
707
}
714
708
@@ -749,14 +743,14 @@ else if (p instanceof Number)
749
743
rowsArrayResult .put (row );
750
744
751
745
} catch (JSONException e ) {
752
- e . printStackTrace ( );
746
+ FLog . w ( SQLitePlugin . TAG , e . getMessage (), e );
753
747
}
754
748
} while (myStatement .step ());
755
749
756
750
try {
757
751
rowsResult .put ("rows" , rowsArrayResult );
758
752
} catch (JSONException e ) {
759
- e . printStackTrace ( );
753
+ FLog . e ( TAG , e . getMessage (), e );
760
754
}
761
755
}
762
756
} finally {
@@ -791,14 +785,14 @@ private class DBRunner implements Runnable {
791
785
}
792
786
793
787
} catch (JSONException ex ){
794
- Log . v ( LOG_TAG ,"Error retrieving assetFilename from options:" , ex );
788
+ FLog . e ( TAG ,"Error retrieving assetFilename from options." , ex );
795
789
}
796
790
this .openFlags = openFlags ;
797
791
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)" ));
799
793
this .bugWorkaround = this .oldImpl && options .has ("androidBugWorkaround" );
800
794
if (this .bugWorkaround )
801
- Log . v ( LOG_TAG , "Android db closing/locking workaround applied" );
795
+ FLog . i ( TAG , "Android db closing/locking workaround applied" );
802
796
803
797
this .q = new LinkedBlockingQueue <>();
804
798
this .openCbc = cbc ;
@@ -808,7 +802,7 @@ public void run() {
808
802
try {
809
803
this .mydb = openDatabase (dbname , this .assetFilename , this .openFlags , this .openCbc , this .oldImpl );
810
804
} catch (Exception e ) {
811
- Log .e (LOG_TAG , "unexpected error, stopping db thread" , e );
805
+ FLog .e (TAG , "unexpected error, stopping db thread" , e );
812
806
dbrmap .remove (dbname );
813
807
return ;
814
808
}
@@ -828,7 +822,7 @@ public void run() {
828
822
dbq = q .take ();
829
823
}
830
824
} catch (Exception e ) {
831
- Log .e (LOG_TAG , "unexpected error" , e );
825
+ FLog .e (TAG , "unexpected error" , e );
832
826
}
833
827
834
828
if (dbq != null && dbq .close ) {
@@ -848,12 +842,12 @@ public void run() {
848
842
dbq .cbc .error ("couldn't delete database" );
849
843
}
850
844
} catch (Exception e ) {
851
- Log .e (LOG_TAG , "couldn't delete database" , e );
845
+ FLog .e (TAG , "couldn't delete database" , e );
852
846
dbq .cbc .error ("couldn't delete database: " + e );
853
847
}
854
848
}
855
849
} catch (Exception e ) {
856
- Log .e (LOG_TAG , "couldn't close database" , e );
850
+ FLog .e (TAG , "couldn't close database" , e );
857
851
if (dbq .cbc != null ) {
858
852
dbq .cbc .error ("couldn't close database: " + e );
859
853
}
0 commit comments