Skip to content

Commit

Permalink
Added Syslog example.
Browse files Browse the repository at this point in the history
  • Loading branch information
ceggers-arri committed Dec 5, 2024
1 parent 83136a7 commit 91e4e04
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 0 deletions.
1 change: 1 addition & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
7 changes: 7 additions & 0 deletions samples/Syslog/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
if(UNIX)
add_executable(Syslog Main.cpp)
target_link_libraries(Syslog plog)
target_compile_options(Syslog PRIVATE -pthread)
target_link_options(Syslog PRIVATE -pthread)
set_target_properties(Syslog PROPERTIES FOLDER Samples)
endif()
53 changes: 53 additions & 0 deletions samples/Syslog/Main.cpp
Original file line number Diff line number Diff line change
@@ -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 <plog/Log.h>
#include <plog/Init.h>
#include <plog/Formatters/SyslogFormatter.h>
#include <plog/Appenders/SyslogAppender.h>
#if __cplusplus >= 201103L
#include <thread>
#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<plog::SyslogFormatter> 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;
}

0 comments on commit 91e4e04

Please sign in to comment.