Commit 8d99374f authored by louiz’'s avatar louiz’

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
parent dabc48b7
......@@ -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):
......
......@@ -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__)
......@@ -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")
{
......
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