got logging working in QSE::App()

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

View File

@ -40,35 +40,35 @@ public:
{
QSE_ASSERT (name != QSE_NULL);
// WARNING: a long name can be truncated
qse_strxcpy (this->name_buf, QSE_COUNTOF(this->name_buf), name);
qse_strxcpy (this->_name_buf, QSE_COUNTOF(this->_name_buf), name);
}
~Named () QSE_CPP_NOEXCEPT {}
qse_char_t* getName () QSE_CPP_NOEXCEPT
{
return this->name_buf;
return this->_name_buf;
}
const qse_char_t* getName () const QSE_CPP_NOEXCEPT
{
return this->name_buf;
return this->_name_buf;
}
void setName (const qse_char_t* name) QSE_CPP_NOEXCEPT
{
QSE_ASSERT (name != QSE_NULL);
// WARNING: a long name can be truncated
qse_strxcpy (this->name_buf, QSE_COUNTOF(this->name_buf), name);
qse_strxcpy (this->_name_buf, QSE_COUNTOF(this->_name_buf), name);
}
bool isNamed () const QSE_CPP_NOEXCEPT
{
return this->name_buf[0] != QSE_T('\0');
return this->_name_buf[0] != QSE_T('\0');
}
bool isNamed (const qse_char_t* n) const QSE_CPP_NOEXCEPT
{
return qse_strcmp(this->name_buf, n) == 0;
return qse_strcmp(this->_name_buf, n) == 0;
}
enum
@ -77,7 +77,7 @@ public:
};
protected:
qse_char_t name_buf[MAX_NAME_LEN + 1];
qse_char_t _name_buf[MAX_NAME_LEN + 1];
};
QSE_END_NAMESPACE(QSE)

View File

