Commit 7c8a7176 authored by louiz’'s avatar louiz’

Only use sd_journal_* if we really are outputing to journald

We check that the device and inode numbers are actually the same as the
JOURNAL_STREAM value, instead of just checking that the value exists.

This fixes the logger unit tests
parent 25423ffd
#include <logger/logger.hpp> #include <logger/logger.hpp>
#include <config/config.hpp> #include <config/config.hpp>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
Logger::Logger(const int log_level): Logger::Logger(const int log_level):
log_level(log_level), log_level(log_level),
stream(std::cout.rdbuf()), stream(std::cout.rdbuf()),
...@@ -8,7 +12,22 @@ Logger::Logger(const int log_level): ...@@ -8,7 +12,22 @@ Logger::Logger(const int log_level):
null_stream{&null_buffer} null_stream{&null_buffer}
{ {
#ifdef SYSTEMD_FOUND #ifdef SYSTEMD_FOUND
if (::getenv("JOURNAL_STREAM") != nullptr && this->use_stdout()) if (!this->use_stdout())
return;
// See https://www.freedesktop.org/software/systemd/man/systemd.exec.html#%24JOURNAL_STREAM
const char* journal_stream = ::getenv("JOURNAL_STREAM");
if (journal_stream == nullptr)
return;
struct stat s{};
const int res = ::fstat(STDOUT_FILENO, &s);
if (res == -1)
return;
const auto stdout_stream = std::to_string(s.st_dev) + ":" + std::to_string(s.st_ino);
if (stdout_stream == journal_stream)
this->use_systemd = true; this->use_systemd = true;
#endif #endif
} }
......
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
*/ */
#include <memory> #include <memory>
#include <string>
#include <iostream> #include <iostream>
#include <fstream> #include <fstream>
#include <sstream> #include <sstream>
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment