added mio_sys_resetlog()

This commit is contained in:
hyung-hwan 2021-07-19 09:27:44 +00:00
parent a2904ad30d
commit 42c533c508
6 changed files with 158 additions and 32 deletions

View File

@ -217,6 +217,7 @@ void* thr_func (void* arg)
mio_t* mio = MIO_NULL;
mio_svc_htts_t* htts = MIO_NULL;
mio_dev_sck_bind_t htts_bind_info;
int htts_no = -1;
mio = mio_open(MIO_NULL, 0, MIO_NULL, MIO_FEATURE_ALL, 512, MIO_NULL);
if (!mio)
@ -225,6 +226,8 @@ void* thr_func (void* arg)
goto oops;
}
mio_setoption (mio, MIO_LOG_TARGET_B, "/dev/stderr");
memset (&htts_bind_info, 0, MIO_SIZEOF(htts_bind_info));
if (g_reuse_port)
{
@ -243,15 +246,23 @@ void* thr_func (void* arg)
}
pthread_mutex_lock (&g_htts_mutex);
g_htts[g_htts_no] = htts;
printf ("starting the loop for %d\n", g_htts_no);
htts_no = g_htts_no;
g_htts[htts_no] = htts;
g_htts_no = (g_htts_no + 1) % g_num_thrs;
pthread_mutex_unlock (&g_htts_mutex);
printf ("entering the loop for %d\n", htts_no);
mio_loop (mio);
printf ("exiting the loop for %d\n", htts_no);
oops:
if (htts) mio_svc_htts_stop (htts);
pthread_mutex_lock (&g_htts_mutex);
if (htts)
{
mio_svc_htts_stop (htts);
g_htts[htts_no] = MIO_NULL;
}
pthread_mutex_unlock (&g_htts_mutex);
if (mio) mio_close (mio);
pthread_exit (MIO_NULL);
@ -524,6 +535,7 @@ int main (int argc, char* argv[])
pthread_t t[MAX_NUM_THRS];
mio_oow_t i;
struct sigaction sigact;
int xret = -1;
// TODO: use getopt() or something similar
for (i = 1; i < argc; )
@ -577,6 +589,8 @@ int main (int argc, char* argv[])
goto oops;
}
mio_setoption (mio, MIO_LOG_TARGET_B, "/dev/stderr");
g_mio = mio;
for (i = 0; i < g_num_thrs; i++)
@ -588,23 +602,32 @@ int main (int argc, char* argv[])
if (add_listener(mio, "[::]:9987") <= -1 ||
add_listener(mio, "0.0.0.0:9987") <= -1) goto oops;
printf ("starting the main loop\n");
printf ("entering the main loop\n");
mio_loop (mio);
printf ("exiting the main loop\n");
/* close all threaded mios here */
printf ("TERMINATING..NORMALLY \n");
memset (&sigact, 0, MIO_SIZEOF(sigact));
sigact.sa_handler = SIG_IGN;
sigaction (SIGINT, &sigact, MIO_NULL);
mio_close (mio);
return 0;
xret = 0;
oops:
printf ("TERMINATING..ABNORMALLY \n");
memset (&sigact, 0, MIO_SIZEOF(sigact));
sigact.sa_handler = SIG_IGN;
sigaction (SIGINT, &sigact, MIO_NULL);
pthread_mutex_lock (&g_htts_mutex);
for (i = 0; i < g_num_thrs; i++)
{
if (g_htts[i]) mio_stop (mio_svc_htts_getmio(g_htts[i]), MIO_STOPREQ_TERMINATION);
}
pthread_mutex_unlock (&g_htts_mutex);
for (i = 0; i < g_num_thrs; i++)
{
pthread_join (t[i], MIO_NULL);
}
if (mio) mio_close (mio);
return -1;
return xret;
}

View File

