summaryrefslogtreecommitdiff
path: root/src/logger
diff options
context:
space:
mode:
Diffstat (limited to 'src/logger')
-rw-r--r--src/logger/logger.cpp4
-rw-r--r--src/logger/logger.hpp76
2 files changed, 43 insertions, 37 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__)