|
| 1 | +/* Icinga 2 | (c) 2021 Icinga GmbH | GPLv2+ */ |
| 2 | + |
| 3 | +#ifdef _WIN32 |
| 4 | +#include "base/windowseventloglogger.hpp" |
| 5 | +#include "base/windowseventloglogger-ti.cpp" |
| 6 | +#include "base/windowseventloglogger-provider.h" |
| 7 | +#include "base/configtype.hpp" |
| 8 | +#include "base/statsfunction.hpp" |
| 9 | +#include <windows.h> |
| 10 | + |
| 11 | +using namespace icinga; |
| 12 | + |
| 13 | +REGISTER_TYPE(WindowsEventLogLogger); |
| 14 | + |
| 15 | +REGISTER_STATSFUNCTION(WindowsEventLogLogger, &WindowsEventLogLogger::StatsFunc); |
| 16 | + |
| 17 | +INITIALIZE_ONCE(&WindowsEventLogLogger::StaticInitialize); |
| 18 | + |
| 19 | +static HANDLE l_EventLog = nullptr; |
| 20 | + |
| 21 | +void WindowsEventLogLogger::StaticInitialize() |
| 22 | +{ |
| 23 | + l_EventLog = RegisterEventSourceA(nullptr, "Icinga 2"); |
| 24 | +} |
| 25 | + |
| 26 | +void WindowsEventLogLogger::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&) |
| 27 | +{ |
| 28 | + DictionaryData nodes; |
| 29 | + |
| 30 | + for (const WindowsEventLogLogger::Ptr& logger : ConfigType::GetObjectsByType<WindowsEventLogLogger>()) { |
| 31 | + nodes.emplace_back(logger->GetName(), 1); |
| 32 | + } |
| 33 | + |
| 34 | + status->Set("windowseventloglogger", new Dictionary(std::move(nodes))); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * Processes a log entry and outputs it to the Windows Event Log. |
| 39 | + * |
| 40 | + * @param entry The log entry. |
| 41 | + */ |
| 42 | +void WindowsEventLogLogger::ProcessLogEntry(const LogEntry& entry) |
| 43 | +{ |
| 44 | + if (l_EventLog != nullptr) { |
| 45 | + std::string message = Logger::SeverityToString(entry.Severity) + "/" + entry.Facility + ": " + entry.Message; |
| 46 | + std::array<const char *, 1> strings{ |
| 47 | + message.c_str() |
| 48 | + }; |
| 49 | + |
| 50 | + WORD eventType; |
| 51 | + switch (entry.Severity) { |
| 52 | + case LogCritical: |
| 53 | + eventType = EVENTLOG_ERROR_TYPE; |
| 54 | + break; |
| 55 | + case LogWarning: |
| 56 | + eventType = EVENTLOG_WARNING_TYPE; |
| 57 | + break; |
| 58 | + default: |
| 59 | + eventType = EVENTLOG_INFORMATION_TYPE; |
| 60 | + } |
| 61 | + |
| 62 | + ReportEventA(l_EventLog, eventType, 0, MSG_PLAIN_LOG_ENTRY, NULL, strings.size(), 0, strings.data(), NULL); |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +void WindowsEventLogLogger::Flush() |
| 67 | +{ |
| 68 | + /* Nothing to do here. */ |
| 69 | +} |
| 70 | + |
| 71 | +#endif /* _WIN32 */ |
0 commit comments