added Ixx length specifier to the formatting function.

improved the system log interface in the logging function
This commit is contained in:
hyung-hwan 2017-09-16 03:21:30 +00:00
parent 93cc3d6da4
commit 33436ab0fb
4 changed files with 286 additions and 83 deletions

View File

@ -52,13 +52,14 @@
/* options */ /* options */
#define QSE_LOG_KEEP_FILE_OPEN (1UL << 13) #define QSE_LOG_KEEP_FILE_OPEN (1UL << 13)
#define QSE_LOG_ENABLE_MASKED (1UL << 14) #define QSE_LOG_ENABLE_MASKED (1UL << 14)
#define QSE_LOG_SYSLOG_PID (1UL << 15) #define QSE_LOG_INCLUDE_PID (1UL << 15)
/* target */ /* target */
#define QSE_LOG_CONSOLE (1UL << 20) #define QSE_LOG_CONSOLE (1UL << 20)
#define QSE_LOG_FILE (1UL << 21) #define QSE_LOG_FILE (1UL << 21)
#define QSE_LOG_SYSLOG (1UL << 22) #define QSE_LOG_SYSLOG (1UL << 22)
#define QSE_LOG_SYSLOG_REMOTE (1UL << 23) #define QSE_LOG_SYSLOG_LOCAL (1UL << 23)
#define QSE_LOG_SYSLOG_REMOTE (1UL << 24)
#define QSE_LOG_MASK_PRIORITY 0x00000FFFUL #define QSE_LOG_MASK_PRIORITY 0x00000FFFUL
#define QSE_LOG_MASK_OPTION 0x000FF000UL #define QSE_LOG_MASK_OPTION 0x000FF000UL
@ -217,6 +218,9 @@ struct qse_log_t
qse_char_t ident[QSE_LOG_IDENT_MAX + 1]; qse_char_t ident[QSE_LOG_IDENT_MAX + 1];
qse_mbs_t* dmsgbuf; qse_mbs_t* dmsgbuf;
#if defined(QSE_CHAR_IS_WCHAR)
qse_wcs_t* wmsgbuf;
#endif
qse_mtx_t mtx; qse_mtx_t mtx;
#if !defined(_WIN32) #if !defined(_WIN32)

View File

@ -63,10 +63,16 @@ enum
LF_T = (1 << 5), LF_T = (1 << 5),
LF_Z = (1 << 6), LF_Z = (1 << 6),
LF_I8 = (1 << 7),
LF_I16 = (1 << 8),
LF_I32 = (1 << 9),
LF_I64 = (1 << 10),
LF_I128 = (1 << 11),
/* long double */ /* long double */
LF_LD = (1 << 7), LF_LD = (1 << 12),
/* __float128 */ /* __float128 */
LF_QD = (1 << 8) LF_QD = (1 << 13)
}; };
static struct static struct

View File

