diff options
author | louiz’ <louiz@louiz.org> | 2018-01-12 03:59:46 +0100 |
---|---|---|
committer | louiz’ <louiz@louiz.org> | 2018-01-12 04:03:36 +0100 |
commit | 7c8a7176d196d4bb3724cfafd41980c16be5f404 (patch) | |
tree | 289e7e3c6f0a6f118d7402d71cdbf090a3190862 | |
parent | 25423ffd4324d90e2743432592d9ae29331b3ca0 (diff) | |
download | biboumi-7c8a7176d196d4bb3724cfafd41980c16be5f404.tar.gz biboumi-7c8a7176d196d4bb3724cfafd41980c16be5f404.tar.bz2 biboumi-7c8a7176d196d4bb3724cfafd41980c16be5f404.tar.xz biboumi-7c8a7176d196d4bb3724cfafd41980c16be5f404.zip |
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
-rw-r--r-- | src/logger/logger.cpp | 21 | ||||
-rw-r--r-- | src/logger/logger.hpp | 1 |
2 files changed, 21 insertions, 1 deletions
diff --git a/src/logger/logger.cpp b/src/logger/logger.cpp index 4287794..482cb18 100644 --- a/src/logger/logger.cpp +++ b/src/logger/logger.cpp @@ -1,6 +1,10 @@ #include <logger/logger.hpp> #include <config/config.hpp> +#include <sys/types.h> +#include <sys/stat.h> +#include <unistd.h> + Logger::Logger(const int log_level): log_level(log_level), stream(std::cout.rdbuf()), @@ -8,7 +12,22 @@ Logger::Logger(const int log_level): null_stream{&null_buffer} { #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; #endif } diff --git a/src/logger/logger.hpp b/src/logger/logger.hpp index a99648c..1689866 100644 --- a/src/logger/logger.hpp +++ b/src/logger/logger.hpp @@ -9,6 +9,7 @@ */ #include <memory> +#include <string> #include <iostream> #include <fstream> #include <sstream> |