Skip to content

Commit 9bbd4e6

Browse files
committed
logging local time for windows platforms
1 parent 06dce3f commit 9bbd4e6

2 files changed

Lines changed: 23 additions & 7 deletions

File tree

src/crypto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ 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_log(const char *destination);
307-
int sqlcipher_set_log_level(unsigned int level);
307+
void sqlcipher_set_log_level(unsigned int level);
308308
void sqlcipher_log(unsigned int tag, const char *message, ...);
309309

310310
#define SQLCIPHER_LOG_NONE 0x00

src/crypto_impl.c

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ static sqlcipher_provider *default_provider = NULL;
9696
static sqlite3_mutex* sqlcipher_static_mutex[SQLCIPHER_MUTEX_COUNT];
9797
static volatile FILE* sqlcipher_log_file = NULL;
9898
static volatile int sqlcipher_log_logcat = 0;
99-
static volatile int sqlcipher_log_level = SQLCIPHER_LOG_NONE;
99+
static volatile unsigned int sqlcipher_log_level = SQLCIPHER_LOG_NONE;
100100

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

17081708
#ifndef SQLCIPHER_OMIT_LOG
1709+
/* constants from https://github.com/Alexpux/mingw-w64/blob/master/mingw-w64-crt/misc/gettimeofday.c */
1710+
#define FILETIME_1970 116444736000000000ull /* seconds between 1/1/1601 and 1/1/1970 */
1711+
#define HECTONANOSEC_PER_SEC 10000000ull
17091712
void sqlcipher_log(unsigned int level, const char *message, ...) {
17101713
va_list params;
17111714
va_start(params, message);
@@ -1724,14 +1727,27 @@ void sqlcipher_log(unsigned int level, const char *message, ...) {
17241727
goto end;
17251728
}
17261729
if(sqlcipher_log_file != NULL){
1727-
char buffer[20];
1730+
char buffer[256];
17281731
struct tm tt;
1729-
struct timeval tv;
17301732
int ms;
1733+
time_t sec;
1734+
#ifdef _WIN32
1735+
SYSTEMTIME st;
1736+
FILETIME ft;
1737+
GetSystemTime(&st);
1738+
SystemTimeToFileTime(&st, &ft);
1739+
sec = (time_t) ((*((sqlite_int64*)&ft) - FILETIME_1970) / HECTONANOSEC_PER_SEC);
1740+
ms = st.wMilliseconds;
1741+
localtime_s(&tt, &sec);
1742+
#else
1743+
struct timeval tv;
17311744
gettimeofday(&tv, NULL);
1745+
sec = tv.tv_sec;
17321746
ms = tv.tv_usec/1000.0;
1733-
localtime_r(&tv.tv_sec, &tt);
1734-
strftime(buffer, 20, "%Y-%m-%d %H:%M:%S", &tt);
1747+
localtime_r(&sec, &tt);
1748+
#endif
1749+
sqlcipher_memset(buffer, 0, 256);
1750+
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &tt);
17351751
fprintf((FILE*)sqlcipher_log_file, "%s.%03d: ", buffer, ms);
17361752
vfprintf((FILE*)sqlcipher_log_file, message, params);
17371753
fprintf((FILE*)sqlcipher_log_file, "\n");
@@ -1746,7 +1762,7 @@ void sqlcipher_log(unsigned int level, const char *message, ...) {
17461762
}
17471763
#endif
17481764

1749-
int sqlcipher_set_log_level(unsigned int level) {
1765+
void sqlcipher_set_log_level(unsigned int level) {
17501766
sqlcipher_log_level = level;
17511767
}
17521768

0 commit comments

Comments
 (0)