|
| 1 | +// |
| 2 | +// Syslog - shows how to use the syslog appender/formatter. |
| 3 | +// For the mappings between plog and syslog severities, refer to SyslogAppender |
| 4 | +// |
| 5 | + |
| 6 | +#include <plog/Log.h> |
| 7 | +#include <plog/Init.h> |
| 8 | +#include <plog/Formatters/SyslogFormatter.h> |
| 9 | +#include <plog/Appenders/SyslogAppender.h> |
| 10 | +#if __cplusplus >= 201103L |
| 11 | +#include <thread> |
| 12 | +#endif |
| 13 | + |
| 14 | +static void log_messages() |
| 15 | +{ |
| 16 | + PLOG_VERBOSE << "This is a VERBOSE message"; |
| 17 | + PLOG_DEBUG << "This is a DEBUG message"; |
| 18 | + PLOG_INFO << "This is an INFO message"; |
| 19 | + PLOG_WARNING << "This is a WARNING message"; |
| 20 | + PLOG_ERROR << "This is an ERROR message"; |
| 21 | + PLOG_FATAL << "This is a FATAL message"; |
| 22 | +} |
| 23 | + |
| 24 | +int main() |
| 25 | +{ |
| 26 | + static plog::SyslogAppender<plog::SyslogFormatter> syslogAppender; |
| 27 | + plog::init(plog::verbose, &syslogAppender); |
| 28 | + |
| 29 | + // Calling openlog() is optional. If omitted, the application name is |
| 30 | + // used (depends on the used C library) and the PID is not included. |
| 31 | + openlog("MyApp", LOG_PID, LOG_USER); |
| 32 | + |
| 33 | + // optional: log only messages up to PLOG_INFO (maps to LOG_NOTICE) |
| 34 | + setlogmask(LOG_UPTO(LOG_NOTICE)); |
| 35 | + |
| 36 | +#if __cplusplus >= 201103L |
| 37 | + // optional: start a 2nd thread which also generates log messages |
| 38 | + // you'll get the same PID but different TID values in the syslog file |
| 39 | + std::thread myThread(log_messages); |
| 40 | +#endif |
| 41 | + |
| 42 | + log_messages(); |
| 43 | + |
| 44 | +#if __cplusplus >= 201103L |
| 45 | + // wait until the 2nd thread has finished |
| 46 | + myThread.join(); |
| 47 | +#endif |
| 48 | + |
| 49 | + // Calling closelog() is optional |
| 50 | + closelog(); |
| 51 | + |
| 52 | + return 0; |
| 53 | +} |
0 commit comments