Skip to content

Commit

Permalink
Log: Log to syslog if syslog.h is found
Browse files Browse the repository at this point in the history
  • Loading branch information
imwints committed Aug 29, 2023
1 parent 288a298 commit c2ac974
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,9 @@ else
LTO := $(THREADS)
endif

#? Look for syslog.h
override ADDFLAGS += $(strip $(shell echo "#include <syslog.h>\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)
Expand Down
21 changes: 20 additions & 1 deletion src/btop_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
#include <mutex>
#include <string>

#if defined(HAVE_SYSLOG)
#include <syslog.h>
#endif

#include <fmt/core.h>
#include <fmt/ostream.h>

Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -79,5 +97,6 @@ namespace Logger {
logfile.clear();
throw std::runtime_error(fmt::format("Exception in Logger::log_write() : {}", e.what()));
}
#endif
}
} // namespace Logger

0 comments on commit c2ac974

Please sign in to comment.