blob: 6f46599979c3e93a5ebee4b5c476a9e008f36100 [file] [log] [blame]
/*
* Copyright (C) 2016-2019 CESNET, https://photonics.cesnet.cz/
*
* Written by Jan Kundrát <jan.kundrat@cesnet.cz>
*
*/
#include <cinttypes>
#include <cstdio>
#include <cstdlib>
#include <spdlog/sinks/systemd_sink.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include "utils/journal.h"
namespace velia::utils {
/** @short Is stderr connected to journald? Not thread safe. */
bool isJournaldActive()
{
const auto stream = ::getenv("JOURNAL_STREAM");
if (!stream) {
return false;
}
uintmax_t dev;
uintmax_t inode;
if (::sscanf(stream, "%" SCNuMAX ":%" SCNuMAX, &dev, &inode) != 2) {
return false;
}
struct stat buf;
if (fstat(STDERR_FILENO, &buf)) {
return false;
}
return static_cast<uintmax_t>(buf.st_dev) == dev && static_cast<uintmax_t>(buf.st_ino) == inode;
}
namespace impl {
/** @short Provide better levels, see https://github.com/gabime/spdlog/pull/1292#discussion_r340777258 */
template <typename Mutex>
class journald_sink : public spdlog::sinks::systemd_sink<Mutex> {
public:
journald_sink()
{
this->syslog_levels_ = {/* spdlog::level::trace */ LOG_DEBUG,
/* spdlog::level::debug */ LOG_INFO,
/* spdlog::level::info */ LOG_NOTICE,
/* spdlog::level::warn */ LOG_WARNING,
/* spdlog::level::err */ LOG_ERR,
/* spdlog::level::critical */ LOG_CRIT,
/* spdlog::level::off */ LOG_ALERT};
}
};
}
std::shared_ptr<spdlog::sinks::sink> create_journald_sink()
{
return std::make_shared<impl::journald_sink<std::mutex>>();
}
}