@ -172,6 +172,10 @@ mio_errnum_t mio_sys_syserrstrb (
mio_oow_t len
);
void mio_sys_resetlog (
mio_t* mio
);
void mio_sys_writelog (
mio_t* mio,
mio_bitmask_t mask,

View File

@ -247,7 +247,23 @@ void mio_fini (mio_t* mio)
mio_sys_fini (mio); /* finalize the system dependent data */
mio_freemem (mio, mio->log.ptr);
if (mio->log.ptr)
{
mio_freemem (mio, mio->log.ptr);
mio->log.ptr = MIO_NULL;
}
if (mio->option.log_target_u)
{
mio_freemem (mio, mio->option.log_target_u);
mio->option.log_target_u = MIO_NULL;
}
if (mio->option.log_target_b)
{
mio_freemem (mio, mio->option.log_target_b);
mio->option.log_target_b = MIO_NULL;
}
}
int mio_setoption (mio_t* mio, mio_option_t id, const void* value)
@ -266,6 +282,50 @@ int mio_setoption (mio_t* mio, mio_option_t id, const void* value)
mio->option.log_maxcapa = *(mio_oow_t*)value;
return 0;
case MIO_LOG_TARGET_B:
{
mio_bch_t* v1;
mio_uch_t* v2;
v1 = mio_dupbcstr(mio, value, MIO_NULL);
if (MIO_UNLIKELY(!v1)) return -1;
v2 = mio_dupbtoucstr(mio, value, MIO_NULL, 1);
if (MIO_UNLIKELY(!v2))
{
mio_freemem (mio, v1);
return -1;
}
mio->option.log_target_u = v2;
mio->option.log_target_b = v1;
mio_sys_resetlog (mio);
return 0;
}
case MIO_LOG_TARGET_U:
{
mio_uch_t* v1;
mio_bch_t* v2;
v1 = mio_dupucstr(mio, value, MIO_NULL);
if (MIO_UNLIKELY(!v1)) return -1;
v2 = mio_duputobcstr(mio, value, MIO_NULL);
if (MIO_UNLIKELY(!v2))
{
mio_freemem (mio, v1);
return -1;
}
mio->option.log_target_u = v1;
mio->option.log_target_b = v2;
mio_sys_resetlog (mio);
return 0;
}
case MIO_LOG_WRITER:
mio->option.log_writer = (mio_log_writer_t)value;
return 0;
@ -291,6 +351,14 @@ int mio_getoption (mio_t* mio, mio_option_t id, void* value)
*(mio_oow_t*)value = mio->option.log_maxcapa;
return 0;
case MIO_LOG_TARGET_B:
*(mio_bch_t**)value = mio->option.log_target_b;
return 0;
case MIO_LOG_TARGET_U:
*(mio_uch_t**)value = mio->option.log_target_u;
return 0;
case MIO_LOG_WRITER:
*(mio_log_writer_t*)value = mio->option.log_writer;
return 0;

View File

@ -135,6 +135,16 @@ enum mio_option_t
MIO_TRAIT,
MIO_LOG_MASK,
MIO_LOG_MAXCAPA,
/* log target for the builtin writer */
MIO_LOG_TARGET_B,
MIO_LOG_TARGET_U,
#if defined(MIO_OOCH_IS_UCH)
# define MIO_LOG_TARGET MIO_LOG_TARGET_U
#else
# define MIO_LOG_TARGET MIO_LOG_TARGET_B
#endif
MIO_LOG_WRITER
};
typedef enum mio_option_t mio_option_t;
@ -663,6 +673,8 @@ struct mio_t
mio_bitmask_t trait;
mio_bitmask_t log_mask;
mio_oow_t log_maxcapa;
mio_uch_t* log_target_u;
mio_bch_t* log_target_b;
mio_log_writer_t log_writer;
} option;

View File

@ -493,7 +493,7 @@ static int dev_sck_kill (mio_dev_t* dev, int force)
}
else
{
MIO_ASSERT (mio, rdev->state == 0);
MIO_ASSERT (mio, (rdev->state & MIO_DEV_SCK_ALL_PROGRESS_BITS) == 0);
MIO_ASSERT (mio, rdev->tmrjob_index == MIO_TMRIDX_INVALID);
if (rdev->on_disconnect) rdev->on_disconnect (rdev);

View File

@ -329,23 +329,8 @@ int mio_sys_initlog (mio_t* mio)
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)
{
/*mio_seterrbfmtwithsyserr (mio, 0, errno, "cannot open log file %hs", LOG_FILE);*/
log->fd = 2;
log->fd_flag = 0;
}
else
{
log->fd_flag |= LOGFD_OPENED_HERE;
}
#if defined(HAVE_ISATTY)
if (isatty(log->fd)) log->fd_flag |= LOGFD_TTY;
#endif
log->fd = 2;
log->fd_flag = 0;
}
else
{
@ -373,6 +358,40 @@ void mio_sys_finilog (mio_t* mio)
}
}
void mio_sys_resetlog (mio_t* mio)
{
mio_sys_log_t* log = &mio->sysdep->log;
if (mio->_features & MIO_FEATURE_LOG_WRITER)
{
int fd;
int fd_flag = 0;
int kill_fd = -1;
fd = open(mio->option.log_target_b, O_CREAT | O_WRONLY | O_APPEND , 0644);
if (fd == -1)
{
fd = 2;
fd_flag = 0;
}
else
{
fd_flag |= LOGFD_OPENED_HERE;
#if defined(HAVE_ISATTY)
if (isatty(fd)) fd_flag |= LOGFD_TTY;
#endif
}
if ((log->fd_flag & LOGFD_OPENED_HERE) && log->fd >= 0) kill_fd = log->fd;
/* swap - risk of race condition depite this late swap */
log->fd = fd;
log->fd_flag = fd_flag;
if (kill_fd >= 0) close (kill_fd);
}
}
void mio_sys_locklog (mio_t* mio)
{
pthread_mutex_lock (&mio->sysdep->log.mtx);