diff --git a/mio/lib/fmt.c b/mio/lib/fmt.c index 2ca4e82..c608fb6 100644 --- a/mio/lib/fmt.c +++ b/mio/lib/fmt.c @@ -1553,12 +1553,25 @@ 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; - x = fmt_outv(&fo, ap); - - if (mio->log.len > 0 && mio->log.ptr[mio->log.len - 1] == MIO_EOL) + if (mio->option.log_mask & MIO_LOG_GUARDED) { - MIO_SYS_WRITE_LOG (mio, mio->log.last_mask, mio->log.ptr, mio->log.len); - mio->log.len = 0; + 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,12 +1620,25 @@ 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; - x = fmt_outv(&fo, ap); - - if (mio->log.len > 0 && mio->log.ptr[mio->log.len - 1] == MIO_EOL) + if (mio->option.log_mask & MIO_LOG_GUARDED) { - MIO_SYS_WRITE_LOG (mio, mio->log.last_mask, mio->log.ptr, mio->log.len); - mio->log.len = 0; + 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; diff --git a/mio/lib/mio.h b/mio/lib/mio.h index ee742df..3eb32d5 100644 --- a/mio/lib/mio.h +++ b/mio/lib/mio.h @@ -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. */ }; diff --git a/mio/lib/sys-log.c b/mio/lib/sys-log.c index 8f7012d..b162f26 100644 --- a/mio/lib/sys-log.c +++ b/mio/lib/sys-log.c @@ -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); +} diff --git a/mio/lib/sys-prv.h b/mio/lib/sys-prv.h index d9c2ca0..46505b8 100644 --- a/mio/lib/sys-prv.h +++ b/mio/lib/sys-prv.h @@ -41,6 +41,8 @@ # error NO SUPPORTED MULTIPLEXER #endif +#include + /* -------------------------------------------------------------------------- */ #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 );