diff --git a/Makefile b/Makefile index 2005f6f17..eb191b25d 100644 --- a/Makefile +++ b/Makefile @@ -156,6 +156,9 @@ else LTO := $(THREADS) endif +#? Look for syslog.h +override ADDFLAGS += $(strip $(shell echo "#include \nint main() {}" | $(CXX) -o /dev/null -x c++ - >/dev/null 2>&1 && echo "-DHAVE_SYSLOG")) + #? The Directories, Source, Includes, Objects and Binary SRCDIR := src INCDIRS := include $(wildcard lib/**/include) diff --git a/src/btop_log.cpp b/src/btop_log.cpp index b262d3060..426f29642 100644 --- a/src/btop_log.cpp +++ b/src/btop_log.cpp @@ -7,6 +7,10 @@ #include #include +#if defined(HAVE_SYSLOG) + #include +#endif + #include #include @@ -44,11 +48,25 @@ namespace Logger { void set(const string& level) { loglevel = v_index(log_levels, level); } void log_write(const size_t level, const std::string_view msg) { - if (loglevel < level or logfile.empty()) { + if (loglevel < level) { return; } atomic_lock lck(busy, true); lose_priv neutered{}; + +#if defined(HAVE_SYSLOG) + int status = LOG_DEBUG; + switch (level) { + case 1: status = LOG_ERR; break; + case 2: status = LOG_WARNING; break; + case 3: status = LOG_INFO; break; + case 4: status = LOG_DEBUG; break; + } + syslog(status, msg.data()); +#else + if (logfile.empty()) { + return; + } std::error_code ec; try { if (fs::exists(logfile) and fs::file_size(logfile, ec) > 1024 << 10 and not ec) { @@ -79,5 +97,6 @@ namespace Logger { logfile.clear(); throw std::runtime_error(fmt::format("Exception in Logger::log_write() : {}", e.what())); } +#endif } } // namespace Logger