defined MIO_FEATURE_LOG_WRITER

This commit is contained in:
hyung-hwan 2021-07-19 04:57:30 +00:00
parent d1d7929830
commit a2904ad30d
3 changed files with 80 additions and 45 deletions

View File

@ -265,6 +265,10 @@ int mio_setoption (mio_t* mio, mio_option_t id, const void* value)
case MIO_LOG_MAXCAPA:
mio->option.log_maxcapa = *(mio_oow_t*)value;
return 0;
case MIO_LOG_WRITER:
mio->option.log_writer = (mio_log_writer_t)value;
return 0;
}
mio_seterrnum (mio, MIO_EINVAL);
@ -286,6 +290,10 @@ int mio_getoption (mio_t* mio, mio_option_t id, void* value)
case MIO_LOG_MAXCAPA:
*(mio_oow_t*)value = mio->option.log_maxcapa;
return 0;
case MIO_LOG_WRITER:
*(mio_log_writer_t*)value = mio->option.log_writer;
return 0;
};
mio_seterrnum (mio, MIO_EINVAL);

View File

@ -122,10 +122,11 @@ typedef struct mio_errinf_t mio_errinf_t;
enum mio_feature_t
{
MIO_FEATURE_LOG = ((mio_bitmask_t)1 << 0),
MIO_FEATURE_MUX = ((mio_bitmask_t)1 << 1),
MIO_FEATURE_MUX = ((mio_bitmask_t)1 << 0),
MIO_FEATURE_LOG = ((mio_bitmask_t)1 << 1),
MIO_FEATURE_LOG_WRITER = ((mio_bitmask_t)1 << 2),
MIO_FEATURE_ALL = (MIO_FEATURE_LOG | MIO_FEATURE_MUX)
MIO_FEATURE_ALL = (MIO_FEATURE_MUX | MIO_FEATURE_LOG | MIO_FEATURE_LOG_WRITER)
};
typedef enum mio_feature_t mio_feature_t;
@ -133,11 +134,11 @@ enum mio_option_t
{
MIO_TRAIT,
MIO_LOG_MASK,
MIO_LOG_MAXCAPA
MIO_LOG_MAXCAPA,
MIO_LOG_WRITER
};
typedef enum mio_option_t mio_option_t;
enum mio_stopreq_t
{
MIO_STOPREQ_NONE = 0,
@ -148,6 +149,15 @@ typedef enum mio_stopreq_t mio_stopreq_t;
/* ========================================================================= */
typedef int (*mio_log_writer_t) (
mio_t* mio,
mio_bitmask_t mask,
const mio_bch_t* dptr,
mio_oow_t dlen
);
/* ========================================================================= */
#define MIO_TMRIDX_INVALID ((mio_tmridx_t)-1)
typedef mio_oow_t mio_tmridx_t;
@ -653,6 +663,7 @@ struct mio_t
mio_bitmask_t trait;
mio_bitmask_t log_mask;
mio_oow_t log_maxcapa;
mio_log_writer_t log_writer;
} option;
struct

View File

