Skip to content

Commit

Permalink
logging local time for windows platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
sjlombardo committed Jan 17, 2022
1 parent 06dce3f commit 9bbd4e6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/crypto.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ int sqlcipher_find_db_index(sqlite3 *db, const char *zDb);
int sqlcipher_codec_ctx_integrity_check(codec_ctx *, Parse *, char *);

int sqlcipher_set_log(const char *destination);
int sqlcipher_set_log_level(unsigned int level);
void sqlcipher_set_log_level(unsigned int level);
void sqlcipher_log(unsigned int tag, const char *message, ...);

#define SQLCIPHER_LOG_NONE 0x00
Expand Down
28 changes: 22 additions & 6 deletions src/crypto_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ static sqlcipher_provider *default_provider = NULL;
static sqlite3_mutex* sqlcipher_static_mutex[SQLCIPHER_MUTEX_COUNT];
static volatile FILE* sqlcipher_log_file = NULL;
static volatile int sqlcipher_log_logcat = 0;
static volatile int sqlcipher_log_level = SQLCIPHER_LOG_NONE;
static volatile unsigned int sqlcipher_log_level = SQLCIPHER_LOG_NONE;

sqlite3_mutex* sqlcipher_mutex(int mutex) {
if(mutex < 0 || mutex >= SQLCIPHER_MUTEX_COUNT) return NULL;
Expand Down Expand Up @@ -1706,6 +1706,9 @@ const char* sqlcipher_codec_get_provider_version(codec_ctx *ctx) {
}

#ifndef SQLCIPHER_OMIT_LOG
/* constants from https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/misc/gettimeofday.c */
#define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */
#define HECTONANOSEC_PER_SEC 10000000ull
void sqlcipher_log(unsigned int level, const char *message, ...) {
va_list params;
va_start(params, message);
Expand All @@ -1724,14 +1727,27 @@ void sqlcipher_log(unsigned int level, const char *message, ...) {
goto end;
}
if(sqlcipher_log_file != NULL){
char buffer[20];
char buffer[256];
struct tm tt;
struct timeval tv;
int ms;
time_t sec;
#ifdef _WIN32
SYSTEMTIME st;
FILETIME ft;
GetSystemTime(&st);
SystemTimeToFileTime(&st, &ft);
sec = (time_t) ((*((sqlite_int64*)&ft) - FILETIME_1970) / HECTONANOSEC_PER_SEC);
ms = st.wMilliseconds;
localtime_s(&tt, &sec);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
sec = tv.tv_sec;
ms = tv.tv_usec/1000.0;
localtime_r(&tv.tv_sec, &tt);
strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", &tt);
localtime_r(&sec, &tt);
#endif
sqlcipher_memset(buffer, 0, 256);
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tt);
fprintf((FILE*)sqlcipher_log_file, "%s.%03d: ", buffer, ms);
vfprintf((FILE*)sqlcipher_log_file, message, params);
fprintf((FILE*)sqlcipher_log_file, "\n");
Expand All @@ -1746,7 +1762,7 @@ void sqlcipher_log(unsigned int level, const char *message, ...) {
}
#endif

int sqlcipher_set_log_level(unsigned int level) {
void sqlcipher_set_log_level(unsigned int level) {
sqlcipher_log_level = level;
}

Expand Down

0 comments on commit 9bbd4e6

Please sign in to comment.