got logging working in QSE::App()

This commit is contained in:
2019-11-10 08:19:15 +00:00
parent 4a1e4858a7
commit 4e43dbe5b9
6 changed files with 88 additions and 123 deletions

View File

@ -76,7 +76,10 @@ protected:
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();
qse_log_init(&this->_log.logger, this->getMmgr(), QSE_T("app"), 0, QSE_NULL);
// instead of relying on the logger's priority masking, the class will do its own masking.
// set the prioriy mask to QSE_LOG_ALL_PRIORITIES.
qse_log_init(&this->_log.logger, this->getMmgr(), QSE_T(""), QSE_LOG_ALL_PRIORITIES, QSE_NULL);
SigScopedMutexLocker sml(g_app_mutex);
if (!g_app_top)
@ -119,6 +122,12 @@ App::~App () QSE_CPP_NOEXCEPT
qse_log_fini (&this->_log.logger);
}
void App::setName (const qse_char_t* name) QSE_CPP_NOEXCEPT
{
QSE::Named<32>::setName (name);
qse_log_setident (&this->_log.logger, name);
}
int App::daemonize (bool chdir_to_root, int fork_count, bool root_only) QSE_CPP_NOEXCEPT
{
if (root_only && QSE_GETEUID() != 0) return -1;
@ -542,14 +551,14 @@ int App::put_char_to_log_buf (qse_char_t c, void* ctx)
app->_log.buf[app->_log.len++] = '\n';
}
app->log_write (app->_log.last_mask, app->_log.buf, app->_log.len);
app->log_write (app->_log.last_pri, app->_log.buf, app->_log.len);
app->_log.len = 0;
}
app->_log.buf[app->_log.len++] = c;
if (c == QSE_T('\n'))
{
app->log_write (app->_log.last_mask, app->_log.buf, app->_log.len);
app->log_write (app->_log.last_pri, app->_log.buf, app->_log.len);
app->_log.len = 0;
}
@ -568,18 +577,18 @@ static int mbs_to_wcs (const qse_mchar_t* mbs, qse_size_t* mbslen, qse_wchar_t*
return qse_mbsntowcsnwithcmgr(mbs, mbslen, wcs, wcslen, app->getCmgr());
}
void App::logfmtv (int mask, const qse_char_t* fmt, va_list ap)
void App::logfmtv (qse_log_priority_flag_t pri, const qse_char_t* fmt, va_list ap)
{
/*if (this->threaded)*/ this->_log.mtx.lock ();
if (this->_log.len > 0 && this->_log.last_mask != mask)
if (this->_log.len > 0 && this->_log.last_pri != pri)
{
if (this->_log.buf[this->_log.len - 1] != QSE_T('\n'))
{
// no line ending - append a line terminator
this->_log.buf[this->_log.len++] = QSE_T('\n');
}
this->log_write (this->_log.last_mask, this->_log.buf, this->_log.len);
this->log_write (this->_log.last_pri, this->_log.buf, this->_log.len);
this->_log.len = 0;
}
@ -595,17 +604,17 @@ void App::logfmtv (int mask, const qse_char_t* fmt, va_list ap)
fo.conv = mbs_to_wcs;
#endif
this->_log.last_mask = mask;
this->_log.last_pri = pri;
qse_fmtout(fmt, &fo, ap);
/*if (this->threaded)*/ this->_log.mtx.unlock ();
}
// default log message output implementation
void App::log_write (int mask, const qse_char_t* msg, qse_size_t len)
void App::log_write (qse_log_priority_flag_t pri, const qse_char_t* msg, qse_size_t len)
{
// 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, pri, QSE_T("%.*js"), (int)(len - 1), msg);
}
/////////////////////////////////

View File

@ -165,37 +165,7 @@ static struct syslog_fac_info_t __syslog_fac_info[] =
static QSE_INLINE int get_active_priority_bits (int flags)
{
int priority_bits = 0;
if (flags & QSE_LOG_MASKED_PRIORITY)
{
priority_bits = flags & QSE_LOG_MASK_PRIORITY;
}
else
{
int pri = flags & QSE_LOG_MASK_PRIORITY;
switch (pri)
{
case QSE_LOG_DEBUG:
priority_bits |= QSE_LOG_DEBUG;
case QSE_LOG_INFO:
priority_bits |= QSE_LOG_INFO;
case QSE_LOG_NOTICE:
priority_bits |= QSE_LOG_NOTICE;
case QSE_LOG_WARNING:
priority_bits |= QSE_LOG_WARNING;
case QSE_LOG_ERROR:
priority_bits |= QSE_LOG_ERROR;
case QSE_LOG_CRITICAL:
priority_bits |= QSE_LOG_CRITICAL;
case QSE_LOG_ALERT:
priority_bits |= QSE_LOG_ALERT;
case QSE_LOG_PANIC:
priority_bits |= QSE_LOG_PANIC;
}
}
return priority_bits;
return flags & QSE_LOG_MASK_PRIORITY;
}
qse_log_t* qse_log_open (qse_mmgr_t* mmgr, qse_size_t xtnsize, const qse_char_t* ident, int potflags, const qse_log_target_data_t* target_data)
@ -269,7 +239,7 @@ int qse_log_init (qse_log_t* log, qse_mmgr_t* mmgr, const qse_char_t* ident, int
#else
log->syslog_facility = QSE_LOG_USER;
#endif
return 0;
}
@ -451,7 +421,7 @@ void qse_log_setoption (qse_log_t* log, int option)
void qse_log_setpriority (qse_log_t* log, int priority)
{
log->flags = (log->flags & (QSE_LOG_MASK_TARGET | QSE_LOG_MASK_OPTION)) | (priority & QSE_LOG_MASK_PRIORITY);
log->active_priority_bits = get_active_priority_bits (log->flags);
log->active_priority_bits = get_active_priority_bits(log->flags);
}
void qse_log_setsyslogfacility (qse_log_t* log, qse_log_facility_t facility)
@ -517,14 +487,7 @@ void qse_log_reportv (qse_log_t* log, const qse_char_t* ident, int pri, const qs
if ((log->flags & QSE_LOG_MASK_TARGET) == 0) return; /* no target */
if (log->flags & QSE_LOG_MASKED_PRIORITY)
{
if (!(pri & (log->flags & QSE_LOG_MASK_PRIORITY))) return;
}
else
{
if (pri > (log->flags & QSE_LOG_MASK_PRIORITY)) return;
}
if (!(pri & (log->flags & QSE_LOG_MASK_PRIORITY))) return; /* excluded priority*/
if (qse_gettime(&now) || qse_localtime(&now, &cnow) <= -1) return;