summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlouiz’ <louiz@louiz.org>2017-09-06 21:30:44 +0200
committerlouiz’ <louiz@louiz.org>2017-09-06 21:30:44 +0200
commit8d99374f1f02a4d229b49f6697247eb1e1f4f940 (patch)
tree77def9e9e192963aa4c70f48a8ea97861c62f08b
parentdabc48b79b6189c99c246ae01af27fa170fd86a3 (diff)
downloadbiboumi-8d99374f1f02a4d229b49f6697247eb1e1f4f940.tar.gz
biboumi-8d99374f1f02a4d229b49f6697247eb1e1f4f940.tar.bz2
biboumi-8d99374f1f02a4d229b49f6697247eb1e1f4f940.tar.xz
biboumi-8d99374f1f02a4d229b49f6697247eb1e1f4f940.zip
When biboumi is logging into journald, use sd_journal_send
This makes sure that multi-lines messages are properly parsed as a single message by journald. fix #3268
-rw-r--r--src/logger/logger.cpp4
-rw-r--r--src/logger/logger.hpp76
-rw-r--r--tests/logger.cpp5
3 files changed, 43 insertions, 42 deletions
diff --git a/src/logger/logger.cpp b/src/logger/logger.cpp
index 92a3d9b..4287794 100644
--- a/src/logger/logger.cpp
+++ b/src/logger/logger.cpp
@@ -7,6 +7,10 @@ Logger::Logger(const int log_level):
null_buffer{},
null_stream{&null_buffer}
{
+#ifdef SYSTEMD_FOUND
+ if (::getenv("JOURNAL_STREAM") != nullptr && this->use_stdout())
+ this->use_systemd = true;
+#endif
}
Logger::Logger(const int log_level, const std::string& log_file):
diff --git a/src/logger/logger.hpp b/src/logger/logger.hpp
index ff6a82b..176726e 100644
--- a/src/logger/logger.hpp
+++ b/src/logger/logger.hpp
@@ -11,6 +11,7 @@
#include <memory>
#include <iostream>
#include <fstream>
+#include <sstream>
#define debug_lvl 0
#define info_lvl 1
@@ -19,7 +20,9 @@
#include "biboumi.h"
#ifdef SYSTEMD_FOUND
+#define SD_JOURNAL_SUPPRESS_LOCATION
# include <systemd/sd-daemon.h>
+# include <systemd/sd-journal.h>
#else
# define SD_DEBUG "[DEBUG]: "
# define SD_INFO "[INFO]: "
@@ -57,6 +60,15 @@ public:
Logger(Logger&&) = delete;
Logger& operator=(Logger&&) = delete;
+#ifdef SYSTEMD_FOUND
+ bool use_stdout() const
+ {
+ return this->stream.rdbuf() == std::cout.rdbuf();
+ }
+
+ bool use_systemd{false};
+#endif
+
private:
const int log_level;
std::ofstream ofstream{};
@@ -66,8 +78,6 @@ private:
std::ostream null_stream;
};
-#define WHERE __FILENAME__, ":", __LINE__, ":\t"
-
namespace logging_details
{
template <typename T>
@@ -84,45 +94,37 @@ namespace logging_details
}
template <typename... U>
- void log_debug(U&&... args)
- {
- auto& os = Logger::instance()->get_stream(debug_lvl);
- os << SD_DEBUG;
- log(os, std::forward<U>(args)...);
- }
-
- template <typename... U>
- void log_info(U&&... args)
- {
- auto& os = Logger::instance()->get_stream(info_lvl);
- os << SD_INFO;
- log(os, std::forward<U>(args)...);
- }
-
- template <typename... U>
- void log_warning(U&&... args)
- {
- auto& os = Logger::instance()->get_stream(warning_lvl);
- os << SD_WARNING;
- log(os, std::forward<U>(args)...);
- }
-
- template <typename... U>
- void log_error(U&&... args)
+ void do_logging(const int level, int syslog_level, const char* src_file, int line, U&&... args)
{
- auto& os = Logger::instance()->get_stream(error_lvl);
- os << SD_ERR;
- log(os, std::forward<U>(args)...);
+ #ifdef SYSTEMD_FOUND
+ if (Logger::instance()->use_systemd)
+ {
+ (void)level;
+ std::ostringstream os;
+ log(os, std::forward<U>(args)...);
+ sd_journal_send("MESSAGE=%s", os.str().data(),
+ "PRIORITY=%i", syslog_level,
+ "CODE_FILE=%s", src_file,
+ "CODE_LINE=%i", line,
+ nullptr);
+ }
+ else
+ {
+ #endif
+ static const char* priority_names[] = {"DEBUG", "INFO", "WARNING", "ERROR"};
+ auto& os = Logger::instance()->get_stream(level);
+ os << '[' << priority_names[level] << "]: " << src_file << ':' << line << ":\t";
+ log(os, std::forward<U>(args)...);
+#ifdef SYSTEMD_FOUND
+ }
+#endif
}
}
-#define log_info(...) logging_details::log_info(WHERE, __VA_ARGS__)
-
-#define log_warning(...) logging_details::log_warning(WHERE, __VA_ARGS__)
-
-#define log_error(...) logging_details::log_error(WHERE, __VA_ARGS__)
-
-#define log_debug(...) logging_details::log_debug(WHERE, __VA_ARGS__)
+#define log_debug(...) logging_details::do_logging(debug_lvl, LOG_DEBUG, __FILENAME__, __LINE__, __VA_ARGS__)
+#define log_info(...) logging_details::do_logging(info_lvl, LOG_INFO, __FILENAME__, __LINE__, __VA_ARGS__)
+#define log_warning(...) logging_details::do_logging(warning_lvl, LOG_WARNING, __FILENAME__, __LINE__, __VA_ARGS__)
+#define log_error(...) logging_details::do_logging(error_lvl, LOG_ERR, __FILENAME__, __LINE__, __VA_ARGS__)
diff --git a/tests/logger.cpp b/tests/logger.cpp
index 1d59a22..1e3392a 100644
--- a/tests/logger.cpp
+++ b/tests/logger.cpp
@@ -10,13 +10,8 @@ using namespace std::string_literals;
TEST_CASE("Basic logging")
{
-#ifdef SYSTEMD_FOUND
- const std::string debug_header = "<7>";
- const std::string error_header = "<3>";
-#else
const std::string debug_header = "[DEBUG]: ";
const std::string error_header = "[ERROR]: ";
-#endif
Logger::instance().reset();
GIVEN("A logger with log_level 0")
{