diff --git a/src/btop.cpp b/src/btop.cpp index 79b2acda5..964e729a7 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -268,10 +268,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); @@ -816,15 +816,15 @@ int main(int argc, char **argv) { } else Logger::set(Config::getS("log_level")); - Logger::info("Logger set to " + (Global::debug ? "DEBUG" : Config::getS("log_level"))); + Logger::info("Logger set to {}", (Global::debug ? "DEBUG" : 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; @@ -834,7 +834,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); } } } @@ -885,7 +885,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 @@ -894,7 +894,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"); diff --git a/src/btop_draw.cpp b/src/btop_draw.cpp index fc9c689da..f7d1ff80c 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -223,7 +223,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 ""; } } diff --git a/src/btop_log.cpp b/src/btop_log.cpp index f7b7a652d..32f433db1 100644 --- a/src/btop_log.cpp +++ b/src/btop_log.cpp @@ -7,6 +7,8 @@ #include #include +#include + #include "btop_shared.hpp" #include "btop_tools.hpp" @@ -40,7 +42,7 @@ namespace Logger { void set(const string& level) { loglevel = v_index(log_levels, level); } - void log_write(const size_t level, const string& msg) { + void log_write(const size_t level, const std::string_view msg) { if (loglevel < level or logfile.empty()) { return; } @@ -64,9 +66,9 @@ namespace Logger { std::ofstream lwrite(logfile, std::ios::app); if (first) { first = false; - lwrite << "\n" << strf_time(tdf) << "===> btop++ v." << Global::Version << "\n"; + fmt::print(lwrite, "\n{}===> btop++ v.{}\n", strf_time(tdf), Global::Version); } - lwrite << strf_time(tdf) << log_levels.at(level) << ": " << msg << "\n"; + fmt::print(lwrite, "{}{}: {}\n", strf_time(tdf), log_levels.at(level), msg); } else { logfile.clear(); @@ -74,7 +76,7 @@ namespace Logger { } catch (const std::exception& e) { logfile.clear(); - throw std::runtime_error("Exception in Logger::log_write() : " + string{e.what()}); + throw std::runtime_error(fmt::format("Exception in Logger::log_write() : {}", e.what())); } } } // namespace Logger diff --git a/src/btop_log.hpp b/src/btop_log.hpp index 412912167..fe1928e15 100644 --- a/src/btop_log.hpp +++ b/src/btop_log.hpp @@ -3,9 +3,11 @@ #pragma once #include -#include +#include #include +#include + namespace Logger { const std::vector log_levels = { "DISABLED", @@ -19,9 +21,25 @@ namespace Logger { //* Set log level, valid arguments: "DISABLED", "ERROR", "WARNING", "INFO" and "DEBUG" void set(const std::string& level); - void log_write(const size_t level, const std::string& msg); - inline void error(const std::string msg) { log_write(1, msg); } - inline void warning(const std::string msg) { log_write(2, msg); } - inline void info(const std::string msg) { log_write(3, msg); } - inline void debug(const std::string msg) { log_write(4, msg); } + void log_write(const size_t level, const std::string_view msg); + + template + inline void error(const fmt::format_string fmt, T&&... args) { + log_write(1, fmt::format(fmt, std::forward(args)...)); + } + + template + inline void warning(const fmt::format_string fmt, T&&... args) { + log_write(2, fmt::format(fmt, std::forward(args)...)); + } + + template + inline void info(const fmt::format_string fmt, T&&... args) { + log_write(3, fmt::format(fmt, std::forward(args)...)); + } + + template + inline void debug(const fmt::format_string fmt, T&&... args) { + log_write(4, fmt::format(fmt, std::forward(args)...)); + } } // namespace Logger diff --git a/src/btop_menu.cpp b/src/btop_menu.cpp index 5be88f729..8ecaab6ad 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -1212,7 +1212,7 @@ namespace Menu { theme_refresh = true; else if (option == "log_level") { Logger::set(optList.at(i)); - Logger::info("Logger set to " + optList.at(i)); + Logger::info("Logger set to {}", optList.at(i)); } else if (is_in(option, "proc_sorting", "cpu_sensor") or option.starts_with("graph_symbol") or option.starts_with("cpu_graph_")) screen_redraw = true; diff --git a/src/btop_theme.cpp b/src/btop_theme.cpp index 1850868c0..b9c526caa 100644 --- a/src/btop_theme.cpp +++ b/src/btop_theme.cpp @@ -156,7 +156,7 @@ namespace Theme { hexa.erase(0, 1); for (auto& c : hexa) { if (not isxdigit(c)) { - Logger::error("Invalid hex value: " + hexa); + Logger::error("Invalid hex value: {}", hexa); return ""; } } @@ -184,9 +184,9 @@ namespace Theme { to_string(stoi(hexa.substr(4, 2), nullptr, 16)) + "m"; } } - else Logger::error("Invalid size of hex value: " + hexa); + else Logger::error("Invalid size of hex value: {}", hexa); } - else Logger::error("Hex value missing: " + hexa); + else Logger::error("Hex value missing: {}", hexa); return ""; } @@ -255,7 +255,7 @@ namespace Theme { else if (not source.at(name).empty()) { t_rgb = ssplit(source.at(name)); if (t_rgb.size() != 3) { - Logger::error("Invalid RGB decimal value: \"" + source.at(name) + "\""); + Logger::error("Invalid RGB decimal value: \"{}\"", source.at(name)); } else { colors[name] = dec_to_color(stoi(t_rgb[0]), stoi(t_rgb[1]), stoi(t_rgb[2]), t_to_256, depth); rgbs[name] = array{stoi(t_rgb[0]), stoi(t_rgb[1]), stoi(t_rgb[2])}; @@ -264,7 +264,7 @@ namespace Theme { } } if (not colors.contains(name) and not is_in(name, "meter_bg", "process_start", "process_mid", "process_end", "graph_text")) { - Logger::debug("Missing color value for \"" + name + "\". Using value from default."); + Logger::debug("Missing color value for \"{}\". Using value from default.", name); colors[name] = hex_to_color(color, t_to_256, depth); rgbs[name] = hex_to_dec(color); } @@ -380,7 +380,7 @@ namespace Theme { std::ifstream themefile(filepath); if (themefile.good()) { - Logger::debug("Loading theme file: " + filename); + Logger::debug("Loading theme file: {}", filename); while (not themefile.bad()) { themefile.ignore(SSmax, '['); if (themefile.eof()) break; diff --git a/src/btop_tools.cpp b/src/btop_tools.cpp index 24f2fb90a..854ad6f5e 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -507,7 +507,7 @@ namespace Tools { for (string readstr; getline(file, readstr); out += readstr); } catch (const std::exception& e) { - Logger::error("readfile() : Exception when reading " + string{path} + " : " + e.what()); + Logger::error("readfile() : Exception when reading {} : {}", path.string(), e.what()); return fallback; } return (out.empty() ? fallback : out); diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index ff8a46635..a140e4a89 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -457,7 +457,7 @@ namespace Cpu { if (++failed < 5) return ""s; else { - Logger::warning("get_cpuHZ() : " + string{e.what()}); + Logger::warning("get_cpuHZ() : {}", e.what()); return ""s; } } @@ -785,7 +785,7 @@ namespace Cpu { //? Notify main thread to redraw screen if we found more cores than previously detected if (cmp_greater(cpu.core_percent.size(), Shared::coreCount)) { - Logger::debug("Changing CPU max corecount from " + to_string(Shared::coreCount) + " to " + to_string(cpu.core_percent.size()) + "."); + Logger::debug("Changing CPU max corecount from {} to {}.", Shared::coreCount, cpu.core_percent.size()); Runner::coreNum_reset = true; Shared::coreCount = cpu.core_percent.size(); while (cmp_less(current_cpu.temp.size(), cpu.core_percent.size() + 1)) current_cpu.temp.push_back({0}); @@ -793,9 +793,9 @@ namespace Cpu { } catch (const std::exception& e) { - Logger::debug("Cpu::collect() : " + string{e.what()}); + Logger::debug("Cpu::collect() : {}", e.what()); if (cread.bad()) throw std::runtime_error("Failed to read /proc/stat"); - else throw std::runtime_error("Cpu::collect() : " + string{e.what()}); + else throw std::runtime_error(fmt::format("Cpu::collect() : {}", e.what())); } if (Config::getB("check_temp") and got_sensors) @@ -1043,7 +1043,7 @@ namespace Mem { } else if (fstype == "zfs") { disks.at(mountpoint).stat = get_zfs_stat_file(dev, zfs_dataset_name_start, zfs_hide_datasets); if (disks.at(mountpoint).stat.empty()) { - Logger::debug("Failed to get ZFS stat file for device " + dev); + Logger::debug("Failed to get ZFS stat file for device {}", dev); } break; } @@ -1057,7 +1057,7 @@ namespace Mem { || (!zfs_hide_datasets && is_directory(disks.at(mountpoint).stat)))) { disks.at(mountpoint).stat = get_zfs_stat_file(dev, zfs_dataset_name_start, zfs_hide_datasets); if (disks.at(mountpoint).stat.empty()) { - Logger::debug("Failed to get ZFS stat file for device " + dev); + Logger::debug("Failed to get ZFS stat file for device {}", dev); } } } @@ -1084,7 +1084,7 @@ namespace Mem { if (std::error_code ec; not fs::exists(mountpoint, ec) or v_contains(ignore_list, mountpoint)) continue; struct statvfs vfs; if (statvfs(mountpoint.c_str(), &vfs) < 0) { - Logger::warning("Failed to get disk/partition stats for mount \""+ mountpoint + "\" with statvfs error code: " + to_string(errno) + ". Ignoring..."); + Logger::warning("Failed to get disk/partition stats for mount \"{}\" with statvfs error code: {}. Ignoring...", mountpoint, to_string(errno)); ignore_list.push_back(mountpoint); new_ignored = true; continue; @@ -1209,14 +1209,14 @@ namespace Mem { while (cmp_greater(disk.io_activity.size(), width * 2)) disk.io_activity.pop_front(); } } else { - Logger::debug("Error in Mem::collect() : when opening " + string{disk.stat}); + Logger::debug("Error in Mem::collect() : when opening {}", disk.stat.string()); } diskread.close(); } old_uptime = uptime; } catch (const std::exception& e) { - Logger::warning("Error in Mem::collect() : " + string{e.what()}); + Logger::warning("Error in Mem::collect() : {}", e.what()); } } @@ -1230,7 +1230,7 @@ namespace Mem { if (access(zfs_pool_stat_path.c_str(), R_OK) == 0) { return zfs_pool_stat_path; } else { - Logger::debug("Cant access folder: " + zfs_pool_stat_path.string()); + Logger::debug("Cant access folder: {}", zfs_pool_stat_path.string()); return ""; } } @@ -1261,7 +1261,7 @@ namespace Mem { if (access(file.path().c_str(), R_OK) == 0) { return file.path(); } else { - Logger::debug("Can't access file: " + file.path().string()); + Logger::debug("Can't access file: {}", file.path().string()); return ""; } } @@ -1270,7 +1270,7 @@ namespace Mem { } } - Logger::debug("Could not read directory: " + zfs_pool_stat_path.string()); + Logger::debug("Could not read directory: {}", zfs_pool_stat_path.string()); return ""; } @@ -1319,7 +1319,7 @@ namespace Mem { // increment read objects counter if no errors were encountered objects_read++; } else { - Logger::debug("Could not read file: " + file.path().string()); + Logger::debug("Could not read file: {}", file.path().string()); } diskread.close(); } @@ -1388,7 +1388,7 @@ namespace Net { getifaddr_wrapper if_wrap {}; if (if_wrap.status != 0) { errors++; - Logger::error("Net::collect() -> getifaddrs() failed with id " + to_string(if_wrap.status)); + Logger::error("Net::collect() -> getifaddrs() failed with id {}", if_wrap.status); redraw = true; return empty_net; } @@ -1425,7 +1425,7 @@ namespace Net { net[iface].ipv4 = ip; } else { int errsv = errno; - Logger::error("Net::collect() -> Failed to convert IPv4 to string for iface " + string(iface) + ", errno: " + strerror(errsv)); + Logger::error("Net::collect() -> Failed to convert IPv4 to string for iface {}, errno: {}", iface, strerror(errsv)); } } } @@ -1436,7 +1436,7 @@ namespace Net { net[iface].ipv6 = ip; } else { int errsv = errno; - Logger::error("Net::collect() -> Failed to convert IPv6 to string for iface " + string(iface) + ", errno: " + strerror(errsv)); + Logger::error("Net::collect() -> Failed to convert IPv6 to string for iface {}, errno: {}", iface, strerror(errsv)); } } } //else, ignoring family==AF_PACKET (see man 3 getifaddrs) which is the first one in the `for` loop.