@ -88,8 +88,12 @@ enum logfd_flag_t
LOGFD_OPENED_HERE = (1 << 1)
};
static int write_all (int fd, const mio_bch_t* ptr, mio_oow_t len)
static int write_all (mio_t* mio, int fd, mio_bitmask_t mask, const mio_bch_t* ptr, mio_oow_t len)
{
if (mio->option.log_writer) return mio->option.log_writer(mio, mask, ptr, len);
if (MIO_UNLIKELY(fd <= -1)) return 0; /* MIO_FEATURE_LOG_WRITER is off but mio->option.log_write is not set */
while (len > 0)
{
mio_ooi_t wr;
@ -122,7 +126,7 @@ static int write_all (int fd, const mio_bch_t* ptr, mio_oow_t len)
return 0;
}
static int write_log (mio_t* mio, int fd, const mio_bch_t* ptr, mio_oow_t len)
static int write_log (mio_t* mio, int fd, mio_bitmask_t mask, const mio_bch_t* ptr, mio_oow_t len)
{
mio_sys_log_t* log = &mio->sysdep->log;
@ -143,7 +147,7 @@ static int write_log (mio_t* mio, int fd, const mio_bch_t* ptr, mio_oow_t len)
if (log->out.len >= MIO_COUNTOF(log->out.buf))
{
int n;
n = write_all(fd, log->out.buf, log->out.len);
n = write_all(mio, fd, mask, log->out.buf, log->out.len);
log->out.len = 0;
if (n <= -1) return -1;
}
@ -155,7 +159,7 @@ static int write_log (mio_t* mio, int fd, const mio_bch_t* ptr, mio_oow_t len)
rcapa = MIO_COUNTOF(log->out.buf);
if (len >= rcapa)
{
if (write_all(fd, ptr, rcapa) <= -1) return -1;
if (write_all(mio, fd, mask, ptr, rcapa) <= -1) return -1;
ptr += rcapa;
len -= rcapa;
}
@ -173,12 +177,12 @@ static int write_log (mio_t* mio, int fd, const mio_bch_t* ptr, mio_oow_t len)
return 0;
}
static void flush_log (mio_t* mio, int fd)
static void flush_log (mio_t* mio, int fd, mio_bitmask_t mask)
{
mio_sys_log_t* log = &mio->sysdep->log;
if (log->out.len > 0)
{
write_all (fd, log->out.buf, log->out.len);
write_all (mio, fd, mask, log->out.buf, log->out.len);
log->out.len = 0;
}
}
@ -188,8 +192,10 @@ void mio_sys_writelog (mio_t* mio, mio_bitmask_t mask, const mio_ooch_t* msg, mi
mio_sys_log_t* log = &mio->sysdep->log;
mio_bch_t buf[256];
mio_oow_t ucslen, bcslen, msgidx;
int n, logfd;
int n, logfd = -1;
if (mio->_features & MIO_FEATURE_LOG_WRITER)
{
if (mask & MIO_LOG_STDERR)
{
logfd = 2;
@ -203,6 +209,7 @@ void mio_sys_writelog (mio_t* mio, mio_bitmask_t mask, const mio_ooch_t* msg, mi
if (logfd <= -1) return;
}
}
}
/* TODO: beautify the log message.
* do classification based on mask. */
@ -260,14 +267,14 @@ void mio_sys_writelog (mio_t* mio, mio_bitmask_t mask, const mio_ooch_t* msg, mi
tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
}
#endif
write_log (mio, logfd, ts, tslen);
write_log (mio, logfd, mask, ts, tslen);
}
if (logfd == log->fd && (log->fd_flag & LOGFD_TTY))
{
if (mask & MIO_LOG_FATAL) write_log (mio, logfd, "\x1B[1;31m", 7);
else if (mask & MIO_LOG_ERROR) write_log (mio, logfd, "\x1B[1;32m", 7);
else if (mask & MIO_LOG_WARN) write_log (mio, logfd, "\x1B[1;33m", 7);
if (mask & MIO_LOG_FATAL) write_log (mio, logfd, mask, "\x1B[1;31m", 7);
else if (mask & MIO_LOG_ERROR) write_log (mio, logfd, mask, "\x1B[1;32m", 7);
else if (mask & MIO_LOG_WARN) write_log (mio, logfd, mask, "\x1B[1;33m", 7);
}
#if defined(MIO_OOCH_IS_UCH)
@ -289,7 +296,7 @@ void mio_sys_writelog (mio_t* mio, mio_bitmask_t mask, const mio_ooch_t* msg, mi
MIO_ASSERT (mio, ucslen > 0); /* if this fails, the buffer size must be increased */
/* attempt to write all converted characters */
if (write_log(mio, logfd, buf, bcslen) <= -1) break;
if (write_log(mio, logfd, mask, buf, bcslen) <= -1) break;
if (n == 0) break;
else
@ -305,23 +312,24 @@ void mio_sys_writelog (mio_t* mio, mio_bitmask_t mask, const mio_ooch_t* msg, mi
}
}
#else
write_log (mio, logfd, msg, len);
write_log (mio, logfd, mask, msg, len);
#endif
if (logfd == log->fd && (log->fd_flag & LOGFD_TTY))
{
if (mask & (MIO_LOG_FATAL | MIO_LOG_ERROR | MIO_LOG_WARN)) write_log (mio, logfd, "\x1B[0m", 4);
if (mask & (MIO_LOG_FATAL | MIO_LOG_ERROR | MIO_LOG_WARN)) write_log (mio, logfd, mask, "\x1B[0m", 4);
}
flush_log (mio, logfd);
flush_log (mio, logfd, mask);
}
int mio_sys_initlog (mio_t* mio)
{
mio_sys_log_t* log = &mio->sysdep->log;
/*mio_oow_t pathlen;*/
/* TODO: */
if (mio->_features & MIO_FEATURE_LOG_WRITER)
{
/* TODO: different file? */
#define LOG_FILE "/dev/stderr"
log->fd = open(LOG_FILE, O_CREAT | O_WRONLY | O_APPEND , 0644);
if (log->fd == -1)
@ -338,6 +346,11 @@ int mio_sys_initlog (mio_t* mio)
#if defined(HAVE_ISATTY)
if (isatty(log->fd)) log->fd_flag |= LOGFD_TTY;
#endif
}
else
{
log->fd = -1;
}
pthread_mutex_init (&log->mtx, MIO_NULL);
return 0;
@ -349,6 +362,8 @@ void mio_sys_finilog (mio_t* mio)
pthread_mutex_destroy (&log->mtx);
if (mio->_features & MIO_FEATURE_LOG_WRITER)
{
if ((log->fd_flag & LOGFD_OPENED_HERE) && log->fd >= 0)
{
close (log->fd);
@ -356,6 +371,7 @@ void mio_sys_finilog (mio_t* mio)
log->fd_flag = 0;
}
}
}
void mio_sys_locklog (mio_t* mio)
{