From 2304a0a5bb11303ab6596c8222da59788c87c155 Mon Sep 17 00:00:00 2001 From: hyung-hwan Date: Mon, 18 Sep 2017 04:06:58 +0000 Subject: [PATCH] enhanced the logging functions to support priority masking and added/changed helper functions to convert betwee numeric values and string values --- qse/include/qse/si/log.h | 35 +++++++++++++--- qse/lib/si/log.c | 88 +++++++++++++++++++++++----------------- qse/samples/si/log01.c | 4 +- 3 files changed, 82 insertions(+), 45 deletions(-) diff --git a/qse/include/qse/si/log.h b/qse/include/qse/si/log.h index 7f6d4b74..62b7ce1c 100644 --- a/qse/include/qse/si/log.h +++ b/qse/include/qse/si/log.h @@ -265,6 +265,18 @@ QSE_EXPORT void qse_log_close ( qse_log_t* log ); +QSE_EXPORT int qse_log_init ( + qse_log_t* log, + qse_mmgr_t* mmgr, + const qse_char_t* ident, + int potflags, + const qse_log_target_t* target +); + +QSE_EXPORT void qse_log_fini ( + qse_log_t* log +); + QSE_EXPORT void qse_log_setident ( qse_log_t* log, const qse_char_t* ident @@ -332,10 +344,22 @@ QSE_EXPORT void qse_log_reportv ( va_list ap ); -QSE_EXPORT qse_size_t qse_get_log_priority_name ( - int pri, - qse_char_t* buf, - qse_size_t len /* length of the buffer, QSE_LOG_PRIORITY_LEN_MAX + 1 is enough */ +/** + * The qse_get_log_priority_name() function returns the name of + * the first priority bit set. + */ +QSE_EXPORT const qse_char_t* qse_get_log_priority_name ( + int pri +); + +/** + * The qse_make_log_priority_name() function composes a priority name + * string representing all priority bits set */ +QSE_EXPORT qse_size_t qse_make_log_priority_name ( + int pri, + const qse_char_t* delim, + qse_char_t* buf, + qse_size_t len /* length of the buffer, QSE_LOG_PRIORITY_LEN_MAX + 1 is enough */ ); /** @@ -343,7 +367,8 @@ QSE_EXPORT qse_size_t qse_get_log_priority_name ( * 0 if \a name is invalid or empty. */ QSE_EXPORT int qse_get_log_priority_by_name ( - const qse_char_t* name + const qse_char_t* name, + const qse_char_t* delim ); QSE_EXPORT int qse_get_log_facility_by_name ( diff --git a/qse/lib/si/log.c b/qse/lib/si/log.c index ea70861a..ec9486c6 100644 --- a/qse/lib/si/log.c +++ b/qse/lib/si/log.c @@ -196,6 +196,30 @@ static QSE_INLINE int get_active_priority_bits (int flags) return priority_bits; } +qse_log_t* qse_log_open (qse_mmgr_t* mmgr, qse_size_t xtnsize, const qse_char_t* ident, int potflags, const qse_log_target_t* target) +{ + qse_log_t* log; + + log = (qse_log_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_log_t) + xtnsize); + if (log) + { + if (qse_log_init (log, mmgr, ident, potflags, target) <= -1) + { + QSE_MMGR_FREE (mmgr, log); + return QSE_NULL; + } + else QSE_MEMSET (QSE_XTN(log), 0, xtnsize); + } + + return log; +} + +void qse_log_close (qse_log_t* log) +{ + qse_log_fini (log); + QSE_MMGR_FREE (log->mmgr, log); +} + int qse_log_init (qse_log_t* log, qse_mmgr_t* mmgr, const qse_char_t* ident, int potflags, const qse_log_target_t* target) { QSE_MEMSET (log, 0, QSE_SIZEOF(*log)); @@ -276,29 +300,6 @@ void qse_log_fini (qse_log_t* log) qse_mtx_fini (&log->mtx); } -qse_log_t* qse_log_open (qse_mmgr_t* mmgr, qse_size_t xtnsize, const qse_char_t* ident, int potflags, const qse_log_target_t* target) -{ - qse_log_t* log; - - log = (qse_log_t*) QSE_MMGR_ALLOC (mmgr, QSE_SIZEOF(qse_log_t) + xtnsize); - if (log) - { - if (qse_log_init (log, mmgr, ident, potflags, target) <= -1) - { - QSE_MMGR_FREE (mmgr, log); - return QSE_NULL; - } - else QSE_MEMSET (QSE_XTN(log), 0, xtnsize); - } - - return log; -} - -void qse_log_close (qse_log_t* log) -{ - qse_log_fini (log); - QSE_MMGR_FREE (log->mmgr, log); -} void qse_log_setident (qse_log_t* log, const qse_char_t* ident) { @@ -349,20 +350,18 @@ void qse_log_settarget (qse_log_t* log, int flags, const qse_log_target_t* targe } log->flags &= (QSE_LOG_MASK_PRIORITY | QSE_LOG_MASK_OPTION); /* preserve the priority and the options */ - if (flags & QSE_LOG_FILE) - { - qse_strxcpy (log->t.file.path, QSE_COUNTOF(log->t.file.path), target->file); - log->flags |= QSE_LOG_FILE; - } + if (flags & QSE_LOG_FILE) log->flags |= QSE_LOG_FILE; + + /* If you just want to set the target file path without enable QSE_LOG_FILE, + * just set target->file without QSE_LOG_FILE in the flags. + * later, you can call this function with QSE_LOG_FILE set but with target->file or QSE_NULL */ + if (target->file) qse_strxcpy (log->t.file.path, QSE_COUNTOF(log->t.file.path), target->file); if (flags & QSE_LOG_CONSOLE) log->flags |= QSE_LOG_CONSOLE; if (flags & QSE_LOG_SYSLOG) log->flags |= QSE_LOG_SYSLOG; - if (flags & QSE_LOG_SYSLOG_REMOTE) - { - log->t.syslog_remote.addr = target->syslog_remote; - log->flags |= QSE_LOG_SYSLOG_REMOTE; - } + if (flags & QSE_LOG_SYSLOG_REMOTE) log->flags |= QSE_LOG_SYSLOG_REMOTE; + if (qse_skadfamily(&target->syslog_remote) != -1) log->t.syslog_remote.addr = target->syslog_remote; } int qse_log_gettarget (qse_log_t* log, qse_log_target_t* target) @@ -375,7 +374,6 @@ int qse_log_gettarget (qse_log_t* log, qse_log_target_t* target) return log->flags & QSE_LOG_MASK_TARGET; } - void qse_log_setoption (qse_log_t* log, int option) { log->flags = (log->flags & (QSE_LOG_MASK_TARGET | QSE_LOG_MASK_PRIORITY)) | (option & QSE_LOG_MASK_OPTION); @@ -682,7 +680,21 @@ static const qse_char_t* __priority_names[] = QSE_T("debug") }; -qse_size_t qse_get_log_priority_name (int pri, qse_char_t* buf, qse_size_t len) +const qse_char_t* qse_get_log_priority_name (int pri) +{ + if (pri & QSE_LOG_PANIC) return __priority_names[0]; + if (pri & QSE_LOG_ALERT) return __priority_names[1]; + if (pri & QSE_LOG_CRITICAL) return __priority_names[2]; + if (pri & QSE_LOG_ERROR) return __priority_names[3]; + if (pri & QSE_LOG_WARNING) return __priority_names[4]; + if (pri & QSE_LOG_NOTICE) return __priority_names[5]; + if (pri & QSE_LOG_INFO) return __priority_names[6]; + if (pri & QSE_LOG_DEBUG) return __priority_names[7]; + + return QSE_NULL; +} + +qse_size_t qse_make_log_priority_name (int pri, const qse_char_t* delim, qse_char_t* buf, qse_size_t len) { qse_size_t tlen, xlen, rem, i; @@ -697,7 +709,7 @@ qse_size_t qse_get_log_priority_name (int pri, qse_char_t* buf, qse_size_t len) xlen = (tlen <= 0)? qse_strxcpy (&buf[tlen], rem, __priority_names[i]): - qse_strxjoin (&buf[tlen], rem, QSE_T("|"), __priority_names[i], QSE_NULL); + qse_strxjoin (&buf[tlen], rem, delim, __priority_names[i], QSE_NULL); rem -= xlen; tlen += xlen; @@ -708,7 +720,7 @@ qse_size_t qse_get_log_priority_name (int pri, qse_char_t* buf, qse_size_t len) return tlen; } -int qse_get_log_priority_by_name (const qse_char_t* name) +int qse_get_log_priority_by_name (const qse_char_t* name, const qse_char_t* delim) { qse_size_t i; qse_cstr_t tok; @@ -718,7 +730,7 @@ int qse_get_log_priority_by_name (const qse_char_t* name) ptr = name; while (ptr) { - ptr = qse_strtok (ptr, QSE_T("|"), &tok); + ptr = qse_strtok (ptr, delim, &tok); if (tok.ptr) { for (i = 0; i < QSE_COUNTOF(__priority_names); i++) diff --git a/qse/samples/si/log01.c b/qse/samples/si/log01.c index ce786413..8b42c101 100644 --- a/qse/samples/si/log01.c +++ b/qse/samples/si/log01.c @@ -26,14 +26,14 @@ static void t1_sub001 (qse_log_t* log) for (i = 0; i < QSE_COUNTOF(xtab); i++) { - len = qse_get_log_priority_name (xtab[i].pri, buf, QSE_COUNTOF(buf)); + len = qse_make_log_priority_name (xtab[i].pri, QSE_T("|"), buf, QSE_COUNTOF(buf)); QSE_LOG3 (log, QSE_NULL, QSE_LOG_INFO, QSE_T("%x => %s [%d]"), xtab[i].pri, buf, len); } for (i = 0; i < QSE_COUNTOF(xtab); i++) { int pri; - if ((pri = qse_get_log_priority_by_name(xtab[i].name)) == xtab[i].pri) + if ((pri = qse_get_log_priority_by_name(xtab[i].name, QSE_T("|"))) == xtab[i].pri) { QSE_LOG2 (log, QSE_NULL, QSE_LOG_INFO, QSE_T("SUCCESS: %s => %x"), xtab[i].name, pri); }