Commit 3b61f50c authored by Gabi Melman's avatar Gabi Melman

Merge pull request #20 from fooinha/syslog-openlog

Syslog openlog
parents ce6ee347 630e301e
......@@ -97,7 +97,8 @@ int main(int, char* [])
// syslog example
//
#ifdef __linux__
auto syslog_logger = spd::syslog_logger("syslog");
std::string ident = "my_app";
auto syslog_logger = spd::syslog_logger("syslog", ident, spd::sinks::syslog::option::PID | spd::sinks::syslog::option::PERROR, "mail" );
syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!");
#endif
}
......
......@@ -86,7 +86,8 @@ int main(int, char* [])
// syslog example
//
#ifdef __linux__
auto syslog_logger = spd::syslog_logger("syslog");
std::string ident = "my_ident";
auto syslog_logger = spd::syslog_logger("syslog", ident, spd::sinks::syslog::option::PID | spd::sinks::syslog::option::PERROR, "mail" );
syslog_logger->warn("This is warning that will end up in syslog. This is Linux only!");
#endif
}
......
......@@ -27,13 +27,42 @@
#include<initializer_list>
#include<chrono>
#ifdef __linux__
#define SYSLOG_NAMES 1
#include <syslog.h>
#endif
namespace spdlog
{
class formatter;
namespace sinks
{
class sink;
#ifdef __linux__
namespace syslog
{
namespace option
{
typedef enum
{
CONS = LOG_CONS,
NDELAY = LOG_NDELAY,
NOWAIT = LOG_NOWAIT,
ODELAY = LOG_ODELAY,
PERROR = LOG_PERROR,
PID = LOG_PID
} option_enum;
}
}
#endif
}
// Common types across the lib
using log_clock = std::chrono::system_clock;
......
......@@ -115,8 +115,10 @@ SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#define FMT_DISALLOW_COPY_AND_ASSIGN(TypeName) \
TypeName(const TypeName&); \
void operator=(const TypeName&)
namespace spdlog {
namespace details {
namespace spdlog
{
namespace details
{
namespace fmt
{
......@@ -2849,8 +2851,10 @@ fmt::print(format, args...);
#define FMT_VARIADIC_W(ReturnType, func, ...) \
FMT_VARIADIC_(wchar_t, ReturnType, func, return func, __VA_ARGS__)
namespace spdlog {
namespace details {
namespace spdlog
{
namespace details
{
namespace fmt
{
FMT_VARIADIC(std::string, format, StringRef)
......
......@@ -87,11 +87,10 @@ inline std::shared_ptr<spdlog::logger> spdlog::stderr_logger_st(const std::strin
#ifdef __linux__
// Create syslog logger
inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string& logger_name)
inline std::shared_ptr<spdlog::logger> spdlog::syslog_logger(const std::string& logger_name, const std::string& ident, int option, const std::string &facility)
{
return create<spdlog::sinks::syslog_sink>(logger_name);
return create<spdlog::sinks::syslog_sink>(logger_name, ident, option, facility);
}
#endif
......
......@@ -27,7 +27,8 @@
#ifdef __linux__
#include <string>
#include <syslog.h>
#include "./sink.h"
#include "../common.h"
#include "../details/log_msg.h"
......@@ -45,8 +46,9 @@ namespace sinks
class syslog_sink : public sink
{
public:
syslog_sink()
syslog_sink(const std::string& ident = "", int option = static_cast<int>(syslog::option::PID), const std::string &facility = "user")
{
_priorities[static_cast<int>(level::TRACE)] = LOG_DEBUG;
_priorities[static_cast<int>(level::DEBUG)] = LOG_DEBUG;
_priorities[static_cast<int>(level::INFO)] = LOG_INFO;
......@@ -59,18 +61,23 @@ public:
_priorities[static_cast<int>(level::ALWAYS)] = LOG_INFO;
_priorities[static_cast<int>(level::OFF)] = LOG_INFO;
::openlog(ident.c_str(), option, syslog_facility_from_name(facility));
}
virtual ~syslog_sink() {
::closelog();
}
virtual ~syslog_sink() = default;
syslog_sink(const syslog_sink&) = delete;
syslog_sink& operator=(const syslog_sink&) = delete;
void log(const details::log_msg &msg) override
{
syslog(syslog_prio_from_level(msg), "%s", msg.formatted.str().c_str());
::syslog(syslog_prio_from_level(msg), "%s", msg.formatted.str().c_str());
};
protected:
/**
* Simply maps spdlog's log level to syslog priority level.
*/
......@@ -81,6 +88,21 @@ protected:
private:
std::array<int, 11> _priorities;
inline int syslog_facility_from_name (const std::string & name)
{
if (name.empty())
return LOG_USER;
for (int i = 0; facilitynames[i].c_name != NULL; ++i)
{
if (name == facilitynames[i].c_name)
return facilitynames[i].c_val;
}
return LOG_USER;
}
};
}
}
......
......@@ -96,7 +96,8 @@ std::shared_ptr<logger> stderr_logger_st(const std::string& logger_name);
// Create a syslog logger
//
#ifdef __linux__
std::shared_ptr<logger> syslog_logger(const std::string& logger_name);
std::shared_ptr<logger> syslog_logger(const std::string& logger_name, const std::string& ident = "", int option = static_cast<int>(sinks::syslog::option::PID), const std::string &facility = "user");
#endif
//
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment