From bf6eb3eda9a5921738afc33857936112a0739a38 Mon Sep 17 00:00:00 2001 From: Christian Eggers Date: Thu, 5 Dec 2024 13:22:20 +0100 Subject: [PATCH] Added Syslog example. --- samples/CMakeLists.txt | 1 + samples/Syslog/CMakeLists.txt | 5 ++++ samples/Syslog/Main.cpp | 53 +++++++++++++++++++++++++++++++++++ 3 files changed, 59 insertions(+) create mode 100644 samples/Syslog/CMakeLists.txt create mode 100644 samples/Syslog/Main.cpp diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt index b91f628..eb50c0f 100644 --- a/samples/CMakeLists.txt +++ b/samples/CMakeLists.txt @@ -78,5 +78,6 @@ add_subdirectory(PrintVar) add_subdirectory(SetFileName) add_subdirectory(Shared) add_subdirectory(SkipNativeEOL) +add_subdirectory(Syslog) add_subdirectory(UtcTime) add_subdirectory(Utf8Everywhere) diff --git a/samples/Syslog/CMakeLists.txt b/samples/Syslog/CMakeLists.txt new file mode 100644 index 0000000..f77fdc8 --- /dev/null +++ b/samples/Syslog/CMakeLists.txt @@ -0,0 +1,5 @@ +if(UNIX) + add_executable(Syslog Main.cpp) + target_link_libraries(Syslog plog) + set_target_properties(Syslog PROPERTIES FOLDER Samples) +endif() diff --git a/samples/Syslog/Main.cpp b/samples/Syslog/Main.cpp new file mode 100644 index 0000000..c4aff95 --- /dev/null +++ b/samples/Syslog/Main.cpp @@ -0,0 +1,53 @@ +// +// Syslog - shows how to use the syslog appender/formatter. +// For the mappings between plog and syslog severities, refer to SyslogAppender +// + +#include +#include +#include +#include +#if __cplusplus >= 201103L +#include +#endif + +static void log_messages() +{ + PLOG_VERBOSE << "This is a VERBOSE message"; + PLOG_DEBUG << "This is a DEBUG message"; + PLOG_INFO << "This is an INFO message"; + PLOG_WARNING << "This is a WARNING message"; + PLOG_ERROR << "This is an ERROR message"; + PLOG_FATAL << "This is a FATAL message"; +} + +int main() +{ + static plog::SyslogAppender syslogAppender; + plog::init(plog::verbose, &syslogAppender); + + // Calling openlog() is optional. If omitted, the application name is + // used (depends on the used C library) and the PID is not included. + openlog("MyApp", LOG_PID, LOG_USER); + + // optional: log only messages up to PLOG_INFO (maps to LOG_NOTICE) + setlogmask(LOG_UPTO(LOG_NOTICE)); + +#if __cplusplus >= 201103L + // optional: start a 2nd thread which also generates log messages + // you'll get the same PID but different TID values in the syslog file + std::thread myThread(log_messages); +#endif + + log_messages(); + +#if __cplusplus >= 201103L + // wait until the 2nd thread has finished + myThread.join(); +#endif + + // Calling closelog() is optional + closelog(); + + return 0; +}