@ -243,6 +243,7 @@ reswitch:
case T('1'): case T('2'): case T('3'): case T('4'): case T('1'): case T('2'): case T('3'): case T('4'):
case T('5'): case T('6'): case T('7'): case T('8'): case T('9'): case T('5'): case T('6'): case T('7'): case T('8'): case T('9'):
if (flagc & FLAGC_LENMOD) goto invalid_format;
for (n = 0;; ++fmt) for (n = 0;; ++fmt)
{ {
n = n * 10 + ch - T('0'); n = n * 10 + ch - T('0');
@ -320,6 +321,54 @@ reswitch:
lm_flag |= LF_QD; lm_flag |= LF_QD;
goto reswitch; goto reswitch;
case T('I'):
{
int save_lm_flag = lm_flag;
if (fmt[0] == T('8'))
{
lm_flag |= LF_I8;
fmt += 1;
}
else if (fmt[0] == T('1') && fmt[1] == T('6'))
{
lm_flag |= LF_I16;
fmt += 2;
}
#if defined(QSE_HAVE_INT128_T)
else if (fmt[0] == T('1') && fmt[1] == T('2') && fmt[2] == T('8'))
{
lm_flag |= LF_I128;
fmt += 3;
}
#endif
else if (fmt[0] == T('3') && fmt[1] == T('2'))
{
lm_flag |= LF_I32;
fmt += 2;
}
#if defined(QSE_HAVE_INT64_T)
else if (fmt[0] == T('6') && fmt[1] == T('4'))
{
lm_flag |= LF_I64;
fmt += 2;
}
#endif
else
{
goto invalid_format;
}
if (flagc & FLAGC_LENMOD)
{
/* conflict with other length modifier */
save_lm_flag = lm_flag;
goto invalid_format;
}
flagc |= FLAGC_LENMOD;
goto reswitch;
}
/* end of length modifiers */ /* end of length modifiers */
case T('n'): case T('n'):
@ -364,6 +413,9 @@ reswitch:
case T('x'): case T('x'):
base = 16; base = 16;
goto handle_nosign; goto handle_nosign;
case T('b'):
base = 2;
goto handle_nosign;
/* end of unsigned integer conversions */ /* end of unsigned integer conversions */
case T('p'): /* pointer */ case T('p'): /* pointer */
@ -748,7 +800,7 @@ reswitch:
goto print_lowercase_s; goto print_lowercase_s;
} }
handle_nosign: handle_nosign:
sign = 0; sign = 0;
if (lm_flag & LF_J) if (lm_flag & LF_J)
{ {
@ -787,6 +839,37 @@ handle_nosign:
num = (unsigned short int)va_arg (ap, int); num = (unsigned short int)va_arg (ap, int);
else if (lm_flag & LF_C) else if (lm_flag & LF_C)
num = (unsigned char)va_arg (ap, int); num = (unsigned char)va_arg (ap, int);
else if (lm_flag & LF_I8)
#if (QSE_SIZEOF_UINT8_T < QSE_SIZEOF_UINT)
num = (qse_uint8_t)va_arg (ap, unsigned int);
#else
num = va_arg (ap, qse_uint8_t);
#endif
else if (lm_flag & LF_I16)
#if (QSE_SIZEOF_UINT16_T < QSE_SIZEOF_UINT)
num = (qse_uint16_t)va_arg (ap, unsigned int);
#else
num = va_arg (ap, qse_uint16_t);
#endif
else if (lm_flag & LF_I32)
#if (QSE_SIZEOF_UINT32_T < QSE_SIZEOF_UINT)
num = (qse_uint32_t)va_arg (ap, unsigned int);
#else
num = va_arg (ap, qse_uint32_t);
#endif
else if (lm_flag & LF_I64)
#if defined(QSE_HAVE_UINT64_T) && (QSE_SIZEOF_UINT64_T < QSE_SIZEOF_UINT)
num = (qse_uint64_t)va_arg (ap, unsigned int);
#else
num = va_arg (ap, qse_uint64_t);
#endif
else if (lm_flag & LF_I128)
#if defined(QSE_HAVE_UINT128_T) && (QSE_SIZEOF_UINT128_T < QSE_SIZEOF_UINT)
num = (qse_uint128_t)va_arg (ap, unsigned int);
#else
num = va_arg (ap, qse_uint128_t);
#endif
else else
num = va_arg (ap, unsigned int); num = va_arg (ap, unsigned int);
goto number; goto number;
@ -830,6 +913,37 @@ handle_sign:
num = (short int)va_arg (ap, int); num = (short int)va_arg (ap, int);
else if (lm_flag & LF_C) else if (lm_flag & LF_C)
num = (char)va_arg (ap, int); num = (char)va_arg (ap, int);
else if (lm_flag & LF_I8)
#if (QSE_SIZEOF_INT8_T < QSE_SIZEOF_INT)
num = (qse_int8_t)va_arg (ap, int);
#else
num = va_arg (ap, qse_int8_t);
#endif
else if (lm_flag & LF_I16)
#if (QSE_SIZEOF_INT16_T < QSE_SIZEOF_INT)
num = (qse_int16_t)va_arg (ap, int);
#else
num = va_arg (ap, qse_int16_t);
#endif
else if (lm_flag & LF_I32)
#if (QSE_SIZEOF_INT32_T < QSE_SIZEOF_INT)
num = (qse_int32_t)va_arg (ap, int);
#else
num = va_arg (ap, qse_int32_t);
#endif
else if (lm_flag & LF_I64)
#if defined(QSE_HAVE_INT64_T) && (QSE_SIZEOF_INT64_T < QSE_SIZEOF_INT)
num = (qse_int64_t)va_arg (ap, int);
#else
num = va_arg (ap, qse_int64_t);
#endif
else if (lm_flag & LF_I128)
#if defined(QSE_HAVE_INT128_T) && (QSE_SIZEOF_INT128_T < QSE_SIZEOF_INT)
num = (qse_int128_t)va_arg (ap, int);
#else
num = va_arg (ap, qse_int128_t);
#endif
else else
num = va_arg (ap, int); num = va_arg (ap, int);
@ -867,7 +981,12 @@ number:
if ((flagc & FLAGC_SHARP) && num != 0) if ((flagc & FLAGC_SHARP) && num != 0)
{ {
if (base == 8) if (base == 2)
{
PUT_CHAR(T('0'));
PUT_CHAR(T('b'));
}
else if (base == 8)
{ {
PUT_CHAR(T('0')); PUT_CHAR(T('0'));
} }

View File

@ -35,6 +35,8 @@
#else #else
#include <syslog.h> #include <syslog.h>
#include <sys/socket.h> #include <sys/socket.h>
#include <netinet/in.h>
#include <sys/un.h>
#include "../cmn/syscall.h" #include "../cmn/syscall.h"
#endif #endif
@ -51,6 +53,22 @@ static const qse_char_t* __priority_names[] =
QSE_NULL QSE_NULL
}; };
static const qse_char_t* __syslog_month_names[] =
{
QSE_MT("Jan"),
QSE_MT("Feb"),
QSE_MT("Mar"),
QSE_MT("Apr"),
QSE_MT("May"),
QSE_MT("Jun"),
QSE_MT("Jul"),
QSE_MT("Aug"),
QSE_MT("Sep"),
QSE_MT("Oct"),
QSE_MT("Nov"),
QSE_MT("Dec"),
};
#ifndef LOG_EMERG #ifndef LOG_EMERG
# define LOG_EMERG 0 # define LOG_EMERG 0
#endif #endif
@ -140,6 +158,7 @@ int qse_log_init (qse_log_t* log, qse_mmgr_t* mmgr, const qse_char_t* ident, int
} }
if (potflags & QSE_LOG_CONSOLE) log->flags |= QSE_LOG_CONSOLE; if (potflags & QSE_LOG_CONSOLE) log->flags |= QSE_LOG_CONSOLE;
if (potflags & QSE_LOG_SYSLOG) log->flags |= QSE_LOG_SYSLOG; if (potflags & QSE_LOG_SYSLOG) log->flags |= QSE_LOG_SYSLOG;
if (potflags & QSE_LOG_SYSLOG_LOCAL) log->flags |= QSE_LOG_SYSLOG_LOCAL;
if (potflags & QSE_LOG_SYSLOG_REMOTE) if (potflags & QSE_LOG_SYSLOG_REMOTE)
{ {
@ -191,6 +210,14 @@ void qse_log_fini (qse_log_t* log)
qse_mbs_close (log->dmsgbuf); qse_mbs_close (log->dmsgbuf);
log->dmsgbuf = QSE_NULL; log->dmsgbuf = QSE_NULL;
} }
#if defined(QSE_CHAR_IS_WCHAR)
if (log->wmsgbuf)
{
qse_wcs_close (log->wmsgbuf);
log->wmsgbuf = QSE_NULL;
}
#endif
qse_mtx_fini (&log->mtx); qse_mtx_fini (&log->mtx);
} }
@ -368,6 +395,11 @@ static QSE_INLINE void __report_over_sio (qse_log_t* log, qse_sio_t* sio, const
} }
} }
if (log->flags & QSE_LOG_INCLUDE_PID)
{
qse_sio_putmbsf (sio, QSE_MT("[%d]"), (int)QSE_GETPID());
id_out = 1;
}
if (id_out) qse_sio_putmbs (sio, QSE_MT(": ")); if (id_out) qse_sio_putmbs (sio, QSE_MT(": "));
qse_sio_putstrvf (sio, fmt, arg); qse_sio_putstrvf (sio, fmt, arg);
@ -400,8 +432,7 @@ void qse_log_reportv (qse_log_t* log, const qse_char_t* ident, int pri, const qs
if (pri > (log->flags & QSE_LOG_MASK_PRIORITY)) return; if (pri > (log->flags & QSE_LOG_MASK_PRIORITY)) return;
} }
if (qse_gettime(&now) <= -1) return; if (qse_gettime(&now) || qse_localtime(&now, &cnow) <= -1) return;
if (qse_localtime(&now, &cnow) <= -1) return;
if (log->flags & (QSE_LOG_CONSOLE | QSE_LOG_FILE)) if (log->flags & (QSE_LOG_CONSOLE | QSE_LOG_FILE))
{ {
@ -457,26 +488,89 @@ void qse_log_reportv (qse_log_t* log, const qse_char_t* ident, int pri, const qs
} }
} }
if (log->flags & (QSE_LOG_SYSLOG | QSE_LOG_SYSLOG_REMOTE)) if (log->flags & (QSE_LOG_SYSLOG | QSE_LOG_SYSLOG_LOCAL | QSE_LOG_SYSLOG_REMOTE))
{ {
va_list xap; va_list xap;
qse_size_t fplen, fpdlen, fpdilen;
int sl_pri, id_out = 0;
const qse_mchar_t* identfmt, * identparenfmt;
if (!log->dmsgbuf) log->dmsgbuf = qse_mbs_open (log->mmgr, 0, 0); if (!log->dmsgbuf) log->dmsgbuf = qse_mbs_open (log->mmgr, 0, 0);
if (!log->dmsgbuf) goto done; if (!log->dmsgbuf) goto done;
va_copy (xap, ap); #if defined(QSE_CHAR_IS_WCHAR)
if (qse_str_vfmt (log->dmsgbuf, fmt, xap) == QSE_TYPE_MAX(qse_size_t)) goto done; if (!log->wmsgbuf) log->wmsgbuf = qse_wcs_open (log->mmgr, 0, 0);
if (!log->wmsgbuf) goto done;
#endif
sl_pri = (pri < QSE_COUNTOF(__syslog_priority))? __syslog_priority[pri]: LOG_DEBUG;
fplen = qse_mbs_fmt(log->dmsgbuf, QSE_MT("<%d>"), (int)(log->syslog_facility | sl_pri));
if (fplen == (qse_size_t)-1) goto done;
fpdlen = qse_mbs_fcat (
log->dmsgbuf, QSE_MT("%s %02d %02d:%02d:%02d "),
__syslog_month_names[cnow.mon], cnow.mday,
cnow.hour, cnow.min, cnow.sec);
if (fpdlen == (qse_size_t)-1) goto done;
#if defined(QSE_CHAR_IS_MCHAR)
identfmt = QSE_MT("%hs");
identparenfmt = QSE_MT("(%hs)");
#else
identfmt = QSE_MT("%ls");
identparenfmt = QSE_MT("(%ls)");
#endif
if (log->ident[0])
{
if (qse_mbs_fcat (log->dmsgbuf, identfmt, log->ident) == (qse_size_t)-1) goto done;
if (ident && ident[0] &&
qse_mbs_fcat (log->dmsgbuf, identparenfmt, ident) == (qse_size_t)-1) goto done;
id_out = 1;
} }
else
{
if (ident && ident[0])
{
if (qse_mbs_fcat (log->dmsgbuf, identfmt, ident) == (qse_size_t)-1) goto done;
id_out = 1;
}
}
if (log->flags & QSE_LOG_INCLUDE_PID)
{
fpdilen = qse_mbs_fcat (log->dmsgbuf, QSE_MT("[%d]"), (int)QSE_GETPID());
if (fpdilen == (qse_size_t)-1) goto done;
id_out = 1;
}
if (id_out)
{
fpdilen = qse_mbs_fcat (log->dmsgbuf, QSE_MT(": "), log->ident);
if (fpdilen == (qse_size_t)-1) goto done;
}
else
{
fpdilen = fpdlen;
}
va_copy (xap, ap);
#if defined(QSE_CHAR_IS_MCHAR)
if (qse_mbs_vfcat (log->dmsgbuf, fmt, xap) == (qse_size_t)-1) goto done;
#else
if (qse_wcs_vfmt (log->wmsgbuf, fmt, xap) == (qse_size_t)-1 ||
qse_mbs_vfcat (log->dmsgbuf, QSE_MT("%.*ls"), QSE_WCS_LEN(log->wmsgbuf), QSE_WCS_PTR(log->wmsgbuf)) == (qse_size_t)-1) goto done;
#endif
if (log->flags & QSE_LOG_SYSLOG) if (log->flags & QSE_LOG_SYSLOG)
{ {
#if defined(_WIN32) #if defined(_WIN32)
/* TODO: windows event log */ /* TODO: windows event log */
#else #else
int sl_pri, sl_opt; int sl_opt;
sl_opt = (log->flags & QSE_LOG_SYSLOG_PID)? LOG_PID: 0; sl_opt = (log->flags & QSE_LOG_INCLUDE_PID)? LOG_PID: 0;
sl_pri = (pri < QSE_COUNTOF(__syslog_priority))? __syslog_priority[pri]: LOG_DEBUG;
if (!log->t.syslog.opened) if (!log->t.syslog.opened)
{ {
@ -491,62 +585,42 @@ void qse_log_reportv (qse_log_t* log, const qse_char_t* ident, int pri, const qs
log->t.syslog.opened = 1; log->t.syslog.opened = 1;
} }
syslog (sl_pri, "%s", QSE_MBS_PTR(log->dmsgbuf)); syslog (sl_pri, "%s", QSE_MBS_CPTR(log->dmsgbuf, fpdilen));
#endif #endif
} }
/* remote syslogging for ipv4 */
if (log->flags & QSE_LOG_SYSLOG_REMOTE) if (log->flags & QSE_LOG_SYSLOG_REMOTE)
{ {
#if defined(_WIN32) #if defined(_WIN32)
/* TODO: windows event log */ /* TODO: windows event log */
#else #else
/* direct interface over udp to a remote syslog server */ /* direct interface over udp to a remote syslog server.
* if you specify a local unix socket address, it may interact with a local syslog server */
#if 0
int sl_pri;
sl_pri = (pri < QSE_COUNTOF(__syslog_priority))? __syslog_priority[pri]: LOG_DEBUG;
#if 0
qse_formattime (tm, QSE_COUNTOF(tm), QSE_T("%b %d %H:%M:%S"), &cnow);
#endif
if (idt == QSE_NULL)
{
qse_strxfmt (log->msgbuf2, QSE_COUNTOF(log->msgbuf2),
QSE_T("<%d>%s %s"),
(int)(log->syslog_facility | sl_pri),
tm, log->msgbuf);
}
else
{
qse_strxfmt (log->msgbuf2, QSE_COUNTOF(log->msgbuf2),
QSE_T("<%d>%s %s: %s"),
(int)(log->syslog_facility | sl_pri),
tm, idt, log->msgbuf);
}
if (log->t.syslog_remote.sock <= -1) if (log->t.syslog_remote.sock <= -1)
{
#if defined(SOCK_CLOEXECX)
log->t.syslog_remote.sock = socket (qse_skadfamily(&log->t.syslog_remote.addr), SOCK_DGRAM | SOCK_CLOEXEC, 0);
#else
log->t.syslog_remote.sock = socket (qse_skadfamily(&log->t.syslog_remote.addr), SOCK_DGRAM, 0); log->t.syslog_remote.sock = socket (qse_skadfamily(&log->t.syslog_remote.addr), SOCK_DGRAM, 0);
#if defined(FD_CLOEXEC)
if (log->t.syslog_remote.sock >= 0)
{
int flag = fcntl (log->t.syslog_remote.sock, F_GETFD);
if (flag >= 0) fcntl (log->t.syslog_remote.sock, F_SETFD, flag | FD_CLOEXEC);
}
#endif
#endif
}
if (log->t.syslog_remote.sock >= 0) if (log->t.syslog_remote.sock >= 0)
{ {
#if defined(QSE_CHAR_IS_MCHAR) sendto (log->t.syslog_remote.sock, QSE_MBS_PTR(log->dmsgbuf), QSE_MBS_LEN(log->dmsgbuf), 0,
sendto (log->t.syslog_remote.sock, log->msgbuf2, qse_strlen(log->msgbuf2), 0,
(struct sockaddr*)&log->t.syslog_remote.addr,
qse_skadsize(&log->t.syslog_remote.addr));
#else
qse_wcstomcs (log->msgbuf2, log->msgbuf_mb, QSE_COUNTOF(log->msgbuf_mb));
sendto (log->t.syslog_remote.sock,
log->msgbuf_mb, qse_mstrlen(log->msgbuf_mb), 0,
(struct sockaddr*)&log->t.syslog_remote.addr, (struct sockaddr*)&log->t.syslog_remote.addr,
qse_skadsize(&log->t.syslog_remote.addr)); qse_skadsize(&log->t.syslog_remote.addr));
}
#endif #endif
} }
#endif
#endif
} }
done: done: