From d8226f269319d430fc38b0cef3cce78b2192d224 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20M=C3=A4rkl?= Date: Tue, 6 Aug 2019 20:47:23 +0200 Subject: [PATCH] Log Level and Timestamp to File --- gui/src/sessionlog.cpp | 18 +++++++++++------- lib/include/chiaki/log.h | 2 ++ lib/src/sessionlog.c | 31 ++++++++++++++++++++----------- 3 files changed, 33 insertions(+), 18 deletions(-) diff --git a/gui/src/sessionlog.cpp b/gui/src/sessionlog.cpp index a905a06..0842ef1 100644 --- a/gui/src/sessionlog.cpp +++ b/gui/src/sessionlog.cpp @@ -64,10 +64,14 @@ void SessionLog::Log(ChiakiLogLevel level, const char *msg) if(file) { + static const QString date_format = "yyyy-MM-dd HH:mm:ss:zzzzzz"; + QString str = QString("[%1] [%2] %3\n").arg( + QDateTime::currentDateTime().toString(date_format), + QString(chiaki_log_level_char(level)), + msg); + QMutexLocker lock(&file_mutex); - // TODO: add timestamp and level - file->write(msg); - file->write("\n"); + file->write(str.toLocal8Bit()); } } @@ -85,12 +89,12 @@ static void LogCb(ChiakiLogLevel level, const char *msg, void *user) #define KEEP_LOG_FILES_COUNT 5 -static const QString date_format = "yyyy-MM-dd_HH-mm-ss-zzzz"; -static const QString session_log_wildcard = "chiaki_session_*.log"; -static const QRegularExpression session_log_regex("chiaki_session_(.*).log"); - QString CreateLogFilename() { + static const QString date_format = "yyyy-MM-dd_HH-mm-ss-zzzzzz"; + static const QString session_log_wildcard = "chiaki_session_*.log"; + static const QRegularExpression session_log_regex("chiaki_session_(.*).log"); + auto base_dir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation); if(base_dir.isEmpty()) return QString(); diff --git a/lib/include/chiaki/log.h b/lib/include/chiaki/log.h index e15b339..f16b6ff 100644 --- a/lib/include/chiaki/log.h +++ b/lib/include/chiaki/log.h @@ -37,6 +37,8 @@ typedef enum { #define CHIAKI_LOG_ALL ((1 << 5) - 1) +CHIAKI_EXPORT char chiaki_log_level_char(ChiakiLogLevel level); + typedef void (*ChiakiLogCb)(ChiakiLogLevel level, const char *msg, void *user); typedef struct chiaki_log_t diff --git a/lib/src/sessionlog.c b/lib/src/sessionlog.c index 4752ce2..1055be1 100644 --- a/lib/src/sessionlog.c +++ b/lib/src/sessionlog.c @@ -20,6 +20,25 @@ #include #include +CHIAKI_EXPORT char chiaki_log_level_char(ChiakiLogLevel level) +{ + switch(level) + { + case CHIAKI_LOG_VERBOSE: + return 'V'; + case CHIAKI_LOG_DEBUG: + return 'D'; + case CHIAKI_LOG_INFO: + return 'I'; + case CHIAKI_LOG_WARNING: + return 'W'; + case CHIAKI_LOG_ERROR: + return 'E'; + default: + return '?'; + } +} + CHIAKI_EXPORT void chiaki_log_init(ChiakiLog *log, uint32_t level_mask, ChiakiLogCb cb, void *user) { log->level_mask = level_mask; @@ -31,30 +50,20 @@ CHIAKI_EXPORT void chiaki_log_cb_print(ChiakiLogLevel level, const char *msg, vo { (void)user; - char c; + char c = chiaki_log_level_char(level); const char *color = NULL; switch(level) { - case CHIAKI_LOG_VERBOSE: - c = 'V'; - break; case CHIAKI_LOG_DEBUG: - c = 'D'; color = "34"; break; - case CHIAKI_LOG_INFO: - c = 'I'; - break; case CHIAKI_LOG_WARNING: - c = 'W'; color = "33"; break; case CHIAKI_LOG_ERROR: - c = 'E'; color = "31"; break; default: - c = '?'; break; }