@ -30,31 +30,32 @@
#include <qse/Types.hpp>
#include <qse/Uncopyable.hpp>
#include <qse/cmn/Mmged.hpp>
#include <qse/cmn/Named.hpp>
#include <qse/cmn/Bitset.hpp>
#include <qse/cmn/time.h>
#include <qse/si/Mutex.hpp>
#include <qse/si/os.h>
#include <qse/si/log.h>
#include <stdarg.h>
#include <qse/si/log.h>
/////////////////////////////////
QSE_BEGIN_NAMESPACE(QSE)
/////////////////////////////////
#define QSE_APP_LOG_ENABLED(app, mask) (((app)->getLogMask() & (mask)) == (mask))
#define QSE_APP_LOG0(app, mask, fmt) do { if (QSE_APP_LOG_ENABLED(app, (mask))) (app)->logfmt((mask), fmt); } while(0)
#define QSE_APP_LOG1(app, mask, fmt, a1) do { if (QSE_APP_LOG_ENABLED(app, (mask))) (app)->logfmt((mask), fmt, a1); } while(0)
#define QSE_APP_LOG2(app, mask, fmt, a1, a2) do { if (QSE_APP_LOG_ENABLED(app, (mask))) (app)->logfmt((mask), fmt, a1, a2); } while(0)
#define QSE_APP_LOG3(app, mask, fmt, a1, a2, a3) do { if (QSE_APP_LOG_ENABLED(app, (mask))) (app)->logfmt((mask), fmt, a1, a2, a3); } while(0)
#define QSE_APP_LOG4(app, mask, fmt, a1, a2, a3, a4) do { if (QSE_APP_LOG_ENABLED(app, (mask))) (app)->logfmt((mask), fmt, a1, a2, a3, a4); } while(0)
#define QSE_APP_LOG5(app, mask, fmt, a1, a2, a3, a4, a5) do { if (QSE_APP_LOG_ENABLED(app, (mask))) (app)->logfmt((mask), fmt, a1, a2, a3, a4, a5); } while(0)
#define QSE_APP_LOG6(app, mask, fmt, a1, a2, a3, a4, a5, a6) do { if (QSE_APP_LOG_ENABLED(app, (mask))) (app)->logfmt((mask), fmt, a1, a2, a3, a4, a5, a6); } while(0)
#define QSE_APP_LOG7(app, mask, fmt, a1, a2, a3, a4, a5, a6, a7) do { if (QSE_APP_LOG_ENABLED(app, (mask))) (app)->logfmt((mask), fmt, a1, a2, a3, a4, a5, a6, a7); } while(0)
#define QSE_APP_LOG8(app, mask, fmt, a1, a2, a3, a4, a5, a6, a7, a8) do { if (QSE_APP_LOG_ENABLED(app, (mask))) (app)->logfmt((mask), fmt, a1, a2, a3, a4, a5, a6, a7, a8); } while(0)
#define QSE_APP_LOG9(app, mask, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9) do { if (QSE_APP_LOG_ENABLED(app, (mask))) (app)->logfmt((mask), fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9); } while(0)
#define QSE_APP_LOG_ENABLED(app, pri) (((app)->getLogPriorityMask() & (pri)) == (pri))
#define QSE_APP_LOG0(app, pri, fmt) do { if (QSE_APP_LOG_ENABLED(app, (pri))) (app)->logfmt((pri), fmt); } while(0)
#define QSE_APP_LOG1(app, pri, fmt, a1) do { if (QSE_APP_LOG_ENABLED(app, (pri))) (app)->logfmt((pri), fmt, a1); } while(0)
#define QSE_APP_LOG2(app, pri, fmt, a1, a2) do { if (QSE_APP_LOG_ENABLED(app, (pri))) (app)->logfmt((pri), fmt, a1, a2); } while(0)
#define QSE_APP_LOG3(app, pri, fmt, a1, a2, a3) do { if (QSE_APP_LOG_ENABLED(app, (pri))) (app)->logfmt((pri), fmt, a1, a2, a3); } while(0)
#define QSE_APP_LOG4(app, pri, fmt, a1, a2, a3, a4) do { if (QSE_APP_LOG_ENABLED(app, (pri))) (app)->logfmt((pri), fmt, a1, a2, a3, a4); } while(0)
#define QSE_APP_LOG5(app, pri, fmt, a1, a2, a3, a4, a5) do { if (QSE_APP_LOG_ENABLED(app, (pri))) (app)->logfmt((pri), fmt, a1, a2, a3, a4, a5); } while(0)
#define QSE_APP_LOG6(app, pri, fmt, a1, a2, a3, a4, a5, a6) do { if (QSE_APP_LOG_ENABLED(app, (pri))) (app)->logfmt((pri), fmt, a1, a2, a3, a4, a5, a6); } while(0)
#define QSE_APP_LOG7(app, pri, fmt, a1, a2, a3, a4, a5, a6, a7) do { if (QSE_APP_LOG_ENABLED(app, (pri))) (app)->logfmt((pri), fmt, a1, a2, a3, a4, a5, a6, a7); } while(0)
#define QSE_APP_LOG8(app, pri, fmt, a1, a2, a3, a4, a5, a6, a7, a8) do { if (QSE_APP_LOG_ENABLED(app, (pri))) (app)->logfmt((pri), fmt, a1, a2, a3, a4, a5, a6, a7, a8); } while(0)
#define QSE_APP_LOG9(app, pri, fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9) do { if (QSE_APP_LOG_ENABLED(app, (pri))) (app)->logfmt((pri), fmt, a1, a2, a3, a4, a5, a6, a7, a8, a9); } while(0)
class App: public Uncopyable, public Types, public Mmged
class App: public Uncopyable, public Types, public Mmged, public Named<32>
{
public:
typedef QSE::Bitset<QSE_NSIGS> SignalSet;
@ -69,6 +70,8 @@ public:
App (Mmgr* mmgr = QSE_NULL) QSE_CPP_NOEXCEPT;
virtual ~App () QSE_CPP_NOEXCEPT;
void setName (const qse_char_t* name) QSE_CPP_NOEXCEPT;
void setCmgr (qse_cmgr_t* cmgr) QSE_CPP_NOEXCEPT
{
this->_cmgr = cmgr;
@ -157,63 +160,47 @@ public:
// =============================================================
// LOGGING SUPPORT
// =============================================================
enum log_mask_t
void setSyslogFacility (qse_log_facility_t fac) /* useful for syslog is set as a target */
{
LOG_DEBUG = (1 << 0),
LOG_INFO = (1 << 1),
LOG_WARN = (1 << 2),
LOG_ERROR = (1 << 3),
LOG_FATAL = (1 << 4),
LOG_TYPE_0 = (1 << 6),
LOG_TYPE_1 = (1 << 7),
LOG_TYPE_2 = (1 << 8),
LOG_TYPE_3 = (1 << 9),
LOG_TYPE_4 = (1 << 10),
LOG_TYPE_5 = (1 << 11),
LOG_TYPE_6 = (1 << 12),
LOG_TYPE_7 = (1 << 13),
LOG_TYPE_8 = (1 << 14),
LOG_TYPE_9 = (1 << 15),
LOG_ALL_LEVELS = (LOG_DEBUG | LOG_INFO | LOG_WARN | LOG_ERROR | LOG_FATAL),
LOG_ALL_TYPES = (LOG_TYPE_0 | LOG_TYPE_1 | LOG_TYPE_2 | LOG_TYPE_3 | LOG_TYPE_4 | LOG_TYPE_5 | LOG_TYPE_6 | LOG_TYPE_7 | LOG_TYPE_8 | LOG_TYPE_9)
};
void setLogMask (int mask) { this->_log.mask = mask; }
int getLogMask () const { return this->_log.mask; }
void setLogOption (int oflags)
{
qse_log_setoption (&this->_log.logger, oflags);
qse_log_setsyslogfacility (&this->_log.logger, fac);
}
void
void setLogPriorityMask (int pri_mask) { this->_log.pri_mask = pri_mask; }
int getLogPriorityMask () const { return this->_log.pri_mask; }
void setLogOption (int option_flags) /* 0 or bitwise-OR'ed of qse_log_option_flag_t bits */
{
qse_log_setoption (&this->_log.logger, option_flags);
}
int getLogOption () const
{
return qse_log_getoption(&this->_log.logger);
}
void setLogTarget (int target_flags, const qse_log_target_data_t& target)
{
qse_log_settarget (this->_log.logger, target_flags, &target);
qse_log_settarget (&this->_log.logger, target_flags, &target);
}
int getLogTarget (qse_log_target_data_t& target)
int getLogTarget (qse_log_target_data_t& target) const
{
return qse_log_gettarget(this->_log.logger, &target);
return qse_log_gettarget(&this->_log.logger, &target);
}
void logfmt (int mask, const qse_char_t* fmt, ...)
void logfmt (qse_log_priority_flag_t pri, const qse_char_t* fmt, ...)
{
va_list ap;
va_start (ap, fmt);
logfmtv (mask, fmt, ap);
logfmtv (pri, fmt, ap);
va_end (ap);
}
void logfmtv (int mask, const qse_char_t* fmt, va_list ap);
void logfmtv (qse_log_priority_flag_t pri, const qse_char_t* fmt, va_list ap);
protected:
// subclasses may override this if the defaulg logging output is not desired.
virtual void log_write (int mask, const qse_char_t* msg, qse_size_t len);
virtual void log_write (qse_log_priority_flag_t mask, const qse_char_t* msg, qse_size_t len);
private:
App* _prev_app;
@ -233,15 +220,16 @@ private:
qse_cmgr_t* _cmgr;
struct log_t
{
log_t (App* app): mask(0), last_mask(0), len(0), mtx(app->getMmgr())
log_t (App* app): pri_mask(0), last_pri(QSE_LOG_PANIC), len(0), mtx(app->getMmgr())
{
}
int mask, last_mask;
int pri_mask; /* what priorities to log */
qse_log_priority_flag_t last_pri;
qse_size_t len;
qse_char_t buf[256];
QSE::Mutex mtx;
qse_log_t logger;
mutable qse_log_t logger;
} _log;
static int set_signal_handler_no_mutex (int sig, SignalHandler sighr);

View File

@ -63,9 +63,8 @@ enum qse_log_priority_flag_t /* bit 0 to 7. potentially to be extended beyond */
enum qse_log_option_flag_t /* bit 12 to 19 */
{
QSE_LOG_KEEP_FILE_OPEN = (1 << 12),
QSE_LOG_MASKED_PRIORITY = (1 << 13),
QSE_LOG_INCLUDE_PID = (1 << 14),
QSE_LOG_HOST_IN_REMOTE_SYSLOG = (1 << 15),
QSE_LOG_INCLUDE_PID = (1 << 13),
QSE_LOG_HOST_IN_REMOTE_SYSLOG = (1 << 14),
};
enum qse_log_target_flag_t /* bit 20 to 31 */
@ -274,10 +273,10 @@ QSE_EXPORT void qse_log_close (
);
QSE_EXPORT int qse_log_init (
qse_log_t* log,
qse_mmgr_t* mmgr,
const qse_char_t* ident,
int potflags,
qse_log_t* log,
qse_mmgr_t* mmgr,
const qse_char_t* ident,
int potflags,
const qse_log_target_data_t* target_data
);

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;

View File

@ -70,7 +70,7 @@ public:
case SIGHUP:
// TODO: don't call stop() if this processs is a guardian
// though it's no harm to call stop().
QSE_APP_LOG3 (this, MyApp::LOG_INFO | MyApp::LOG_TYPE_0, QSE_T("requesting to stop server...app %p server %p - pid %d\n"), this, &this->server, (int)getpid());
QSE_APP_LOG3 (this, QSE_LOG_INFO, QSE_T("requesting to stop server...app %p server %p - pid %d\n"), this, &this->server, (int)getpid());
this->server.stop();
break;
}
@ -87,12 +87,18 @@ public:
if (this->guardProcess(signals) > 0)
{
int target_flags;
qse_log_target_data_t target_data;
qse_log_target_data_t target_data;
target_data.file = QSE_T("tcpsvr01.log");
this->setLogTarget (target_flags, target_data);
this->setLogMask (MyApp::LOG_ALL_LEVELS | MyApp::LOG_ALL_TYPES);
this->setName (QSE_T("tcpsvr01"));
this->setLogTarget (QSE_LOG_CONSOLE | QSE_LOG_FILE, target_data);
this->setLogPriorityMask (QSE_LOG_ALL_PRIORITIES);
this->setLogOption (QSE_LOG_KEEP_FILE_OPEN | QSE_LOG_INCLUDE_PID);
QSE_APP_LOG0 (this, MyApp::LOG_INFO | MyApp::LOG_TYPE_0, QSE_T("Stareting server\n"));
QSE_APP_LOG0 (this, QSE_LOG_DEBUG, QSE_T("Starting server\n"));
QSE_APP_LOG0 (this, QSE_LOG_DEBUG, QSE_T("hello"));
QSE_APP_LOG0 (this, QSE_LOG_INFO, QSE_T(" <tcpsvr01r> "));
QSE_APP_LOG0 (this, QSE_LOG_INFO, QSE_T("started\n"));
this->server.setThreadStackSize (256000);
return this->server.start (QSE_T("[::]:9998,0.0.0.0:9998"));
}
@ -126,7 +132,7 @@ app2.acceptSignal (SIGINT);
app4.neglectSignal (SIGINT);
app3.neglectSignal (SIGINT);
app2.neglectSignal (SIGINT);
QSE_APP_LOG1 (&app, MyApp::LOG_INFO | MyApp::LOG_TYPE_0, QSE_T("END OF %d\n"), (int)getpid());
QSE_APP_LOG1 (&app, QSE_LOG_INFO, QSE_T("END OF %d\n"), (int)getpid());
return n;
}