icinga2/lib/base/journaldlogger.cpp
Yonas Habteab 91c7e60df8 Replace all existing copyright headers with SPDX headers
I've used the following command to replace the original copyright header
lines in a C-style comment block:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{/\*[^*]*\(\s*c\s*\)\s*(\d{4})\s*Icinga\s+GmbH[^*]*\*/}{// SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n// SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```

For files that use shell-style comments (#) like CMakeLists.txt, I've
used this command:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f -exec perl -pi -e 's{#.*\(\s*c\s*\)\s(\d{4})\sIcinga\s+GmbH.*}{# SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n# SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```

And for SQL files:

```
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{--.*\(c\)\s(\d{4})\sIcinga\sGmbH.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
$ find . \( -type d \( -name '\..*' -o -name third-party -o -name scripts -o -name prefix -o -name malloc -o -name server -o -name docker -o -name build -o -name doc \) -prune \) -o -type f \( -name '*.sql' \) -exec perl -pi -e 's{-- Copyright \(c\)\s(\d{4})\sIcinga\s+Development\sTeam.*}{-- SPDX-FileCopyrightText: \1 Icinga GmbH <https://icinga.com>\n-- SPDX-License-Identifier: GPL-2.0-or-later}gi' {} +
```
2026-02-04 14:00:05 +01:00

88 lines
2.6 KiB
C++

// SPDX-FileCopyrightText: 2021 Icinga GmbH <https://icinga.com>
// SPDX-License-Identifier: GPL-2.0-or-later
#include "base/i2-base.hpp"
#if !defined(_WIN32) && defined(HAVE_SYSTEMD)
#include "base/journaldlogger.hpp"
#include "base/journaldlogger-ti.cpp"
#include "base/configtype.hpp"
#include "base/statsfunction.hpp"
#include "base/sysloglogger.hpp"
#include <systemd/sd-journal.h>
using namespace icinga;
REGISTER_TYPE(JournaldLogger);
REGISTER_STATSFUNCTION(JournaldLogger, &JournaldLogger::StatsFunc);
void JournaldLogger::StatsFunc(const Dictionary::Ptr& status, const Array::Ptr&)
{
DictionaryData nodes;
for (const JournaldLogger::Ptr& journaldlogger : ConfigType::GetObjectsByType<JournaldLogger>()) {
nodes.emplace_back(journaldlogger->GetName(), 1); //add more stats
}
status->Set("journaldlogger", new Dictionary(std::move(nodes)));
}
void JournaldLogger::OnConfigLoaded()
{
ObjectImpl<JournaldLogger>::OnConfigLoaded();
m_ConfiguredJournalFields.clear();
m_ConfiguredJournalFields.push_back(
String("SYSLOG_FACILITY=") + Value(SyslogHelper::FacilityToNumber(GetFacility())));
const String identifier = GetIdentifier();
if (!identifier.IsEmpty()) {
m_ConfiguredJournalFields.push_back(String("SYSLOG_IDENTIFIER=" + identifier));
}
}
void JournaldLogger::ValidateFacility(const Lazy<String>& lvalue, const ValidationUtils& utils)
{
ObjectImpl<JournaldLogger>::ValidateFacility(lvalue, utils);
if (!SyslogHelper::ValidateFacility(lvalue()))
BOOST_THROW_EXCEPTION(ValidationError(this, { "facility" }, "Invalid facility specified."));
}
/**
* Processes a log entry and outputs it to journald.
*
* @param entry The log entry.
*/
void JournaldLogger::ProcessLogEntry(const LogEntry& entry)
{
const std::vector<String> sdFields {
String("MESSAGE=") + entry.Message.GetData(),
String("PRIORITY=") + Value(SyslogHelper::SeverityToNumber(entry.Severity)),
String("ICINGA2_FACILITY=") + entry.Facility,
};
SystemdJournalSend(sdFields);
}
void JournaldLogger::Flush()
{
/* Nothing to do here. */
}
void JournaldLogger::SystemdJournalSend(const std::vector<String>& varJournalFields) const
{
struct iovec iovec[m_ConfiguredJournalFields.size() + varJournalFields.size()];
int iovecCount = 0;
for (const String& journalField: m_ConfiguredJournalFields) {
iovec[iovecCount] = IovecFromString(journalField);
iovecCount++;
}
for (const String& journalField: varJournalFields) {
iovec[iovecCount] = IovecFromString(journalField);
iovecCount++;
}
sd_journal_sendv(iovec, iovecCount);
}
struct iovec JournaldLogger::IovecFromString(const String& s) {
return { const_cast<char *>(s.CStr()), s.GetLength() };
}
#endif /* !_WIN32 && HAVE_SYSTEMD */