coding for logging support in QSE::App

This commit is contained in:
hyung-hwan 2019-11-08 08:33:38 +00:00
parent eaad08930c
commit 3130a85700
5 changed files with 47 additions and 41 deletions

View File

@ -184,6 +184,19 @@ public:
void setLogMask (int mask) { this->_log.mask = mask; } void setLogMask (int mask) { this->_log.mask = mask; }
int getLogMask () const { return this->_log.mask; } int getLogMask () const { return this->_log.mask; }
/*
void setLogTarget (int flags, const qse_log_target_t& target)
{
if (this->_log.logger) qse_log_settarget (this->_log.logger, flags, &target);
}
void setLogOption (int oflags)
{
qse_log_setoption (&this->_log.logger, oflags);
}
*/
void logfmt (int mask, const qse_char_t* fmt, ...) void logfmt (int mask, const qse_char_t* fmt, ...)
{ {
va_list ap; va_list ap;
@ -216,7 +229,7 @@ private:
qse_cmgr_t* _cmgr; qse_cmgr_t* _cmgr;
struct log_t struct log_t
{ {
log_t (App* app): mask(0), last_mask(0), len(0), mtx(app->getMmgr()), logger(QSE_NULL) log_t (App* app): mask(0), last_mask(0), len(0), mtx(app->getMmgr())
{ {
} }
@ -224,7 +237,7 @@ private:
qse_size_t len; qse_size_t len;
qse_char_t buf[256]; qse_char_t buf[256];
QSE::Mutex mtx; QSE::Mutex mtx;
qse_log_t* logger; qse_log_t logger;
} _log; } _log;
static int set_signal_handler_no_mutex (int sig, SignalHandler sighr); static int set_signal_handler_no_mutex (int sig, SignalHandler sighr);

View File

@ -356,27 +356,27 @@ int qse_gmtime (const qse_ntime_t* nt, qse_btime_t* bt)
/* TODO: remove dependency on gmtime/gmtime_r */ /* TODO: remove dependency on gmtime/gmtime_r */
#if defined(_WIN32) #if defined(_WIN32)
tm = gmtime (&t); tm = gmtime(&t);
#elif defined(__OS2__) #elif defined(__OS2__)
# if defined(__WATCOMC__) # if defined(__WATCOMC__)
struct tm btm; struct tm btm;
tm = _gmtime (&t, &btm); tm = _gmtime(&t, &btm);
# else # else
# error Please support other compilers # error Please support other compilers
# endif # endif
#elif defined(__DOS__) #elif defined(__DOS__)
# if defined(__WATCOMC__) # if defined(__WATCOMC__)
struct tm btm; struct tm btm;
tm = _gmtime (&t, &btm); tm = _gmtime(&t, &btm);
# else # else
# error Please support other compilers # error Please support other compilers
# endif # endif
#elif defined(HAVE_GMTIME_R) #elif defined(HAVE_GMTIME_R)
struct tm btm; struct tm btm;
tm = gmtime_r (&t, &btm); tm = gmtime_r(&t, &btm);
#else #else
/* thread unsafe */ /* thread unsafe */
tm = gmtime_r (&t); tm = gmtime_r(&t);
#endif #endif
if (tm == QSE_NULL) return -1; if (tm == QSE_NULL) return -1;
@ -409,27 +409,27 @@ int qse_localtime (const qse_ntime_t* nt, qse_btime_t* bt)
/* TODO: remove dependency on localtime/localtime_r */ /* TODO: remove dependency on localtime/localtime_r */
#if defined(_WIN32) #if defined(_WIN32)
tm = localtime (&t); tm = localtime(&t);
#elif defined(__OS2__) #elif defined(__OS2__)
# if defined(__WATCOMC__) # if defined(__WATCOMC__)
struct tm btm; struct tm btm;
tm = _localtime (&t, &btm); tm = _localtime(&t, &btm);
# else # else
# error Please support other compilers # error Please support other compilers
# endif # endif
#elif defined(__DOS__) #elif defined(__DOS__)
# if defined(__WATCOMC__) # if defined(__WATCOMC__)
struct tm btm; struct tm btm;
tm = _localtime (&t, &btm); tm = _localtime(&t, &btm);
# else # else
# error Please support other compilers # error Please support other compilers
# endif # endif
#elif defined(HAVE_LOCALTIME_R) #elif defined(HAVE_LOCALTIME_R)
struct tm btm; struct tm btm;
tm = localtime_r (&t, &btm); tm = localtime_r(&t, &btm);
#else #else
/* thread unsafe */ /* thread unsafe */
tm = localtime (&t); tm = localtime(&t);
#endif #endif
if (tm == QSE_NULL) return -1; if (tm == QSE_NULL) return -1;
@ -448,6 +448,18 @@ int qse_localtime (const qse_ntime_t* nt, qse_btime_t* bt)
bt->gmtoff = tm->tm_gmtoff; bt->gmtoff = tm->tm_gmtoff;
#elif defined(HAVE_STRUCT_TM___TM_GMTOFF) #elif defined(HAVE_STRUCT_TM___TM_GMTOFF)
bt->gmtoff = tm->__tm_gmtoff; bt->gmtoff = tm->__tm_gmtoff;
#elif defined(_WIN32)
{
TIME_ZONE_INFORMATION tzi;
if (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_INVALID)
{
bt->gmtoff = -((int)tiz.Bias * 60);
}
else
{
bt->gmtoff = QSE_TYPE_MIN(int); /* unknown */
}
}
#else #else
bt->gmtoff = QSE_TYPE_MIN(int); /* unknown */ bt->gmtoff = QSE_TYPE_MIN(int); /* unknown */
#endif #endif

View File

@ -76,6 +76,7 @@ protected:
App::App (Mmgr* mmgr) QSE_CPP_NOEXCEPT: Mmged(mmgr), _prev_app(QSE_NULL), _next_app(QSE_NULL), _guarded_child_pid(-1), _log(this) App::App (Mmgr* mmgr) QSE_CPP_NOEXCEPT: Mmged(mmgr), _prev_app(QSE_NULL), _next_app(QSE_NULL), _guarded_child_pid(-1), _log(this)
{ {
this->_cmgr = qse_getdflcmgr(); this->_cmgr = qse_getdflcmgr();
qse_log_init(&this->_log.logger, this->getMmgr(), QSE_T("app"), 0, QSE_NULL);
SigScopedMutexLocker sml(g_app_mutex); SigScopedMutexLocker sml(g_app_mutex);
if (!g_app_top) if (!g_app_top)
@ -115,12 +116,7 @@ App::~App () QSE_CPP_NOEXCEPT
g_app_top = this->_next_app; g_app_top = this->_next_app;
} }
qse_log_fini (&this->_log.logger);
if (this->_log.logger)
{
qse_log_close (this->_log.logger);
this->_log.logger = QSE_NULL;
}
} }
int App::daemonize (bool chdir_to_root, int fork_count, bool root_only) QSE_CPP_NOEXCEPT int App::daemonize (bool chdir_to_root, int fork_count, bool root_only) QSE_CPP_NOEXCEPT
@ -608,25 +604,10 @@ void App::logfmtv (int mask, const qse_char_t* fmt, va_list ap)
// default log message output implementation // default log message output implementation
void App::log_write (int mask, const qse_char_t* msg, qse_size_t len) void App::log_write (int mask, const qse_char_t* msg, qse_size_t len)
{ {
if (!this->_log.logger)
{
qse_log_target_t target;
// TODO: ...
QSE_MEMSET (&target, 0, QSE_SIZEOF(target));
target.file = QSE_T("app.log");
this->_log.logger = qse_log_open(this->getMmgr(), 0, QSE_T("app"), QSE_LOG_INCLUDE_PID | QSE_LOG_ALL_PRIORITIES | QSE_LOG_FILE | QSE_LOG_CONSOLE, &target);
}
if (this->_log.logger)
{
// TOOD: add qse_log_write() which doesn't do formatting??
// the last character is \n. qse_log_report() knows to terminate a line. so exclude it from reporting // the last character is \n. qse_log_report() knows to terminate a line. so exclude it from reporting
qse_log_report (this->_log.logger, QSE_NULL, QSE_LOG_LOCAL0 | QSE_LOG_INFO, QSE_T("%.*js"), (int)(len - 1), msg); qse_log_report (&this->_log.logger, QSE_NULL, QSE_LOG_LOCAL0 | QSE_LOG_INFO, QSE_T("%.*js"), (int)(len - 1), msg);
}
} }
///////////////////////////////// /////////////////////////////////
QSE_END_NAMESPACE(QSE) QSE_END_NAMESPACE(QSE)
///////////////////////////////// /////////////////////////////////

