Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use fmt for logging and allow to log to system daemon directly #606

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ include(CMakeDependentOption)
option(BTOP_STATIC "Link btop statically" OFF)
option(BTOP_LTO "Enable LTO" ON)
option(BTOP_USE_MOLD "Use mold to link btop" OFF)
option(BTOP_SYSLOG "Log with syslog instead of log file" OFF)
option(BTOP_JOURNALD "Directly log to journald instead of log file" OFF)
option(BTOP_PEDANTIC "Enable a bunch of additional warnings" OFF)
option(BTOP_WERROR "Compile with warnings as errors" OFF)
option(BTOP_GPU "Enable GPU support" ON)
Expand All @@ -60,6 +62,7 @@ add_executable(btop
src/btop_config.cpp
src/btop_draw.cpp
src/btop_input.cpp
src/btop_log.cpp
src/btop_menu.cpp
src/btop_shared.cpp
src/btop_theme.cpp
Expand Down Expand Up @@ -131,6 +134,7 @@ target_compile_definitions(btop PRIVATE
_GLIBCXX_ASSERTIONS _LIBCPP_ENABLE_ASSERTIONS=1
# Only has an effect with optimizations enabled
$<$<NOT:$<CONFIG:Debug>>:_FORTIFY_SOURCE=2>
FMT_HEADER_ONLY
)

# Enable GPU support
Expand Down Expand Up @@ -180,6 +184,22 @@ set(THREADS_PREFER_PTHREAD_FLAG ON)
find_package(Threads REQUIRED)
target_link_libraries(btop PRIVATE Threads::Threads)

# Logging
if(BTOP_JOURNALD)
find_package(Systemd REQUIRED)
target_compile_definitions(btop PRIVATE HAVE_JOURNALD)
target_link_libraries(btop PRIVATE Systemd::Journald)
message(STATUS "Using journald as logging backend")
elseif(BTOP_SYSLOG)
check_include_file_cxx("syslog.h" HAVE_SYSLOG)
if(HAVE_SYSLOG)
target_compile_definitions(btop PRIVATE HAVE_SYSLOG)
message(STATUS "Using syslog as logging backend")
endif()
else()
message(STATUS "Using plain log file")
endif()

if(CMAKE_SYSTEM_NAME STREQUAL "Darwin")
target_link_libraries(btop PRIVATE $<LINK_LIBRARY:FRAMEWORK,CoreFoundation)
target_link_libraries(btop PRIVATE $<LINK_LIBRARY:FRAMEWORK,IOKit)
Expand Down
9 changes: 8 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,13 @@ else
LTO := $(THREADS)
endif

#? Enable journald direct logging
ifeq ($(ENABLE_JOURNALD),true)
override ADDFLAGS += $(shell echo "int main() {}" | $(CXX) -include "systemd/sd-journal.h" -lsystemd -o /dev/null -x c++ - >/dev/null 2>&1 && echo "-DHAVE_JOURNALD -lsystemd")
else ifeq ($(ENABLE_SYSLOG),true)
override ADDFLAGS += $(strip $(shell echo "int main() {}" | $(CXX) -include "syslog.h" -o /dev/null -x c++ - >/dev/null 2>&1 && echo "-DHAVE_SYSLOG"))
endif

#? The Directories, Source, Includes, Objects and Binary
SRCDIR := src
INCDIRS := include $(wildcard lib/**/include)
Expand All @@ -183,7 +190,7 @@ override GOODFLAGS := $(foreach flag,$(TESTFLAGS),$(strip $(shell echo "int main
override REQFLAGS := -std=c++20
WARNFLAGS := -Wall -Wextra -pedantic
OPTFLAGS := -O2 -ftree-vectorize -flto=$(LTO)
LDCXXFLAGS := -pthread -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS -D_FILE_OFFSET_BITS=64 $(GOODFLAGS) $(ADDFLAGS)
LDCXXFLAGS := -pthread -DFMT_HEADER_ONLY -D_FORTIFY_SOURCE=2 -D_GLIBCXX_ASSERTIONS -D_FILE_OFFSET_BITS=64 $(GOODFLAGS) $(ADDFLAGS)
override CXXFLAGS += $(REQFLAGS) $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS)
override LDFLAGS += $(LDCXXFLAGS) $(OPTFLAGS) $(WARNFLAGS)
INC := $(foreach incdir,$(INCDIRS),-isystem $(incdir)) -I$(SRCDIR)
Expand Down
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,8 @@ Also needs a UTF8 locale and a font that covers:
| `-DBTOP_STATIC=<ON\|OFF>` | Enables static linking (OFF by default) |
| `-DBTOP_LTO=<ON\|OFF>` | Enables link time optimization (ON by default) |
| `-DBTOP_USE_MOLD=<ON\|OFF>` | Use mold to link btop (OFF by default) |
| `-DBTOP_SYSLOG=<ON\|OFF>` | Log to syslog instead of a log file (OFF by default) |
| `-DBTOP_JOURNALD=<ON\|OFF>` | Log to journald instead of a log file (OFF by default) |
| `-DBTOP_PEDANTIC=<ON\|OFF>` | Compile with additional warnings (OFF by default) |
| `-DBTOP_WERROR=<ON\|OFF>` | Compile with warnings as errors (OFF by default) |
| `-DBTOP_GPU=<ON\|OFF>` | Enable GPU support (ON by default) |
Expand Down Expand Up @@ -764,6 +766,7 @@ Also needs a UTF8 locale and a font that covers:
| `-DBTOP_STATIC=<ON\|OFF>` | Enables static linking (OFF by default) |
| `-DBTOP_LTO=<ON\|OFF>` | Enables link time optimization (ON by default) |
| `-DBTOP_USE_MOLD=<ON\|OFF>` | Use mold to link btop (OFF by default) |
| `-DBTOP_SYSLOG=<ON\|OFF>` | Log to syslog instead of a log file (OFF by default) |
| `-DBTOP_PEDANTIC=<ON\|OFF>` | Compile with additional warnings (OFF by default) |
| `-DBTOP_WERROR=<ON\|OFF>` | Compile with warnings as errors (OFF by default) |
| `-DCMAKE_INSTALL_PREFIX=<path>` | The installation prefix ('/usr/local' by default) |
Expand Down Expand Up @@ -1053,6 +1056,13 @@ optional arguments:
and screen draw functions and sets loglevel to DEBUG
```

It's also possible to specify a log level using the `BTOP_LOG_LEVEL` environment variable.
It takes the same values as the `log_level` configuation option + "DISABLED", being case insensitive.
```bash
BTOP_LOG_LEVEL=info btop
```
will launch btop with the info logging level.

## LICENSE

[Apache License 2.0](LICENSE)
26 changes: 26 additions & 0 deletions cmake/Modules/FindSystemd.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# SPDX-License-Identifier: Apache-2.0
#
# Find systemd
#

if(CMAKE_SYSTEM_NAME STREQUAL "Linux")
find_path(Systemd_INCLUDE_DIR NAMES systemd/sd-journal.h)
find_library(Systemd_LIBRARY NAMES systemd)

include(FindPackageHandleStandardArgs)
find_package_handle_standard_args(Systemd REQUIRED_VARS Systemd_LIBRARY Systemd_INCLUDE_DIR)

if(Systemd_FOUND AND NOT TARGET Systemd::Systemd)
add_library(Systemd::Systemd UNKNOWN IMPORTED)
set_target_properties(Systemd::Systemd PROPERTIES
IMPORTED_LOCATION "${Systemd_LIBRARY}"
)
endif()

if(Systemd_FOUND AND NOT TARGET Systemd::Journald)
add_library(Systemd::Journald ALIAS Systemd::Systemd)
endif()

mark_as_advanced(Systemd_INCLUDE_DIR Systemd_LIBRARY)
endif()

48 changes: 32 additions & 16 deletions src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ tab-size = 4
#include <semaphore>
#endif

#include "btop_shared.hpp"
#include "btop_tools.hpp"
#include "btop_config.hpp"
#include "btop_input.hpp"
#include "btop_theme.hpp"
#include "btop_draw.hpp"
#include "btop_input.hpp"
#include "btop_log.hpp"
#include "btop_menu.hpp"
#include "btop_shared.hpp"
#include "btop_theme.hpp"
#include "btop_tools.hpp"

using std::atomic;
using std::cout;
Expand Down Expand Up @@ -283,10 +284,10 @@ void clean_quit(int sig) {

if (not Global::exit_error_msg.empty()) {
sig = 1;
Logger::error(Global::exit_error_msg);
Logger::error("{}", Global::exit_error_msg);
fmt::println(std::cerr, "{}ERROR: {}{}{}", Global::fg_red, Global::fg_white, Global::exit_error_msg, Fx::reset);
}
Logger::info("Quitting! Runtime: " + sec_to_dhms(time_s() - Global::start_time));
Logger::info("Quitting! Runtime: {}", sec_to_dhms(time_s() - Global::start_time));

const auto excode = (sig != -1 ? sig : 0);

Expand Down Expand Up @@ -846,7 +847,9 @@ int main(int argc, char **argv) {
}
else {
Config::conf_file = Config::conf_dir / "btop.conf";
#if !(defined(HAVE_JOURNALD) || defined(HAVE_SYSLOG))
Logger::logfile = Config::conf_dir / "btop.log";
#endif
Theme::user_theme_dir = Config::conf_dir / "themes";
if (not fs::exists(Theme::user_theme_dir) and not fs::create_directory(Theme::user_theme_dir, ec)) Theme::user_theme_dir.clear();
}
Expand Down Expand Up @@ -879,27 +882,40 @@ int main(int argc, char **argv) {
}

//? Config init
{ vector<string> load_warnings;
{
vector<string> load_warnings;
Config::load(Config::conf_file, load_warnings);

if (Config::current_boxes.empty()) Config::check_boxes(Config::getS("shown_boxes"));
Config::set("lowcolor", (Global::arg_low_color ? true : not Config::getB("truecolor")));

std::string log_level_env {};
{
char* log_env = std::getenv("BTOP_LOG_LEVEL");
if (log_env != nullptr) {
log_level_env = Tools::str_to_upper(log_env);
}
}
if (Global::debug) {
Logger::set("DEBUG");
Logger::debug("Starting in DEBUG mode!");
}
else Logger::set(Config::getS("log_level"));

Logger::info("Logger set to " + (Global::debug ? "DEBUG" : Config::getS("log_level")));
else if (v_contains(Logger::log_levels, log_level_env)) {
Logger::set(log_level_env);
Logger::info("Log level set to {}", log_level_env);
}
else {
Logger::set(Config::getS("log_level"));
Logger::info("Log level set to {}", Config::getS("log_level"));
}

for (const auto& err_str : load_warnings) Logger::warning(err_str);
for (const auto& err_str : load_warnings) Logger::warning("{}", err_str);
}

//? Try to find and set a UTF-8 locale
if (std::setlocale(LC_ALL, "") != nullptr and not s_contains((string)std::setlocale(LC_ALL, ""), ";")
and str_to_upper(s_replace((string)std::setlocale(LC_ALL, ""), "-", "")).ends_with("UTF8")) {
Logger::debug("Using locale " + (string)std::setlocale(LC_ALL, ""));
Logger::debug("Using locale {}", std::setlocale(LC_ALL, ""));
}
else {
string found;
Expand All @@ -909,7 +925,7 @@ int main(int argc, char **argv) {
found = std::getenv(loc_env);
if (std::setlocale(LC_ALL, found.c_str()) == nullptr) {
set_failure = true;
Logger::warning("Failed to set locale " + found + " continuing anyway.");
Logger::warning("Failed to set locale {} continuing anyway.", found);
}
}
}
Expand Down Expand Up @@ -942,7 +958,7 @@ int main(int argc, char **argv) {
Logger::warning("No UTF-8 locale detected! Some symbols might not display correctly.");
}
else if (std::setlocale(LC_ALL, string(cur_locale + ".UTF-8").c_str()) != nullptr) {
Logger::debug("Setting LC_ALL=" + cur_locale + ".UTF-8");
Logger::debug("Setting LC_ALL={}.UTF-8", cur_locale);
}
else if(std::setlocale(LC_ALL, "en_US.UTF-8") != nullptr) {
Logger::debug("Setting LC_ALL=en_US.UTF-8");
Expand All @@ -960,7 +976,7 @@ int main(int argc, char **argv) {
}
#endif
else if (not set_failure)
Logger::debug("Setting LC_ALL=" + found);
Logger::debug("Setting LC_ALL={}", found);
}

//? Initialize terminal and set options
Expand All @@ -969,7 +985,7 @@ int main(int argc, char **argv) {
clean_quit(1);
}

if (Term::current_tty != "unknown") Logger::info("Running on " + Term::current_tty);
if (Term::current_tty != "unknown") Logger::info("Running on {}", Term::current_tty);
if (not Global::arg_tty and Config::getB("force_tty")) {
Config::set("tty_mode", true);
Logger::info("Forcing tty mode: setting 16 color mode and using tty friendly graph symbols");
Expand Down
1 change: 1 addition & 0 deletions src/btop_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tab-size = 4
#include <fmt/core.h>

#include "btop_config.hpp"
#include "btop_log.hpp"
#include "btop_shared.hpp"
#include "btop_tools.hpp"

Expand Down
13 changes: 7 additions & 6 deletions src/btop_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ tab-size = 4
#include <stdexcept>
#include <string>

#include "btop_draw.hpp"
#include "btop_config.hpp"
#include "btop_theme.hpp"
#include "btop_shared.hpp"
#include "btop_tools.hpp"
#include "btop_draw.hpp"
#include "btop_input.hpp"
#include "btop_log.hpp"
#include "btop_menu.hpp"
#include "btop_shared.hpp"
#include "btop_theme.hpp"
#include "btop_tools.hpp"


using std::array;
Expand Down Expand Up @@ -225,7 +226,7 @@ namespace Draw {
c_upos = ulen(first);
}
catch (const std::exception& e) {
Logger::error("In TextEdit::operator() : " + string{e.what()});
Logger::error("In TextEdit::operator() : {}", e.what());
return "";
}
}
Expand Down Expand Up @@ -294,7 +295,7 @@ namespace Draw {
}

bool update_clock(bool force) {
const auto& clock_format = Config::getS("clock_format");
const std::string_view clock_format = Config::getS("clock_format");
if (not Cpu::shown or clock_format.empty()) {
if (clock_format.empty() and not Global::clock.empty()) Global::clock.clear();
return false;
Expand Down
Loading
Loading