diff --git a/include/plog/Appenders/SyslogAppender.h b/include/plog/Appenders/SyslogAppender.h new file mode 100644 index 0000000..1386dd7 --- /dev/null +++ b/include/plog/Appenders/SyslogAppender.h @@ -0,0 +1,39 @@ +#pragma once +#include +#include + +namespace plog +{ + template + 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; + } + } + }; +} diff --git a/include/plog/Formatters/SyslogFormatter.h b/include/plog/Formatters/SyslogFormatter.h new file mode 100644 index 0000000..99775db --- /dev/null +++ b/include/plog/Formatters/SyslogFormatter.h @@ -0,0 +1,30 @@ +#pragma once +#include +#include +#include + +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 + } + }; +} 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..cbc2e18 --- /dev/null +++ b/samples/Syslog/CMakeLists.txt @@ -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() 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; +}