add trace log filter and tag feature via PRAGMA cipher_trace_filter

This commit is contained in:
Stephen Lombardo 2021-12-17 12:42:05 -05:00
parent 74cabf6325
commit d4811ffd69
3 changed files with 40 additions and 5 deletions

View File

@ -142,7 +142,7 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
}
} else
if( sqlite3StrICmp(zLeft,"cipher_test")==0 ){
char *flags = sqlite3_mprintf("%i", sqlcipher_get_test_flags());
char *flags = sqlite3_mprintf("%u", sqlcipher_get_test_flags());
codec_vdbe_return_string(pParse, "cipher_test", flags, P4_DYNAMIC);
}else
if( sqlite3StrICmp(zLeft,"cipher_test_rand")==0 ){
@ -682,6 +682,17 @@ int sqlcipher_codec_pragma(sqlite3* db, int iDb, Parse *pParse, const char *zLef
sqlcipher_codec_ctx_integrity_check(ctx, pParse, "cipher_integrity_check");
}
} else
if( sqlite3StrICmp(zLeft, "cipher_trace_filter")==0 && zRight){
unsigned int filter = 0;
printf("%s\n",zRight);
if(sqlite3_strlike("%CORE%", zRight, '\'')==0) filter |= SQLCIPHER_TRACE_CORE;
if(sqlite3_strlike("%MEMORY%", zRight, '\'')==0) filter |= SQLCIPHER_TRACE_MEMORY;
if(sqlite3_strlike("%MUTEX%", zRight, '\'')==0) filter |= SQLCIPHER_TRACE_MUTEX;
if(sqlite3_strlike("%PROVIDER%", zRight, '\'')==0) filter |= SQLCIPHER_TRACE_PROVIDER;
if(sqlite3_strlike("%ALL%", zRight, '\'')==0) filter |= SQLCIPHER_TRACE_ALL;
sqlcipher_set_trace_filter(filter);
codec_vdbe_return_string(pParse, "cipher_trace_filter", sqlite3_mprintf("%u", filter), P4_DYNAMIC);
} else
if( sqlite3StrICmp(zLeft, "cipher_trace")== 0 && zRight ){
char *profile_status = sqlite3_mprintf("%d", sqlcipher_set_trace(zRight));
codec_vdbe_return_string(pParse, "cipher_trace", profile_status, P4_DYNAMIC);

View File

@ -304,7 +304,14 @@ int sqlcipher_find_db_index(sqlite3 *db, const char *zDb);
int sqlcipher_codec_ctx_integrity_check(codec_ctx *, Parse *, char *);
int sqlcipher_set_trace(const char *destination);
void sqlcipher_trace(const char *message, ...);
int sqlcipher_set_trace_filter(unsigned int filter);
void sqlcipher_trace(unsigned int tag, const char *message, ...);
#define SQLCIPHER_TRACE_CORE 0x01
#define SQLCIPHER_TRACE_PROVIDER 0x02
#define SQLCIPHER_TRACE_MEMORY 0x04
#define SQLCIPHER_TRACE_MUTEX 0x08
#define SQLCIPHER_TRACE_ALL 0xffffffff
#ifdef CODEC_DEBUG
#ifdef __ANDROID__
@ -316,20 +323,28 @@ void sqlcipher_trace(const char *message, ...);
#ifdef SQLCIPHER_OMIT_TRACE
#define CODEC_TRACE(...)
#else
#define CODEC_TRACE(...) {sqlcipher_trace(__VA_ARGS__);};
#define CODEC_TRACE(...) {sqlcipher_trace(SQLCIPHER_TRACE_CORE, __VA_ARGS__);};
#endif
#endif
#ifdef CODEC_DEBUG_MUTEX
#define CODEC_TRACE_MUTEX(...) CODEC_TRACE(__VA_ARGS__)
#else
#ifdef SQLCIPHER_OMIT_TRACE
#define CODEC_TRACE_MUTEX(...)
#else
#define CODEC_TRACE_MUTEX(...) {sqlcipher_trace(SQLCIPHER_TRACE_MUTEX, __VA_ARGS__);};
#endif
#endif
#ifdef CODEC_DEBUG_MEMORY
#define CODEC_TRACE_MEMORY(...) CODEC_TRACE(__VA_ARGS__)
#else
#ifdef SQLCIPHER_OMIT_TRACE
#define CODEC_TRACE_MEMORY(...)
#else
#define CODEC_TRACE_MEMORY(...) {sqlcipher_trace(SQLCIPHER_TRACE_MEMORY, __VA_ARGS__);};
#endif
#endif
#ifdef CODEC_DEBUG_PAGEDATA

View File

@ -90,6 +90,7 @@ static sqlcipher_provider *default_provider = NULL;
static sqlite3_mutex* sqlcipher_static_mutex[SQLCIPHER_MUTEX_COUNT];
static volatile FILE* sqlcipher_trace_file = NULL;
static volatile int sqlcipher_trace_logcat = 0;
static volatile int sqlcipher_trace_filter = 0;
sqlite3_mutex* sqlcipher_mutex(int mutex) {
if(mutex < 0 || mutex >= SQLCIPHER_MUTEX_COUNT) return NULL;
@ -1641,8 +1642,12 @@ const char* sqlcipher_codec_get_provider_version(codec_ctx *ctx) {
}
#ifndef SQLCIPHER_OMIT_TRACE
void sqlcipher_trace(const char *message, ...) {
void sqlcipher_trace(unsigned int tag, const char *message, ...) {
va_list params;
if((sqlcipher_trace_filter & tag) == 0 || (sqlcipher_trace_logcat == 0 && sqlcipher_trace_file == NULL)) {
/* no log target or tag not in included filters */
return;
}
va_start(params, message);
if(sqlcipher_trace_file != NULL){
vfprintf((FILE*)sqlcipher_trace_file, message, params);
@ -1656,6 +1661,10 @@ void sqlcipher_trace(const char *message, ...) {
}
#endif
int sqlcipher_set_trace_filter(unsigned int filter) {
sqlcipher_trace_filter = filter;
}
int sqlcipher_set_trace(const char *destination){
#ifdef SQLCIPHER_OMIT_TRACE
return SQLITE_ERROR;
@ -1681,7 +1690,7 @@ int sqlcipher_set_trace(const char *destination){
if((sqlcipher_trace_file = fopen(destination, "a")) == 0) return SQLITE_ERROR;
#endif
}
sqlcipher_trace("sqlcipher_set_trace: set trace to %s\n", destination);
sqlcipher_trace(SQLCIPHER_TRACE_CORE, "sqlcipher_set_trace: set trace to %s\n", destination);
return SQLITE_OK;
#endif
}