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

@ -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,24 +604,9 @@ 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)