2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_SYSLOGLOGGER_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_SYSLOGLOGGER_HPP_INCLUDED
14#if (defined(__unix__) || defined(__APPLE__)) && defined(LOGIT_HAS_SYSLOG)
16#define LOGIT_SYSLOG_ENABLED 1
18#define LOGIT_SYSLOG_ENABLED 0
23# if LOGIT_SYSLOG_ENABLED
28 class SyslogLogger :
public ILogger {
42 Config(
const char* i =
"log-it",
int f = LOG_USER,
bool a =
true)
52 openlog(m_cfg.ident, LOG_PID | LOG_NDELAY, m_cfg.facility);
53 if (m_cfg.async && m_cfg.use_dedicated_executor) {
54 m_executor.reset(
new detail::SingleThreadExecutor());
55 m_executor->set_max_queue_size(m_cfg.queue_capacity);
56 m_executor->set_queue_policy(m_cfg.queue_policy);
64 SyslogLogger(
const char* ident,
int facility,
bool async)
72 bool use_dedicated_executor,
73 std::size_t queue_capacity,
79 use_dedicated_executor,
84 ~SyslogLogger()
override {
shutdown(); closelog(); }
89 void log(
const LogRecord& rec,
const std::string& msg)
override {
90 std::lock_guard<std::mutex> lifecycle_lock(m_lifecycle_mutex);
91 if (m_shutdown.load(std::memory_order_acquire))
return;
93 bool raw_mode = rec.raw_mode;
95 auto task = [
this, lvl, raw_mode, s]() {
96 if (!raw_mode &&
static_cast<int>(lvl) < m_level.load())
return;
97 syslog(m_map(lvl),
"%s", s.c_str());
101 m_last_ts.store(rec.timestamp_ms);
132 std::lock_guard<std::mutex> lifecycle_lock(m_lifecycle_mutex);
133 if (m_shutdown.exchange(
true, std::memory_order_acq_rel))
return;
136 m_executor->shutdown();
137 }
else if (m_cfg.async) {
154 std::mutex m_lifecycle_mutex;
156 std::atomic<int64_t> m_last_ts{0};
157 std::atomic<bool> m_shutdown{
false};
158 std::unique_ptr<detail::SingleThreadExecutor> m_executor;
160 static Config make_config(
164 bool use_dedicated_executor,
165 std::size_t queue_capacity,
167 Config config(ident, facility, async);
168 config.use_dedicated_executor = use_dedicated_executor;
169 config.queue_capacity = queue_capacity;
170 config.queue_policy = queue_policy;
178 class SyslogLogger : public ILogger {
185 bool use_dedicated_executor =
false;
186 std::size_t queue_capacity = 0;
188 Config(
const char* i=
"",
int f=0,
bool a=
false) : ident(i), facility(f), async(a) {}
202 SyslogLogger(
const char* ident,
int facility,
bool async) { (void)ident; (void)facility; (void)async; }
205 SyslogLogger(
const char* ident,
int facility,
bool async,
bool use_dedicated_executor,
206 std::size_t queue_capacity,
QueuePolicy queue_policy) {
207 (void)ident; (void)facility; (void)async; (void)use_dedicated_executor;
208 (void)queue_capacity; (void)queue_policy;
214 void log(
const LogRecord& rec,
const std::string& msg)
override { (void)rec; (void)msg; }
Defines the interface for loggers used in the logging system.
virtual void shutdown()
Stops logger-owned asynchronous resources after draining pending work.
std::string get_string_param(const LoggerParam ¶m) const override
Return empty string parameter.
void set_log_level(LogLevel l) override
Ignore level change.
double get_float_param(const LoggerParam ¶m) const override
Return zero float parameter.
SyslogLogger(const char *ident, int facility, bool async, bool use_dedicated_executor, std::size_t queue_capacity, QueuePolicy queue_policy)
Construct with parameters and ignored dedicated executor options.
SyslogLogger()
Construct stub logger.
SyslogLogger(const Config &c)
Construct with configuration.
LogLevel get_log_level() const override
Return default log level.
void log(const LogRecord &rec, const std::string &msg) override
Ignore log request.
int64_t get_int_param(const LoggerParam ¶m) const override
Return zero integer parameter.
SyslogLogger(const char *ident, int facility, bool async)
Construct with parameters.
void wait() override
No-op wait.
void add_task(std::function< void()> task)
Enqueue a task for asynchronous execution.
void wait()
Block until the queue is empty or a shutdown is requested.
static TaskExecutor & get_instance()
Returns the global executor instance.
QueuePolicy
Queue overflow handling policy used by TaskExecutor and SingleThreadExecutor.
The primary namespace for the LogIt++ library.
@ LOG_LVL_TRACE
Trace level logging.
@ LOG_LVL_ERROR
Error level logging.
@ LOG_LVL_INFO
Information level logging.
@ LOG_LVL_FATAL
Fatal level logging.
@ LOG_LVL_DEBUG
Debug level logging.
@ LOG_LVL_WARN
Warning level logging.
LoggerParam
Enumeration for different logger parameters that can be retrieved.
detail::QueuePolicy QueuePolicy
Public queue overflow handling policy.
Stores log metadata and content.
QueuePolicy queue_policy
Unused.
std::size_t queue_capacity
Unused.
int facility
Unused facility.
const char * ident
Unused identifier.
Config(const char *i="", int f=0, bool a=false)
bool use_dedicated_executor
Unused flag.