enhanced the logging functions to support priority masking and added/changed helper functions to convert betwee numeric values and string values

This commit is contained in:
hyung-hwan 2017-09-18 04:06:58 +00:00
parent c5ac9558ab
commit 2304a0a5bb
3 changed files with 82 additions and 45 deletions

View File

@ -265,6 +265,18 @@ QSE_EXPORT void qse_log_close (
qse_log_t* log 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_EXPORT void qse_log_setident (
qse_log_t* log, qse_log_t* log,
const qse_char_t* ident const qse_char_t* ident
@ -332,10 +344,22 @@ QSE_EXPORT void qse_log_reportv (
va_list ap va_list ap
); );
QSE_EXPORT qse_size_t qse_get_log_priority_name ( /**
int pri, * The qse_get_log_priority_name() function returns the name of
qse_char_t* buf, * the first priority bit set.
qse_size_t len /* length of the buffer, QSE_LOG_PRIORITY_LEN_MAX + 1 is enough */ */
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. * 0 if \a name is invalid or empty.
*/ */
QSE_EXPORT int qse_get_log_priority_by_name ( 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 ( QSE_EXPORT int qse_get_log_facility_by_name (

View File

@ -196,6 +196,30 @@ static QSE_INLINE int get_active_priority_bits (int flags)
return priority_bits; 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) 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)); 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_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) 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 */ log->flags &= (QSE_LOG_MASK_PRIORITY | QSE_LOG_MASK_OPTION); /* preserve the priority and the options */
if (flags & QSE_LOG_FILE) if (flags & QSE_LOG_FILE) log->flags |= QSE_LOG_FILE;
{
qse_strxcpy (log->t.file.path, QSE_COUNTOF(log->t.file.path), target->file); /* If you just want to set the target file path without enable QSE_LOG_FILE,
log->flags |= 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_CONSOLE) log->flags |= QSE_LOG_CONSOLE;
if (flags & QSE_LOG_SYSLOG) log->flags |= QSE_LOG_SYSLOG; 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) 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; return log->flags & QSE_LOG_MASK_TARGET;
} }
void qse_log_setoption (qse_log_t* log, int option) 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); 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_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; 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)? xlen = (tlen <= 0)?
qse_strxcpy (&buf[tlen], rem, __priority_names[i]): 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; rem -= xlen;
tlen += 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; 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_size_t i;
qse_cstr_t tok; qse_cstr_t tok;
@ -718,7 +730,7 @@ int qse_get_log_priority_by_name (const qse_char_t* name)
ptr = name; ptr = name;
while (ptr) while (ptr)
{ {
ptr = qse_strtok (ptr, QSE_T("|"), &tok); ptr = qse_strtok (ptr, delim, &tok);
if (tok.ptr) if (tok.ptr)
{ {
for (i = 0; i < QSE_COUNTOF(__priority_names); i++) for (i = 0; i < QSE_COUNTOF(__priority_names); i++)

View File

@ -26,14 +26,14 @@ static void t1_sub001 (qse_log_t* log)
for (i = 0; i < QSE_COUNTOF(xtab); i++) 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); 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++) for (i = 0; i < QSE_COUNTOF(xtab); i++)
{ {
int pri; 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); QSE_LOG2 (log, QSE_NULL, QSE_LOG_INFO, QSE_T("SUCCESS: %s => %x"), xtab[i].name, pri);
} }