View File

@ -556,7 +556,7 @@ void qse_log_reportv (qse_log_t* log, const qse_char_t* ident, int pri, const qs
if (log->flags & QSE_LOG_CONSOLE) if (log->flags & QSE_LOG_CONSOLE)
{ {
if (!log->t.console.sio) if (!log->t.console.sio)
log->t.console.sio = qse_sio_openstd (log->mmgr, 0, QSE_SIO_STDERR, QSE_SIO_APPEND | QSE_SIO_IGNOREMBWCERR); log->t.console.sio = qse_sio_openstd(log->mmgr, 0, QSE_SIO_STDERR, QSE_SIO_APPEND | QSE_SIO_IGNOREMBWCERR);
if (log->t.console.sio) if (log->t.console.sio)
{ {
va_list xap; va_list xap;
@ -568,7 +568,7 @@ void qse_log_reportv (qse_log_t* log, const qse_char_t* ident, int pri, const qs
if (log->flags & QSE_LOG_FILE) if (log->flags & QSE_LOG_FILE)
{ {
if (!log->t.file.sio) if (!log->t.file.sio)
log->t.file.sio = qse_sio_open (log->mmgr, 0, log->t.file.path, QSE_SIO_CREATE | QSE_SIO_APPEND | QSE_SIO_IGNOREMBWCERR); log->t.file.sio = qse_sio_open(log->mmgr, 0, log->t.file.path, QSE_SIO_CREATE | QSE_SIO_APPEND | QSE_SIO_IGNOREMBWCERR);
if (log->t.file.sio) if (log->t.file.sio)
{ {
va_list xap; va_list xap;