Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Syslog appender/formatter #295

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions include/plog/Appenders/SyslogAppender.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#pragma once
#include <plog/Appenders/IAppender.h>
#include <syslog.h>

namespace plog
{
template<class Formatter>
class PLOG_LINKAGE_HIDDEN SyslogAppender : public IAppender
{
public:
virtual void write(const Record& record) PLOG_OVERRIDE
{
std::string str = Formatter::format(record);

syslog(toPriority(record.getSeverity()), "%s", str.c_str());
}

private:
static int toPriority(Severity severity)
{
switch (severity)
{
case fatal:
return LOG_EMERG;
case error:
return LOG_ERR;
case warning:
return LOG_WARNING;
case info:
return LOG_NOTICE;
case debug:
return LOG_INFO;
case verbose:
default:
return LOG_DEBUG;
}
}
};
}
30 changes: 30 additions & 0 deletions include/plog/Formatters/SyslogFormatter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once
#include <plog/Record.h>
#include <plog/Util.h>
#include <utility>

namespace plog
{
class SyslogFormatter
{
public:
static util::nstring header()
{
return util::nstring();
}

static util::nstring format(const Record& record)
{
util::nostringstream ss;
ss << PLOG_NSTR("[") << record.getTid() << PLOG_NSTR("] ")
<< record.getFunc() << PLOG_NSTR("@") << record.getLine()
<< PLOG_NSTR(" ") << record.getMessage() /* no line feed */;

#if __cplusplus > 201703L
return std::move(ss).str();
#else
return ss.str();
#endif
}
};
}
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;
}