You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
spdlog/include/spdlog/sinks/syslog_sink.h

77 lines
2.0 KiB
C

9 years ago
//
// Copyright(c) 2015 Gabi Melman.
// Distributed under the MIT License (http://opensource.org/licenses/MIT)
//
#pragma once
#include "../common.h"
9 years ago
#ifdef SPDLOG_ENABLE_SYSLOG
#include "../details/log_msg.h"
8 years ago
#include "sink.h"
9 years ago
#include <array>
#include <string>
#include <syslog.h>
8 years ago
namespace spdlog {
namespace sinks {
9 years ago
/**
* Sink that write to syslog using the `syscall()` library call.
*
* Locking is not needed, as `syslog()` itself is thread-safe.
*/
class syslog_sink : public sink
{
public:
//
8 years ago
syslog_sink(const std::string &ident = "", int syslog_option = 0, int syslog_facility = LOG_USER)
: _ident(ident)
9 years ago
{
_priorities[static_cast<size_t>(level::trace)] = LOG_DEBUG;
_priorities[static_cast<size_t>(level::debug)] = LOG_DEBUG;
_priorities[static_cast<size_t>(level::info)] = LOG_INFO;
_priorities[static_cast<size_t>(level::warn)] = LOG_WARNING;
_priorities[static_cast<size_t>(level::err)] = LOG_ERR;
_priorities[static_cast<size_t>(level::critical)] = LOG_CRIT;
_priorities[static_cast<size_t>(level::off)] = LOG_INFO;
9 years ago
8 years ago
// set ident to be program name if empty
::openlog(_ident.empty() ? nullptr : _ident.c_str(), syslog_option, syslog_facility);
9 years ago
}
~syslog_sink() override
9 years ago
{
::closelog();
}
8 years ago
syslog_sink(const syslog_sink &) = delete;
syslog_sink &operator=(const syslog_sink &) = delete;
9 years ago
void log(const details::log_msg &msg) override
{
::syslog(syslog_prio_from_level(msg), "%s", msg.raw.str().c_str());
}
8 years ago
void flush() override {}
9 years ago
private:
std::array<int, 7> _priorities;
8 years ago
// must store the ident because the man says openlog might use the pointer as is and not a string copy
9 years ago
const std::string _ident;
//
// Simply maps spdlog's log level to syslog priority level.
//
int syslog_prio_from_level(const details::log_msg &msg) const
{
return _priorities[static_cast<size_t>(msg.level)];
9 years ago
}
};
8 years ago
} // namespace sinks
} // namespace spdlog
9 years ago
#endif