Skip to content

Commit b46c44a

Browse files
committed
Added Syslog example.
1 parent 83136a7 commit b46c44a

File tree

3 files changed

+59
-0
lines changed

3 files changed

+59
-0
lines changed

samples/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -78,5 +78,6 @@ add_subdirectory(PrintVar)
7878
add_subdirectory(SetFileName)
7979
add_subdirectory(Shared)
8080
add_subdirectory(SkipNativeEOL)
81+
add_subdirectory(Syslog)
8182
add_subdirectory(UtcTime)
8283
add_subdirectory(Utf8Everywhere)

samples/Syslog/CMakeLists.txt

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
if(UNIX)
2+
add_executable(Syslog Main.cpp)
3+
target_link_libraries(Syslog plog)
4+
set_target_properties(Syslog PROPERTIES FOLDER Samples)
5+
endif()

samples/Syslog/Main.cpp

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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

Comments
 (0)