Skip to content

Commit ffe6c58

Browse files
Replace android.util.Log with custom logger implementation, updated documentation.
1 parent 0c5204a commit ffe6c58

22 files changed

+260
-74
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@ db = Room.databaseBuilder(context, AppDatabase.class, databaseFile.getAbsolutePa
7676
.openHelperFactory(factory).build();
7777
```
7878
79+
### Logging
80+
81+
Logging may occur in 3 distinct areas within this library:
82+
83+
1. Within the Java client library
84+
2. Within the JNI interop layer
85+
3. Within SQLCipher core
86+
87+
##### Java Client Logging
88+
89+
By default, logging within the Java client library is routed to Logcat. If you wish to disable this logging entirely, you may utilize
90+
the [`NoopTarget`](sqlcipher/src/main/java/net/zetetic/database/NoopTarget.java) instead:
91+
92+
```java
93+
Logger.setTarget(new NoopTarget());
94+
```
95+
96+
You can instead provide a custom logging target by registering a different target that implements the [`LogTarget`](sqlcipher/src/main/java/net/zetetic/database/LogTarget.java) interface.
97+
98+
##### JNI Interop Layer
99+
100+
There are two different compile-specific options available to alter the logging output from the JNI layer. To remove `INFO`, `DEBUG`, and `VERBOSE` log messages from the JNI layer, include `-DNDEBUG` with CFLAGS; this will allow `WARN` and `ERROR` logs to output to logcat. Alternatively, to exclude all log output from JNI, build the library using `-DSQLCIPHER_OMIT_LOG`.
101+
102+
##### SQLCipher core
103+
104+
To manage the logging produced from SQLCipher core, please review the runtime configurations: [`PRAGMA cipher_log`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log),
105+
[`PRAGMA cipher_log_level`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log_level), and [`PRAGMA cipher_log_source`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log_source).
106+
79107
### Building
80108
81109
## Android NDK

README.md.template

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,34 @@ db = Room.databaseBuilder(context, AppDatabase.class, databaseFile.getAbsolutePa
7676
.openHelperFactory(factory).build();
7777
```
7878

79+
### Logging
80+
81+
Logging may occur in 3 distinct areas within this library:
82+
83+
1. Within the Java client library
84+
2. Within the JNI interop layer
85+
3. Within SQLCipher core
86+
87+
##### Java Client Logging
88+
89+
By default, logging within the Java client library is routed to Logcat. If you wish to disable this logging entirely, you may utilize
90+
the [`NoopTarget`](sqlcipher/src/main/java/net/zetetic/database/NoopTarget.java) instead:
91+
92+
```java
93+
Logger.setTarget(new NoopTarget());
94+
```
95+
96+
You can instead provide a custom logging target by registering a different target that implements the [`LogTarget`](sqlcipher/src/main/java/net/zetetic/database/LogTarget.java) interface.
97+
98+
##### JNI Interop Layer
99+
100+
There are two different compile-specific options available to alter the logging output from the JNI layer. To remove `INFO`, `DEBUG`, and `VERBOSE` log messages from the JNI layer, include `-DNDEBUG` with CFLAGS; this will allow `WARN` and `ERROR` logs to output to logcat. Alternatively, to exclude all log output from JNI, build the library using `-DSQLCIPHER_OMIT_LOG`.
101+
102+
##### SQLCipher core
103+
104+
To manage the logging produced from SQLCipher core, please review the runtime configurations: [`PRAGMA cipher_log`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log),
105+
[`PRAGMA cipher_log_level`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log_level), and [`PRAGMA cipher_log_source`](https://www.zetetic.net/sqlcipher/sqlcipher-api/#cipher_log_source).
106+
79107
### Building
80108

81109
## Android NDK

sqlcipher/src/main/java/net/zetetic/database/AbstractCursor.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828
import android.net.Uri;
2929
import android.os.Build;
3030
import android.os.Bundle;
31-
import android.util.Log;
3231

3332
import java.lang.ref.WeakReference;
3433

@@ -257,7 +256,7 @@ public int getColumnIndex(String columnName) {
257256
final int periodIndex = columnName.lastIndexOf('.');
258257
if (periodIndex != -1) {
259258
Exception e = new Exception();
260-
Log.e(TAG, "requesting column name with table name -- " + columnName, e);
259+
Logger.e(TAG, "requesting column name with table name -- " + columnName, e);
261260
columnName = columnName.substring(periodIndex + 1);
262261
}
263262

sqlcipher/src/main/java/net/zetetic/database/DatabaseUtils.java

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import android.os.Parcel;
3636
import android.os.ParcelFileDescriptor;
3737
import android.text.TextUtils;
38-
import android.util.Log;
3938

4039
import android.database.Cursor;
4140

@@ -114,14 +113,14 @@ public static final void writeExceptionToParcel(Parcel reply, Exception e) {
114113
logException = false;
115114
} else {
116115
reply.writeException(e);
117-
Log.e(TAG, "Writing exception to parcel", e);
116+
Logger.e(TAG, "Writing exception to parcel", e);
118117
return;
119118
}
120119
reply.writeInt(code);
121120
reply.writeString(e.getMessage());
122121

123122
if (logException) {
124-
Log.e(TAG, "Writing exception to parcel", e);
123+
Logger.e(TAG, "Writing exception to parcel", e);
125124
}
126125
}
127126

@@ -1080,7 +1079,7 @@ private void buildSQL() throws SQLException {
10801079
sb.append(sbv);
10811080

10821081
mInsertSQL = sb.toString();
1083-
if (DEBUG) Log.v(TAG, "insert statement is " + mInsertSQL);
1082+
if (DEBUG) Logger.v(TAG, "insert statement is " + mInsertSQL);
10841083
}
10851084

10861085
private SQLiteStatement getStatement(boolean allowReplace) throws SQLException {
@@ -1124,21 +1123,21 @@ private long insertInternal(ContentValues values, boolean allowReplace) {
11241123
try {
11251124
SQLiteStatement stmt = getStatement(allowReplace);
11261125
stmt.clearBindings();
1127-
if (DEBUG) Log.v(TAG, "--- inserting in table " + mTableName);
1126+
if (DEBUG) Logger.v(TAG, "--- inserting in table " + mTableName);
11281127
for (Map.Entry<String, Object> e: values.valueSet()) {
11291128
final String key = e.getKey();
11301129
int i = getColumnIndex(key);
11311130
DatabaseUtils.bindObjectToProgram(stmt, i, e.getValue());
11321131
if (DEBUG) {
1133-
Log.v(TAG, "binding " + e.getValue() + " to column " +
1132+
Logger.v(TAG, "binding " + e.getValue() + " to column " +
11341133
i + " (" + key + ")");
11351134
}
11361135
}
11371136
long result = stmt.executeInsert();
11381137
mDb.setTransactionSuccessful();
11391138
return result;
11401139
} catch (SQLException e) {
1141-
Log.e(TAG, "Error inserting " + values + " into table " + mTableName, e);
1140+
Logger.e(TAG, "Error inserting " + values + " into table " + mTableName, e);
11421141
return -1;
11431142
} finally {
11441143
mDb.endTransaction();
@@ -1278,10 +1277,10 @@ public long execute() {
12781277
+ "execute");
12791278
}
12801279
try {
1281-
if (DEBUG) Log.v(TAG, "--- doing insert or replace in table " + mTableName);
1280+
if (DEBUG) Logger.v(TAG, "--- doing insert or replace in table " + mTableName);
12821281
return mPreparedStatement.executeInsert();
12831282
} catch (SQLException e) {
1284-
Log.e(TAG, "Error executing InsertHelper with table " + mTableName, e);
1283+
Logger.e(TAG, "Error executing InsertHelper with table " + mTableName, e);
12851284
return -1;
12861285
} finally {
12871286
// you can only call this once per prepare

sqlcipher/src/main/java/net/zetetic/database/DefaultDatabaseErrorHandler.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import net.zetetic.database.sqlcipher.SQLiteDatabase;
2727

2828
import android.database.sqlite.SQLiteException;
29-
import android.util.Log;
3029
import android.util.Pair;
3130

3231
/**
@@ -57,7 +56,7 @@ public final class DefaultDatabaseErrorHandler implements DatabaseErrorHandler {
5756
* is detected.
5857
*/
5958
public void onCorruption(SQLiteDatabase dbObj, SQLiteException exception) {
60-
Log.e(TAG, "Corruption reported by sqlite on database: " + dbObj.getPath());
59+
Logger.e(TAG, "Corruption reported by sqlite on database: " + dbObj.getPath());
6160

6261
// If this is a SEE build, do not delete any database files.
6362
// It may be that the user has specified an incorrect password.
@@ -107,12 +106,12 @@ private void deleteDatabaseFile(String fileName) {
107106
if (fileName.equalsIgnoreCase(":memory:") || fileName.trim().length() == 0) {
108107
return;
109108
}
110-
Log.e(TAG, "deleting the database file: " + fileName);
109+
Logger.e(TAG, "deleting the database file: " + fileName);
111110
try {
112111
SQLiteDatabase.deleteDatabase(new File(fileName));
113112
} catch (Exception e) {
114113
/* print warning and ignore exception */
115-
Log.w(TAG, "delete failed: " + e.getMessage());
114+
Logger.w(TAG, "delete failed: " + e.getMessage());
116115
}
117116
}
118117
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package net.zetetic.database;
2+
3+
public interface LogTarget {
4+
boolean isLoggable (String tag, int priority);
5+
void log(int priority, String tag, String message, Throwable throwable);
6+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package net.zetetic.database;
2+
3+
import android.util.Log;
4+
5+
public class LogcatTarget implements LogTarget {
6+
7+
@Override
8+
public boolean isLoggable(String tag, int priority) {
9+
return Log.isLoggable(tag, priority);
10+
}
11+
12+
public void log(int priority, String tag, String message, Throwable throwable){
13+
switch (priority){
14+
case Logger.VERBOSE -> Log.v(tag, message, throwable);
15+
case Logger.DEBUG -> Log.d(tag, message, throwable);
16+
case Logger.INFO -> Log.i(tag, message, throwable);
17+
case Logger.WARN -> Log.w(tag, message, throwable);
18+
case Logger.ERROR -> Log.e(tag, message, throwable);
19+
case Logger.ASSERT -> Log.wtf(tag, message, throwable);
20+
}
21+
}
22+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package net.zetetic.database;
2+
3+
public class Logger {
4+
5+
static {
6+
setTarget(new LogcatTarget());
7+
}
8+
9+
public static final int VERBOSE = 2;
10+
public static final int DEBUG = 3;
11+
public static final int INFO = 4;
12+
public static final int WARN = 5;
13+
public static final int ERROR = 6;
14+
public static final int ASSERT = 7;
15+
16+
private static LogTarget target;
17+
18+
public static void setTarget(LogTarget target){
19+
Logger.target = target;
20+
}
21+
22+
private static LogTarget getTarget(){
23+
if(Logger.target == null){
24+
setTarget(new NoopTarget());
25+
}
26+
return Logger.target;
27+
}
28+
29+
public static boolean isLoggable(String tag, int priority){
30+
return getTarget().isLoggable(tag, priority);
31+
}
32+
33+
public static void i(String tag, String message) {
34+
getTarget().log(Logger.INFO, tag, message, null);
35+
}
36+
37+
public static void i(String tag, String message, Throwable throwable) {
38+
getTarget().log(Logger.INFO, tag, message, throwable);
39+
}
40+
41+
public static void d(String tag, String message) {
42+
getTarget().log(Logger.DEBUG, tag, message, null);
43+
}
44+
45+
public static void d(String tag, String message, Throwable throwable) {
46+
getTarget().log(Logger.DEBUG, tag, message, throwable);
47+
}
48+
49+
public static void e(String tag, String message) {
50+
getTarget().log(Logger.ERROR, tag, message, null);
51+
}
52+
53+
public static void e(String tag, String message, Throwable throwable) {
54+
getTarget().log(Logger.ERROR, tag, message, throwable);
55+
}
56+
57+
public static void v(String tag, String message) {
58+
getTarget().log(Logger.VERBOSE, tag, message, null);
59+
}
60+
61+
public static void v(String tag, String message, Throwable throwable) {
62+
getTarget().log(Logger.VERBOSE, tag, message, throwable);
63+
}
64+
65+
public static void w(String tag, String message) {
66+
getTarget().log(Logger.WARN, tag, message, null);
67+
}
68+
69+
public static void w(String tag, String message, Throwable throwable) {
70+
getTarget().log(Logger.WARN, tag, message, throwable);
71+
}
72+
73+
public static void wtf(String tag, String message) {
74+
getTarget().log(Logger.ASSERT, tag, message, null);
75+
}
76+
77+
public static void wtf(String tag, String message, Throwable throwable) {
78+
getTarget().log(Logger.ASSERT, tag, message, throwable);
79+
}
80+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package net.zetetic.database;
2+
3+
public class NoopTarget implements LogTarget {
4+
5+
@Override
6+
public boolean isLoggable(String tag, int priority) {
7+
return false;
8+
}
9+
10+
@Override
11+
public void log(int priority, String tag, String message, Throwable throwable) {}
12+
}

sqlcipher/src/main/java/net/zetetic/database/sqlcipher/CloseGuard.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
*/
2020

2121
package net.zetetic.database.sqlcipher;
22-
import android.util.Log;
22+
23+
import net.zetetic.database.Logger;
2324

2425
/**
2526
* CloseGuard is a mechanism for flagging implicit finalizer cleanup of
@@ -229,7 +230,7 @@ public static interface Reporter {
229230
*/
230231
private static final class DefaultReporter implements Reporter {
231232
@Override public void report (String message, Throwable allocationSite) {
232-
Log.w(message, allocationSite);
233+
android.util.Log.w(message, allocationSite);
233234
}
234235
}
235236
}

0 commit comments

Comments
 (0)