Skip to content

Commit d4811ff

Browse files
committed
add trace log filter and tag feature via PRAGMA cipher_trace_filter
1 parent 74cabf6 commit d4811ff

File tree

3 files changed

+40
-5
lines changed

3 files changed

+40
-5
lines changed

src/crypto.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
142142
}
143143
} else
144144
if( sqlite3StrICmp(zLeft,"cipher_test")==0 ){
145-
char *flags = sqlite3_mprintf("%i", sqlcipher_get_test_flags());
145+
char *flags = sqlite3_mprintf("%u", sqlcipher_get_test_flags());
146146
codec_vdbe_return_string(pParse, "cipher_test", flags, P4_DYNAMIC);
147147
}else
148148
if( sqlite3StrICmp(zLeft,"cipher_test_rand")==0 ){
@@ -682,6 +682,17 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
682682
sqlcipher_codec_ctx_integrity_check(ctx, pParse, "cipher_integrity_check");
683683
}
684684
} else
685+
if( sqlite3StrICmp(zLeft, "cipher_trace_filter")==0 && zRight){
686+
unsigned int filter = 0;
687+
printf("%s\n",zRight);
688+
if(sqlite3_strlike("%CORE%", zRight, '\'')==0) filter |= SQLCIPHER_TRACE_CORE;
689+
if(sqlite3_strlike("%MEMORY%", zRight, '\'')==0) filter |= SQLCIPHER_TRACE_MEMORY;
690+
if(sqlite3_strlike("%MUTEX%", zRight, '\'')==0) filter |= SQLCIPHER_TRACE_MUTEX;
691+
if(sqlite3_strlike("%PROVIDER%", zRight, '\'')==0) filter |= SQLCIPHER_TRACE_PROVIDER;
692+
if(sqlite3_strlike("%ALL%", zRight, '\'')==0) filter |= SQLCIPHER_TRACE_ALL;
693+
sqlcipher_set_trace_filter(filter);
694+
codec_vdbe_return_string(pParse, "cipher_trace_filter", sqlite3_mprintf("%u", filter), P4_DYNAMIC);
695+
} else
685696
if( sqlite3StrICmp(zLeft, "cipher_trace")== 0 && zRight ){
686697
char *profile_status = sqlite3_mprintf("%d", sqlcipher_set_trace(zRight));
687698
codec_vdbe_return_string(pParse, "cipher_trace", profile_status, P4_DYNAMIC);

src/crypto.h

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,14 @@ int sqlcipher_find_db_index(sqlite3 *db, const char *zDb);
304304
int sqlcipher_codec_ctx_integrity_check(codec_ctx *, Parse *, char *);
305305

306306
int sqlcipher_set_trace(const char *destination);
307-
void sqlcipher_trace(const char *message, ...);
307+
int sqlcipher_set_trace_filter(unsigned int filter);
308+
void sqlcipher_trace(unsigned int tag, const char *message, ...);
309+
310+
#define SQLCIPHER_TRACE_CORE 0x01
311+
#define SQLCIPHER_TRACE_PROVIDER 0x02
312+
#define SQLCIPHER_TRACE_MEMORY 0x04
313+
#define SQLCIPHER_TRACE_MUTEX 0x08
314+
#define SQLCIPHER_TRACE_ALL 0xffffffff
308315

309316
#ifdef CODEC_DEBUG
310317
#ifdef __ANDROID__
@@ -316,20 +323,28 @@ void sqlcipher_trace(const char *message, ...);
316323
#ifdef SQLCIPHER_OMIT_TRACE
317324
#define CODEC_TRACE(...)
318325
#else
319-
#define CODEC_TRACE(...) {sqlcipher_trace(__VA_ARGS__);};
326+
#define CODEC_TRACE(...) {sqlcipher_trace(SQLCIPHER_TRACE_CORE, __VA_ARGS__);};
320327
#endif
321328
#endif
322329

323330
#ifdef CODEC_DEBUG_MUTEX
324331
#define CODEC_TRACE_MUTEX(...) CODEC_TRACE(__VA_ARGS__)
325332
#else
333+
#ifdef SQLCIPHER_OMIT_TRACE
326334
#define CODEC_TRACE_MUTEX(...)
335+
#else
336+
#define CODEC_TRACE_MUTEX(...) {sqlcipher_trace(SQLCIPHER_TRACE_MUTEX, __VA_ARGS__);};
337+
#endif
327338
#endif
328339

329340
#ifdef CODEC_DEBUG_MEMORY
330341
#define CODEC_TRACE_MEMORY(...) CODEC_TRACE(__VA_ARGS__)
331342
#else
343+
#ifdef SQLCIPHER_OMIT_TRACE
332344
#define CODEC_TRACE_MEMORY(...)
345+
#else
346+
#define CODEC_TRACE_MEMORY(...) {sqlcipher_trace(SQLCIPHER_TRACE_MEMORY, __VA_ARGS__);};
347+
#endif
333348
#endif
334349

335350
#ifdef CODEC_DEBUG_PAGEDATA

src/crypto_impl.c

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ static sqlcipher_provider *default_provider = NULL;
9090
static sqlite3_mutex* sqlcipher_static_mutex[SQLCIPHER_MUTEX_COUNT];
9191
static volatile FILE* sqlcipher_trace_file = NULL;
9292
static volatile int sqlcipher_trace_logcat = 0;
93+
static volatile int sqlcipher_trace_filter = 0;
9394

9495
sqlite3_mutex* sqlcipher_mutex(int mutex) {
9596
if(mutex < 0 || mutex >= SQLCIPHER_MUTEX_COUNT) return NULL;
@@ -1641,8 +1642,12 @@ const char* sqlcipher_codec_get_provider_version(codec_ctx *ctx) {
16411642
}
16421643

16431644
#ifndef SQLCIPHER_OMIT_TRACE
1644-
void sqlcipher_trace(const char *message, ...) {
1645+
void sqlcipher_trace(unsigned int tag, const char *message, ...) {
16451646
va_list params;
1647+
if((sqlcipher_trace_filter & tag) == 0 || (sqlcipher_trace_logcat == 0 && sqlcipher_trace_file == NULL)) {
1648+
/* no log target or tag not in included filters */
1649+
return;
1650+
}
16461651
va_start(params, message);
16471652
if(sqlcipher_trace_file != NULL){
16481653
vfprintf((FILE*)sqlcipher_trace_file, message, params);
@@ -1656,6 +1661,10 @@ void sqlcipher_trace(const char *message, ...) {
16561661
}
16571662
#endif
16581663

1664+
int sqlcipher_set_trace_filter(unsigned int filter) {
1665+
sqlcipher_trace_filter = filter;
1666+
}
1667+
16591668
int sqlcipher_set_trace(const char *destination){
16601669
#ifdef SQLCIPHER_OMIT_TRACE
16611670
return SQLITE_ERROR;
@@ -1681,7 +1690,7 @@ int sqlcipher_set_trace(const char *destination){
16811690
if((sqlcipher_trace_file = fopen(destination, "a")) == 0) return SQLITE_ERROR;
16821691
#endif
16831692
}
1684-
sqlcipher_trace("sqlcipher_set_trace: set trace to %s\n", destination);
1693+
sqlcipher_trace(SQLCIPHER_TRACE_CORE, "sqlcipher_set_trace: set trace to %s\n", destination);
16851694
return SQLITE_OK;
16861695
#endif
16871696
}

0 commit comments

Comments
 (0)