defined MIO_LOG_GUARDED

This commit is contained in:
hyung-hwan 2021-07-17 15:17:07 +00:00
parent 08721ab0c6
commit b27753626d
4 changed files with 66 additions and 11 deletions

View File

@ -1553,13 +1553,26 @@ mio_ooi_t mio_logbfmtv (mio_t* mio, mio_bitmask_t mask, const mio_bch_t* fmt, va
fo.putbchars = log_bcs;
fo.putuchars = log_ucs;
if (mio->option.log_mask & MIO_LOG_GUARDED)
{
mio_sys_locklog (mio);
x = fmt_outv(&fo, ap);
if (mio->log.len > 0 && mio->log.ptr[mio->log.len - 1] == MIO_EOL)
{
MIO_SYS_WRITE_LOG (mio, mio->log.last_mask, mio->log.ptr, mio->log.len);
mio->log.len = 0;
}
mio_sys_unlocklog (mio);
}
else
{
x = fmt_outv(&fo, ap);
if (mio->log.len > 0 && mio->log.ptr[mio->log.len - 1] == MIO_EOL)
{
MIO_SYS_WRITE_LOG (mio, mio->log.last_mask, mio->log.ptr, mio->log.len);
mio->log.len = 0;
}
}
return (x <= -1)? -1: fo.count;
}
@ -1607,13 +1620,26 @@ mio_ooi_t mio_logufmtv (mio_t* mio, mio_bitmask_t mask, const mio_uch_t* fmt, va
fo.putbchars = log_bcs;
fo.putuchars = log_ucs;
if (mio->option.log_mask & MIO_LOG_GUARDED)
{
mio_sys_locklog (mio);
x = fmt_outv(&fo, ap);
if (mio->log.len > 0 && mio->log.ptr[mio->log.len - 1] == MIO_EOL)
{
MIO_SYS_WRITE_LOG (mio, mio->log.last_mask, mio->log.ptr, mio->log.len);
mio->log.len = 0;
}
mio_sys_unlocklog (mio);
}
else
{
x = fmt_outv(&fo, ap);
if (mio->log.len > 0 && mio->log.ptr[mio->log.len - 1] == MIO_EOL)
{
MIO_SYS_WRITE_LOG (mio, mio->log.last_mask, mio->log.ptr, mio->log.len);
mio->log.len = 0;
}
}
return (x <= -1)? -1: fo.count;
}

View File

@ -542,6 +542,7 @@ enum mio_log_mask_t
MIO_LOG_ALL_LEVELS = (MIO_LOG_DEBUG | MIO_LOG_INFO | MIO_LOG_WARN | MIO_LOG_ERROR | MIO_LOG_FATAL),
MIO_LOG_ALL_TYPES = (MIO_LOG_UNTYPED | MIO_LOG_CORE | MIO_LOG_DEV | MIO_LOG_TIMER),
MIO_LOG_GUARDED = (1u << 13), /* make logging thread-safe */
MIO_LOG_STDOUT = (1u << 14), /* write log messages to stdout without timestamp. MIO_LOG_STDOUT wins over MIO_LOG_STDERR. */
MIO_LOG_STDERR = (1u << 15) /* write log messages to stderr without timestamp. */
};

View File

@ -319,7 +319,6 @@ void mio_sys_writelog (mio_t* mio, mio_bitmask_t mask, const mio_ooch_t* msg, mi
int mio_sys_initlog (mio_t* mio)
{
mio_sys_log_t* log = &mio->sysdep->log;
mio_bitmask_t logmask;
/*mio_oow_t pathlen;*/
/* TODO: */
@ -340,9 +339,13 @@ int mio_sys_initlog (mio_t* mio)
if (isatty(log->fd)) log->fd_flag |= LOGFD_TTY;
#endif
/*
mio_bitmask_t logmask;
logmask = MIO_LOG_ALL_TYPES | MIO_LOG_ALL_LEVELS;
mio_setoption (mio, MIO_LOG_MASK, &logmask);
*/
pthread_mutex_init (&log->mtx, MIO_NULL);
return 0;
}
@ -350,6 +353,8 @@ void mio_sys_finilog (mio_t* mio)
{
mio_sys_log_t* log = &mio->sysdep->log;
pthread_mutex_destroy (&log->mtx);
if ((log->fd_flag & LOGFD_OPENED_HERE) && log->fd >= 0)
{
close (log->fd);
@ -357,3 +362,13 @@ void mio_sys_finilog (mio_t* mio)
log->fd_flag = 0;
}
}
void mio_sys_locklog (mio_t* mio)
{
pthread_mutex_lock (&mio->sysdep->log.mtx);
}
void mio_sys_unlocklog (mio_t* mio)
{
pthread_mutex_unlock (&mio->sysdep->log.mtx);
}

View File

@ -41,6 +41,8 @@
# error NO SUPPORTED MULTIPLEXER
#endif
#include <pthread.h>
/* -------------------------------------------------------------------------- */
#if defined(USE_POLL)
@ -87,6 +89,8 @@ struct mio_sys_log_t
mio_bch_t buf[4096];
mio_oow_t len;
} out;
pthread_mutex_t mtx;
};
typedef struct mio_sys_log_t mio_sys_log_t;
@ -133,6 +137,15 @@ void mio_sys_finilog (
mio_t* mio
);
void mio_sys_locklog (
mio_t* mio
);
void mio_sys_locklog (
mio_t* mio
);
int mio_sys_initmux (
mio_t* mio
);