change the logging function to support priority masking
This commit is contained in:
parent
cdc64c347f
commit
c5ac9558ab
@ -14,7 +14,7 @@
|
||||
|
||||
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
|
||||
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||||
OF MERCHANTABILITY AND FITNESS FOR A PARTICAR PURPOSE ARE DISCLAIMED.
|
||||
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||||
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||||
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
@ -37,64 +37,65 @@
|
||||
/* TODO: improve this and complete win32 portion */
|
||||
#define QSE_LOG_IDENT_MAX 32
|
||||
|
||||
#define QSE_LOG_MASK_PRIORITY 0x00000FFFUL
|
||||
#define QSE_LOG_MASK_OPTION 0x000FF000UL
|
||||
#define QSE_LOG_MASK_TARGET 0xFFF00000UL
|
||||
#define QSE_LOG_PRIORITY_LEN_MAX 64
|
||||
|
||||
#define QSE_LOG_MASK_PRIORITY 0x000000FF /* bit 0 to 7 */
|
||||
/* bit 8 to 11 unused */
|
||||
#define QSE_LOG_MASK_OPTION 0x000FF000 /* bit 12 to 19 */
|
||||
#define QSE_LOG_MASK_TARGET 0xFFF00000 /* bit 20 to 31 */
|
||||
|
||||
|
||||
/* priority */
|
||||
#define QSE_LOG_PANIC 0x0000UL
|
||||
#define QSE_LOG_ALERT 0x0001UL
|
||||
#define QSE_LOG_CRITICAL 0x0002UL
|
||||
#define QSE_LOG_ERROR 0x0003UL
|
||||
#define QSE_LOG_WARNING 0x0004UL
|
||||
#define QSE_LOG_NOTICE 0x0005UL
|
||||
#define QSE_LOG_INFO 0x0006UL
|
||||
#define QSE_LOG_DEBUG 0x0007UL
|
||||
#define QSE_LOG_PANIC (1 << 0)
|
||||
#define QSE_LOG_ALERT (1 << 1)
|
||||
#define QSE_LOG_CRITICAL (1 << 2)
|
||||
#define QSE_LOG_ERROR (1 << 3)
|
||||
#define QSE_LOG_WARNING (1 << 4)
|
||||
#define QSE_LOG_NOTICE (1 << 5)
|
||||
#define QSE_LOG_INFO (1 << 6)
|
||||
#define QSE_LOG_DEBUG (1 << 7)
|
||||
|
||||
/* options */
|
||||
#define QSE_LOG_KEEP_FILE_OPEN (1UL << 13)
|
||||
#define QSE_LOG_ENABLE_MASKED (1UL << 14)
|
||||
#define QSE_LOG_INCLUDE_PID (1UL << 15)
|
||||
#define QSE_LOG_HOST_IN_REMOTE_SYSLOG (1UL << 16)
|
||||
#define QSE_LOG_KEEP_FILE_OPEN (1 << 13)
|
||||
#define QSE_LOG_MASKED_PRIORITY (1 << 14)
|
||||
#define QSE_LOG_INCLUDE_PID (1 << 15)
|
||||
#define QSE_LOG_HOST_IN_REMOTE_SYSLOG (1 << 16)
|
||||
|
||||
/* target */
|
||||
#define QSE_LOG_CONSOLE (1UL << 20)
|
||||
#define QSE_LOG_FILE (1UL << 21)
|
||||
#define QSE_LOG_SYSLOG (1UL << 22)
|
||||
#define QSE_LOG_SYSLOG_REMOTE (1UL << 23)
|
||||
#define QSE_LOG_CONSOLE (1 << 20)
|
||||
#define QSE_LOG_FILE (1 << 21)
|
||||
#define QSE_LOG_SYSLOG (1 << 22)
|
||||
#define QSE_LOG_SYSLOG_REMOTE (1 << 23)
|
||||
|
||||
|
||||
|
||||
/* facility */
|
||||
enum qse_log_facility_t
|
||||
{
|
||||
QSE_LOG_KERN = (0<<3), /* kernel messages */
|
||||
QSE_LOG_USER = (1<<3), /* random user-level messages */
|
||||
QSE_LOG_MAIL = (2<<3), /* mail system */
|
||||
QSE_LOG_DAEMON = (3<<3), /* system daemons */
|
||||
QSE_LOG_AUTH = (4<<3), /* security/authorization messages */
|
||||
QSE_LOG_SYSLOGD = (5<<3), /* messages from syslogd */
|
||||
QSE_LOG_LPR = (6<<3), /* line printer subsystem */
|
||||
QSE_LOG_NEWS = (7<<3), /* network news subsystem */
|
||||
QSE_LOG_UUCP = (8<<3), /* UUCP subsystem */
|
||||
QSE_LOG_CRON = (9<<3), /* clock daemon */
|
||||
QSE_LOG_AUTHPRIV = (10<<3), /* authorization messages (private) */
|
||||
QSE_LOG_FTP = (11<<3), /* ftp daemon */
|
||||
QSE_LOG_LOCAL0 = (16<<3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL1 = (17<<3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL2 = (18<<3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL3 = (19<<3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL4 = (20<<3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL5 = (21<<3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL6 = (22<<3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL7 = (23<<3) /* reserved for local use */
|
||||
QSE_LOG_KERN = (0 << 3), /* kernel messages */
|
||||
QSE_LOG_USER = (1 << 3), /* random user-level messages */
|
||||
QSE_LOG_MAIL = (2 << 3), /* mail system */
|
||||
QSE_LOG_DAEMON = (3 << 3), /* system daemons */
|
||||
QSE_LOG_AUTH = (4 << 3), /* security/authorization messages */
|
||||
QSE_LOG_SYSLOGD = (5 << 3), /* messages from syslogd */
|
||||
QSE_LOG_LPR = (6 << 3), /* line printer subsystem */
|
||||
QSE_LOG_NEWS = (7 << 3), /* network news subsystem */
|
||||
QSE_LOG_UUCP = (8 << 3), /* UUCP subsystem */
|
||||
QSE_LOG_CRON = (9 << 3), /* clock daemon */
|
||||
QSE_LOG_AUTHPRIV = (10 << 3), /* authorization messages (private) */
|
||||
QSE_LOG_FTP = (11 << 3), /* ftp daemon */
|
||||
QSE_LOG_LOCAL0 = (16 << 3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL1 = (17 << 3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL2 = (18 << 3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL3 = (19 << 3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL4 = (20 << 3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL5 = (21 << 3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL6 = (22 << 3), /* reserved for local use */
|
||||
QSE_LOG_LOCAL7 = (23 << 3) /* reserved for local use */
|
||||
};
|
||||
typedef enum qse_log_facility_t qse_log_facility_t;
|
||||
|
||||
|
||||
/* TODO: support ENABLE_MASKED??? */
|
||||
#define QSE_LOG_ENABLED(log,pri) ((pri) <= ((log)->flags & QSE_LOG_MASK_PRIORITY))
|
||||
|
||||
#define QSE_LOG_ENABLED(log,pri) ((pri) & (log)->active_priority_bits)
|
||||
|
||||
#if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
|
||||
#define QSE_LOG(log,ident,pri,...) \
|
||||
@ -233,6 +234,8 @@ struct qse_log_t
|
||||
#endif
|
||||
qse_mtx_t mtx;
|
||||
|
||||
int active_priority_bits;
|
||||
|
||||
#if !defined(_WIN32)
|
||||
qse_log_facility_t syslog_facility;
|
||||
#endif
|
||||
@ -298,21 +301,11 @@ QSE_EXPORT void qse_log_setpriority (
|
||||
int priority
|
||||
);
|
||||
|
||||
QSE_EXPORT int qse_log_setprioritybyname (
|
||||
qse_log_t* log,
|
||||
const qse_char_t* name
|
||||
);
|
||||
|
||||
QSE_EXPORT void qse_log_setsyslogfacility (
|
||||
qse_log_t* log,
|
||||
qse_log_facility_t facility
|
||||
);
|
||||
|
||||
QSE_EXPORT int qse_log_setsyslogfacilitybyname (
|
||||
qse_log_t* log,
|
||||
const qse_char_t* name
|
||||
);
|
||||
|
||||
#if defined(QSE_HAVE_INLINE)
|
||||
static QSE_INLINE int qse_log_getpriority (qse_log_t* log)
|
||||
{
|
||||
@ -322,10 +315,6 @@ static QSE_INLINE int qse_log_getpriority (qse_log_t* log)
|
||||
#define qse_log_getpriority(log) ((log)->flags & QSE_LOG_MASK_PRIORITY)
|
||||
#endif
|
||||
|
||||
const qse_char_t* qse_log_getpriorityname (
|
||||
qse_log_t* log
|
||||
);
|
||||
|
||||
|
||||
QSE_EXPORT void qse_log_report (
|
||||
qse_log_t* log,
|
||||
@ -343,8 +332,23 @@ QSE_EXPORT void qse_log_reportv (
|
||||
va_list ap
|
||||
);
|
||||
|
||||
QSE_EXPORT const qse_char_t* qse_get_log_priority_name (
|
||||
int pri
|
||||
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 */
|
||||
);
|
||||
|
||||
/**
|
||||
* \return an integer bitwised-ORed of priority bits if \a name is valid.
|
||||
* 0 if \a name is invalid or empty.
|
||||
*/
|
||||
QSE_EXPORT int qse_get_log_priority_by_name (
|
||||
const qse_char_t* name
|
||||
);
|
||||
|
||||
QSE_EXPORT int qse_get_log_facility_by_name (
|
||||
const qse_char_t* name,
|
||||
qse_log_facility_t* fcode
|
||||
);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
251
qse/lib/si/log.c
251
qse/lib/si/log.c
@ -41,18 +41,6 @@
|
||||
#include "../cmn/syscall.h"
|
||||
#endif
|
||||
|
||||
static const qse_char_t* __priority_names[] =
|
||||
{
|
||||
QSE_T("panic"),
|
||||
QSE_T("alert"),
|
||||
QSE_T("critical"),
|
||||
QSE_T("error"),
|
||||
QSE_T("warning"),
|
||||
QSE_T("notice"),
|
||||
QSE_T("info"),
|
||||
QSE_T("debug")
|
||||
};
|
||||
|
||||
static const qse_mchar_t* __syslog_month_names[] =
|
||||
{
|
||||
QSE_MT("Jan"),
|
||||
@ -94,16 +82,48 @@ static const qse_mchar_t* __syslog_month_names[] =
|
||||
# define LOG_DEBUG 7
|
||||
#endif
|
||||
|
||||
static int __syslog_priority[] =
|
||||
/* use a simple look-up table for mapping a priority to a syslog value.
|
||||
* i assume it's faster than getting the position of the first lowest bit set
|
||||
* and use a smaller and dense table without gap-filling 0s. */
|
||||
static int __syslog_priority[256] =
|
||||
{
|
||||
LOG_EMERG,
|
||||
LOG_ALERT,
|
||||
LOG_CRIT,
|
||||
LOG_ERR,
|
||||
LOG_WARNING,
|
||||
LOG_NOTICE,
|
||||
LOG_INFO,
|
||||
LOG_DEBUG
|
||||
0,
|
||||
LOG_EMERG, /* 1 */
|
||||
LOG_ALERT, /* 2 */
|
||||
0,
|
||||
LOG_CRIT, /* 4 */
|
||||
0, 0, 0,
|
||||
LOG_ERR, /* 8 */
|
||||
0, 0, 0, 0, 0, 0, 0,
|
||||
LOG_WARNING, /* 16 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
LOG_NOTICE, /* 32 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0,
|
||||
LOG_INFO, /* 64 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0,
|
||||
LOG_DEBUG, /* 128 */
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
|
||||
0, 0, 0, 0, 0, 0, 0
|
||||
};
|
||||
|
||||
#if defined(_WIN32)
|
||||
@ -137,12 +157,45 @@ static struct syslog_fac_info_t __syslog_fac_info[] =
|
||||
{ QSE_T("local4"), QSE_LOG_LOCAL4 },
|
||||
{ QSE_T("local5"), QSE_LOG_LOCAL5 },
|
||||
{ QSE_T("local6"), QSE_LOG_LOCAL6 },
|
||||
{ QSE_T("local7"), QSE_LOG_LOCAL7 },
|
||||
|
||||
{ QSE_NULL, 0 }
|
||||
{ QSE_T("local7"), QSE_LOG_LOCAL7 }
|
||||
};
|
||||
#endif
|
||||
|
||||
static QSE_INLINE int get_active_priority_bits (int flags)
|
||||
{
|
||||
int priority_bits = 0;
|
||||
|
||||
if (flags & QSE_LOG_MASKED_PRIORITY)
|
||||
{
|
||||
priority_bits = flags & QSE_LOG_MASK_PRIORITY;
|
||||
}
|
||||
else
|
||||
{
|
||||
int pri = flags & QSE_LOG_MASK_PRIORITY;
|
||||
switch (pri)
|
||||
{
|
||||
case QSE_LOG_DEBUG:
|
||||
priority_bits |= QSE_LOG_DEBUG;
|
||||
case QSE_LOG_INFO:
|
||||
priority_bits |= QSE_LOG_INFO;
|
||||
case QSE_LOG_NOTICE:
|
||||
priority_bits |= QSE_LOG_NOTICE;
|
||||
case QSE_LOG_WARNING:
|
||||
priority_bits |= QSE_LOG_WARNING;
|
||||
case QSE_LOG_ERROR:
|
||||
priority_bits |= QSE_LOG_ERROR;
|
||||
case QSE_LOG_CRITICAL:
|
||||
priority_bits |= QSE_LOG_CRITICAL;
|
||||
case QSE_LOG_ALERT:
|
||||
priority_bits |= QSE_LOG_ALERT;
|
||||
case QSE_LOG_PANIC:
|
||||
priority_bits |= QSE_LOG_PANIC;
|
||||
}
|
||||
}
|
||||
|
||||
return priority_bits;
|
||||
}
|
||||
|
||||
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));
|
||||
@ -169,12 +222,15 @@ int qse_log_init (qse_log_t* log, qse_mmgr_t* mmgr, const qse_char_t* ident, int
|
||||
if (ident) qse_strxcpy (log->ident, QSE_COUNTOF(log->ident), ident);
|
||||
if (qse_mtx_init(&log->mtx, mmgr) <= -1) return -1;
|
||||
|
||||
log->active_priority_bits = get_active_priority_bits(log->flags);
|
||||
|
||||
#if defined(_WIN32)
|
||||
/* TODO: windows event logging */
|
||||
#else
|
||||
log->syslog_facility = QSE_LOG_USER;
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -328,64 +384,16 @@ void qse_log_setoption (qse_log_t* log, int option)
|
||||
void qse_log_setpriority (qse_log_t* log, int priority)
|
||||
{
|
||||
log->flags = (log->flags & (QSE_LOG_MASK_TARGET | QSE_LOG_MASK_OPTION)) | (priority & QSE_LOG_MASK_PRIORITY);
|
||||
log->active_priority_bits = get_active_priority_bits (log->flags);
|
||||
}
|
||||
|
||||
int qse_log_setprioritybyname (qse_log_t* log, const qse_char_t* name)
|
||||
{
|
||||
|
||||
qse_size_t i;
|
||||
|
||||
for (i = 0; i < QSE_COUNTOF(__priority_names); i++)
|
||||
{
|
||||
if (qse_strcmp(__priority_names[i], name) == 0)
|
||||
{
|
||||
qse_log_setpriority (log, i);
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
||||
const qse_char_t* qse_log_getpriorityname (qse_log_t* log)
|
||||
{
|
||||
int pri = log->flags & QSE_LOG_MASK_PRIORITY;
|
||||
|
||||
if (pri < 0 || pri >= QSE_COUNTOF(__priority_names)) return QSE_NULL;
|
||||
return __priority_names[pri];
|
||||
}
|
||||
|
||||
|
||||
void qse_log_setsyslogfacility (qse_log_t* log, qse_log_facility_t facility)
|
||||
{
|
||||
#ifndef _WIN32
|
||||
#if !defined(_WIN32)
|
||||
log->syslog_facility = facility;
|
||||
#endif
|
||||
}
|
||||
|
||||
int qse_log_setsyslogfacilitybyname (qse_log_t* log, const qse_char_t* name)
|
||||
{
|
||||
#if defined(_WIN32)
|
||||
/* do nothing */
|
||||
return 0;
|
||||
#else
|
||||
struct syslog_fac_info_t* f = __syslog_fac_info;
|
||||
|
||||
while (f->name != QSE_NULL)
|
||||
{
|
||||
if (qse_strcmp (f->name, name) == 0)
|
||||
{
|
||||
log->syslog_facility = f->code;
|
||||
return 0;
|
||||
}
|
||||
|
||||
f++;
|
||||
}
|
||||
|
||||
return -1;
|
||||
#endif
|
||||
}
|
||||
|
||||
static QSE_INLINE void __report_over_sio (qse_log_t* log, qse_sio_t* sio, const qse_mchar_t* tm, const qse_char_t* ident, const qse_char_t* fmt, va_list arg)
|
||||
{
|
||||
int id_out = 0;
|
||||
@ -442,7 +450,7 @@ void qse_log_reportv (qse_log_t* log, const qse_char_t* ident, int pri, const qs
|
||||
|
||||
if ((log->flags & QSE_LOG_MASK_TARGET) == 0) return; /* no target */
|
||||
|
||||
if (log->flags & QSE_LOG_ENABLE_MASKED)
|
||||
if (log->flags & QSE_LOG_MASKED_PRIORITY)
|
||||
{
|
||||
if (!(pri & (log->flags & QSE_LOG_MASK_PRIORITY))) return;
|
||||
}
|
||||
@ -522,6 +530,7 @@ void qse_log_reportv (qse_log_t* log, const qse_char_t* ident, int pri, const qs
|
||||
if (!log->wmsgbuf) goto done;
|
||||
#endif
|
||||
|
||||
/* the priority value given must have only 1 bit set. otherwise, it will translate to wrong values */
|
||||
sl_pri = (pri < QSE_COUNTOF(__syslog_priority))? __syslog_priority[(pri & QSE_LOG_MASK_PRIORITY)]: LOG_DEBUG;
|
||||
|
||||
fplen = qse_mbs_fmt(log->dmsgbuf, QSE_MT("<%d>"), (int)(log->syslog_facility | sl_pri));
|
||||
@ -533,7 +542,6 @@ void qse_log_reportv (qse_log_t* log, const qse_char_t* ident, int pri, const qs
|
||||
cnow.hour, cnow.min, cnow.sec);
|
||||
if (fpdlen == (qse_size_t)-1) goto done;
|
||||
|
||||
|
||||
if (log->flags & QSE_LOG_HOST_IN_REMOTE_SYSLOG)
|
||||
{
|
||||
struct utsname un;
|
||||
@ -656,8 +664,91 @@ done:
|
||||
qse_mtx_unlock (&log->mtx);
|
||||
}
|
||||
|
||||
const qse_char_t* qse_get_log_priority_name (int pri)
|
||||
/* --------------------------------------------------------------------------
|
||||
* HELPER FUNCTIONS
|
||||
* -------------------------------------------------------------------------- */
|
||||
|
||||
static const qse_char_t* __priority_names[] =
|
||||
{
|
||||
if (pri < 0 || pri >= QSE_COUNTOF(__priority_names)) return QSE_NULL;
|
||||
return __priority_names[pri];
|
||||
/* NOTE: QSE_LOG_PRIORITY_LEN_MAX must be redefined if strings here
|
||||
* can produce a longer compositional name. e.g. panic|critical|... */
|
||||
QSE_T("panic"),
|
||||
QSE_T("alert"),
|
||||
QSE_T("critical"),
|
||||
QSE_T("error"),
|
||||
QSE_T("warning"),
|
||||
QSE_T("notice"),
|
||||
QSE_T("info"),
|
||||
QSE_T("debug")
|
||||
};
|
||||
|
||||
qse_size_t qse_get_log_priority_name (int pri, qse_char_t* buf, qse_size_t len)
|
||||
{
|
||||
qse_size_t tlen, xlen, rem, i;
|
||||
|
||||
tlen = 0;
|
||||
rem = len;
|
||||
|
||||
for (i = 0; i < QSE_COUNTOF(__priority_names); i++)
|
||||
{
|
||||
if (pri & (1UL << i))
|
||||
{
|
||||
if (rem <= 1) break;
|
||||
|
||||
xlen = (tlen <= 0)?
|
||||
qse_strxcpy (&buf[tlen], rem, __priority_names[i]):
|
||||
qse_strxjoin (&buf[tlen], rem, QSE_T("|"), __priority_names[i], QSE_NULL);
|
||||
|
||||
rem -= xlen;
|
||||
tlen += xlen;
|
||||
}
|
||||
}
|
||||
|
||||
if (len >= tlen) buf[tlen] = QSE_T('\0');
|
||||
return tlen;
|
||||
}
|
||||
|
||||
int qse_get_log_priority_by_name (const qse_char_t* name)
|
||||
{
|
||||
qse_size_t i;
|
||||
qse_cstr_t tok;
|
||||
const qse_char_t* ptr;
|
||||
int pri = 0;
|
||||
|
||||
ptr = name;
|
||||
while (ptr)
|
||||
{
|
||||
ptr = qse_strtok (ptr, QSE_T("|"), &tok);
|
||||
if (tok.ptr)
|
||||
{
|
||||
for (i = 0; i < QSE_COUNTOF(__priority_names); i++)
|
||||
{
|
||||
if (qse_strxcmp(tok.ptr, tok.len, __priority_names[i]) == 0)
|
||||
{
|
||||
pri |= (1UL << i);
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (i >= QSE_COUNTOF(__priority_names)) return 0; /* unknown name included */
|
||||
}
|
||||
}
|
||||
|
||||
return pri;
|
||||
}
|
||||
|
||||
|
||||
int qse_get_log_facility_by_name (const qse_char_t* name, qse_log_facility_t* fcode)
|
||||
{
|
||||
qse_size_t i;
|
||||
|
||||
for (i = 0; i < QSE_COUNTOF(__syslog_fac_info); i++)
|
||||
{
|
||||
if (qse_strcmp (__syslog_fac_info[i].name, name) == 0)
|
||||
{
|
||||
*fcode = __syslog_fac_info[i].code;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
|
@ -4,7 +4,47 @@
|
||||
#include <qse/si/nwad.h>
|
||||
#include <stdio.h>
|
||||
|
||||
void t1 (void)
|
||||
static void t1_sub001 (qse_log_t* log)
|
||||
{
|
||||
static struct
|
||||
{
|
||||
const qse_char_t* name;
|
||||
int pri;
|
||||
} xtab[] =
|
||||
{
|
||||
{ QSE_T(""), 0 },
|
||||
{ QSE_T(" "), 0 },
|
||||
{ QSE_T("|"), 0 },
|
||||
{ QSE_T("debug|xinfo|panic"), 0 },
|
||||
{ QSE_T("debug|info|panic"), (QSE_LOG_DEBUG | QSE_LOG_INFO | QSE_LOG_PANIC) },
|
||||
{ QSE_T("debug|notice|debug|panic"), (QSE_LOG_DEBUG | QSE_LOG_NOTICE | QSE_LOG_PANIC) },
|
||||
{ QSE_T("notice"), QSE_LOG_NOTICE }
|
||||
};
|
||||
qse_size_t i;
|
||||
qse_char_t buf[QSE_LOG_PRIORITY_LEN_MAX + 1];
|
||||
qse_size_t len;
|
||||
|
||||
for (i = 0; i < QSE_COUNTOF(xtab); i++)
|
||||
{
|
||||
len = qse_get_log_priority_name (xtab[i].pri, 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)
|
||||
{
|
||||
QSE_LOG2 (log, QSE_NULL, QSE_LOG_INFO, QSE_T("SUCCESS: %s => %x"), xtab[i].name, pri);
|
||||
}
|
||||
else
|
||||
{
|
||||
QSE_LOG3 (log, QSE_NULL, QSE_LOG_INFO, QSE_T("FAILURE: %s => got %x expected %x"), xtab[i].name, pri, xtab[i].pri);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void t1 (void)
|
||||
{
|
||||
qse_log_t* log;
|
||||
qse_log_target_t t;
|
||||
@ -32,6 +72,8 @@ void t1 (void)
|
||||
QSE_ASSERT (qse_log_getoption (log) == (QSE_LOG_INCLUDE_PID | QSE_LOG_HOST_IN_REMOTE_SYSLOG));
|
||||
QSE_ASSERT (qse_log_gettarget (log, QSE_NULL) == (QSE_LOG_CONSOLE | QSE_LOG_FILE | QSE_LOG_SYSLOG | QSE_LOG_SYSLOG_REMOTE));
|
||||
|
||||
t1_sub001 (log);
|
||||
|
||||
for (i = 0; i < 10; i++)
|
||||
{
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user