2#ifndef LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_EVENTLOGLOGGER_HPP_INCLUDED
3#define LOGIT_CPP_HEADER_LOGIT_CPP_LOGIT_LOGGERS_EVENTLOGLOGGER_HPP_INCLUDED
14#if defined(_WIN32) && defined(LOGIT_HAS_WIN_EVENT_LOG)
16#define LOGIT_WIN_EVENT_ENABLED 1
18#define LOGIT_WIN_EVENT_ENABLED 0
23# if LOGIT_WIN_EVENT_ENABLED
28 class EventLogLogger :
public ILogger {
49 m_hsrc = RegisterEventSourceW(
nullptr, m_cfg.source);
50 if (m_cfg.async && m_cfg.use_dedicated_executor) {
51 m_executor.reset(
new detail::SingleThreadExecutor());
52 m_executor->set_max_queue_size(m_cfg.queue_capacity);
53 m_executor->set_queue_policy(m_cfg.queue_policy);
65 const wchar_t* source,
67 bool use_dedicated_executor,
68 std::size_t queue_capacity,
73 use_dedicated_executor,
78 ~EventLogLogger()
override {
shutdown();
if (m_hsrc) DeregisterEventSource(m_hsrc); }
83 void log(
const LogRecord& rec,
const std::string& msg)
override {
84 std::lock_guard<std::mutex> lifecycle_lock(m_lifecycle_mutex);
85 if (m_shutdown.load(std::memory_order_acquire))
return;
87 bool raw_mode = rec.raw_mode;
89 auto task = [
this, lvl, raw_mode, s]() {
90 if (!raw_mode &&
static_cast<int>(lvl) < m_level.load())
return;
92 WORD type = m_map(lvl);
93 int n = MultiByteToWideChar(CP_UTF8, 0, s.c_str(),
static_cast<int>(s.size()),
nullptr, 0);
94 std::wstring wmsg; wmsg.resize(n);
95 MultiByteToWideChar(CP_UTF8, 0, s.c_str(),
static_cast<int>(s.size()), &wmsg[0], n);
96 LPCWSTR arr[1] = { wmsg.c_str() };
97 ReportEventW(m_hsrc, type, 0, 0,
nullptr, 1, 0, arr,
nullptr);
101 m_last_ts.store(rec.timestamp_ms);
133 std::lock_guard<std::mutex> lifecycle_lock(m_lifecycle_mutex);
134 if (m_shutdown.exchange(
true, std::memory_order_acq_rel))
return;
137 m_executor->shutdown();
138 }
else if (m_cfg.async) {
152 }
return EVENTLOG_INFORMATION_TYPE;
155 std::mutex m_lifecycle_mutex;
156 HANDLE m_hsrc =
nullptr;
158 std::atomic<int64_t> m_last_ts{0};
159 std::atomic<bool> m_shutdown{
false};
160 std::unique_ptr<detail::SingleThreadExecutor> m_executor;
162 static Config make_config(
163 const wchar_t* source,
165 bool use_dedicated_executor,
166 std::size_t queue_capacity,
168 Config config(source, async);
169 config.use_dedicated_executor = use_dedicated_executor;
170 config.queue_capacity = queue_capacity;
171 config.queue_policy = queue_policy;
179 class EventLogLogger : public ILogger {
185 bool use_dedicated_executor =
false;
186 std::size_t queue_capacity = 0;
188 Config(
const wchar_t* s = L
"",
bool a =
false) : source(s), async(a) {}
205 std::size_t queue_capacity,
QueuePolicy queue_policy) {
206 (void)source; (void)async; (void)use_dedicated_executor;
207 (void)queue_capacity; (void)queue_policy;
213 void log(
const LogRecord& rec,
const std::string& msg)
override { (void)rec; (void)msg; }
Defines the interface for loggers used in the logging system.
double get_float_param(const LoggerParam ¶m) const override
Return zero float parameter.
EventLogLogger()
Construct stub logger.
void log(const LogRecord &rec, const std::string &msg) override
Ignore log request.
void wait() override
No-op wait.
std::string get_string_param(const LoggerParam ¶m) const override
Return empty string parameter.
EventLogLogger(const wchar_t *source, bool async, bool use_dedicated_executor, std::size_t queue_capacity, QueuePolicy queue_policy)
Construct with parameters and ignored dedicated executor options.
LogLevel get_log_level() const override
Return default log level.
EventLogLogger(const wchar_t *source, bool async)
Construct with parameters.
void set_log_level(LogLevel l) override
Ignore level change.
EventLogLogger(const Config &c)
Construct with configuration.
int64_t get_int_param(const LoggerParam ¶m) const override
Return zero integer parameter.
virtual void shutdown()
Stops logger-owned asynchronous resources after draining pending work.
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.
QueuePolicy queue_policy
Unused.
bool use_dedicated_executor
Unused flag.
Config(const wchar_t *s=L"", bool a=false)
const wchar_t * source
Unused source name.
std::size_t queue_capacity
Unused.
Stores log metadata and content.