diff --git a/src/btop.cpp b/src/btop.cpp index bc75a2135..ac8b0cdd9 100644 --- a/src/btop.cpp +++ b/src/btop.cpp @@ -284,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); @@ -892,15 +892,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; @@ -910,7 +910,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); } } } @@ -943,7 +943,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"); @@ -961,7 +961,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 @@ -970,7 +970,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 16ec5c2c2..800357d1f 100644 --- a/src/btop_draw.cpp +++ b/src/btop_draw.cpp @@ -226,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 ""; } } diff --git a/src/btop_log.cpp b/src/btop_log.cpp index f7b7a652d..eb4fc0054 100644 --- a/src/btop_log.cpp +++ b/src/btop_log.cpp @@ -6,6 +6,9 @@ #include #include #include +#include + +#include #include "btop_shared.hpp" #include "btop_tools.hpp" @@ -40,7 +43,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 +67,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 +77,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 0db2a1f64..96913d3c4 100644 --- a/src/btop_menu.cpp +++ b/src/btop_menu.cpp @@ -1367,7 +1367,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", "show_gpu_info") 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 c884facce..86de978ba 100644 --- a/src/btop_tools.cpp +++ b/src/btop_tools.cpp @@ -519,7 +519,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); @@ -596,15 +596,15 @@ namespace Tools { report_line = fmt::format(custom_locale, "Timer [{}] took {:L} μs", name, elapsed_time); if (delayed_report) - report_buffer.emplace_back(report_line); + report_buffer.push_back(std::move(report_line)); else - Logger::debug(report_line); + Logger::debug("{}", report_line); } void DebugTimer::force_report() { if (report_buffer.empty()) return; for (const auto& line : report_buffer) - Logger::debug(line); + Logger::debug("{}", line); report_buffer.clear(); } diff --git a/src/freebsd/btop_collect.cpp b/src/freebsd/btop_collect.cpp index 0cb8b3066..2a2973cad 100644 --- a/src/freebsd/btop_collect.cpp +++ b/src/freebsd/btop_collect.cpp @@ -16,9 +16,6 @@ indent = tab tab-size = 4 */ #include -#include -#include -#include #include // man 3 getifaddrs: "BUGS: If both and are being included, must be included before " #include @@ -42,7 +39,6 @@ tab-size = 4 #include #include #include -#include #include #include #include @@ -50,14 +46,20 @@ tab-size = 4 #include #include -#include #include +#include +#include +#include #include +#include #include #include #include +#include #include -#include +#include + +#include #include "../btop_config.hpp" #include "../btop_log.hpp" @@ -448,8 +450,8 @@ namespace Cpu { if (cpu.core_percent.at(i).size() > 40) cpu.core_percent.at(i).pop_front(); } catch (const std::exception &e) { - Logger::error("Cpu::collect() : " + (string)e.what()); - throw std::runtime_error("collect() : " + (string)e.what()); + Logger::error("Cpu::collect() : {}", e.what()); + throw std::runtime_error(fmt::format("collect() : {}", e.what())); } } @@ -552,7 +554,7 @@ namespace Mem { size_t len = 512; if (fgets(poolName, len, poolPipe())) { poolName[strcspn(poolName, "\n")] = 0; - Logger::debug("zpool found: " + string(poolName)); + Logger::debug("zpool found: {}", poolName); Mem::zpools.push_back(std::regex_replace(poolName, toReplace, "%25")); } } @@ -577,7 +579,7 @@ namespace Mem { devstat_compute_statistics(&d, nullptr, etime, DSM_TOTAL_BYTES_READ, &total_bytes_read, DSM_TOTAL_BYTES_WRITE, &total_bytes_write, DSM_NONE); assign_values(disk, total_bytes_read, total_bytes_write); string mountpoint = mapping.at(disk.dev); - Logger::debug("dev " + devStatName + " -> " + mountpoint + " read=" + std::to_string(total_bytes_read) + " write=" + std::to_string(total_bytes_write)); + Logger::debug("dev {} -> {} read={} write={}", devStatName, mountpoint, total_bytes_read, total_bytes_write); } } @@ -767,7 +769,7 @@ namespace Mem { continue; struct statvfs vfs; if (statvfs(mountpoint.c_str(), &vfs) < 0) { - Logger::warning("Failed to get disk/partition stats with statvfs() for: " + mountpoint); + Logger::warning("Failed to get disk/partition stats with statvfs() for: {}", mountpoint); continue; } disk.total = vfs.f_blocks * vfs.f_frsize; @@ -841,7 +843,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; } @@ -876,7 +878,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)); } } } @@ -887,7 +889,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_LINK (see man 3 getifaddrs) diff --git a/src/linux/btop_collect.cpp b/src/linux/btop_collect.cpp index d1f7a5578..d36c7b6be 100644 --- a/src/linux/btop_collect.cpp +++ b/src/linux/btop_collect.cpp @@ -588,7 +588,7 @@ namespace Cpu { if (++failed < 5) return ""s; else { - Logger::warning("get_cpuHZ() : " + string{e.what()}); + Logger::warning("get_cpuHZ() : {}", e.what()); return ""s; } } @@ -916,7 +916,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}); @@ -924,9 +924,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) @@ -949,7 +949,7 @@ namespace Gpu { //? Dynamic loading & linking nvml_dl_handle = dlopen("libnvidia-ml.so", RTLD_LAZY); if (!nvml_dl_handle) { - Logger::info(std::string("Failed to load libnvidia-ml.so, NVIDIA GPUs will not be detected: ") + dlerror()); + Logger::info("Failed to load libnvidia-ml.so, NVIDIA GPUs will not be detected: {}", dlerror()); return false; } @@ -957,7 +957,7 @@ namespace Gpu { auto sym = dlsym(nvml_dl_handle, sym_name); auto err = dlerror(); if (err != NULL) { - Logger::error(string("NVML: Couldn't find function ") + sym_name + ": " + err); + Logger::error("NVML: Couldn't find function {}: {}", sym_name, err); return (void*)nullptr; } else return sym; }; @@ -985,14 +985,14 @@ namespace Gpu { //? Function calls nvmlReturn_t result = nvmlInit(); if (result != NVML_SUCCESS) { - Logger::debug(std::string("Failed to initialize NVML, NVIDIA GPUs will not be detected: ") + nvmlErrorString(result)); + Logger::debug("Failed to initialize NVML, NVIDIA GPUs will not be detected: {}", nvmlErrorString(result)); return false; } //? Device count result = nvmlDeviceGetCount(&device_count); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get device count: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get device count: {}", nvmlErrorString(result)); return false; } @@ -1016,7 +1016,7 @@ namespace Gpu { if (NVML_SUCCESS == result) { initialized = false; dlclose(nvml_dl_handle); - } else Logger::warning(std::string("Failed to shutdown NVML: ") + nvmlErrorString(result)); + } else Logger::warning("Failed to shutdown NVML: {}", nvmlErrorString(result)); return !initialized; } @@ -1033,7 +1033,7 @@ namespace Gpu { //? Device Handle result = nvmlDeviceGetHandleByIndex(i, devices.data() + i); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get device handle: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get device handle: {}", nvmlErrorString(result)); gpus[i].supported_functions = {false, false, false, false, false, false, false, false}; continue; } @@ -1042,7 +1042,7 @@ namespace Gpu { char name[NVML_DEVICE_NAME_BUFFER_SIZE]; result = nvmlDeviceGetName(devices[i], name, NVML_DEVICE_NAME_BUFFER_SIZE); if (result != NVML_SUCCESS) - Logger::warning(std::string("NVML: Failed to get device name: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get device name: {}", nvmlErrorString(result)); else { gpu_names[i] = string(name); for (const auto& brand : {"NVIDIA", "Nvidia", "(R)", "(TM)"}) { @@ -1055,7 +1055,7 @@ namespace Gpu { unsigned int max_power; result = nvmlDeviceGetPowerManagementLimit(devices[i], &max_power); if (result != NVML_SUCCESS) - Logger::warning(std::string("NVML: Failed to get maximum GPU power draw, defaulting to 225W: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get maximum GPU power draw, defaulting to 225W: {}", nvmlErrorString(result)); else { gpus[i].pwr_max_usage = max_power; // RSMI reports power in microWatts gpu_pwr_total_max += max_power; @@ -1065,7 +1065,7 @@ namespace Gpu { unsigned int temp_max; result = nvmlDeviceGetTemperatureThreshold(devices[i], NVML_TEMPERATURE_THRESHOLD_SHUTDOWN, &temp_max); if (result != NVML_SUCCESS) - Logger::warning(std::string("NVML: Failed to get maximum GPU temperature, defaulting to 110°C: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get maximum GPU temperature, defaulting to 110°C: {}", nvmlErrorString(result)); else gpus[i].temp_max = (long long)temp_max; } @@ -1075,7 +1075,7 @@ namespace Gpu { unsigned int tx; nvmlReturn_t result = nvmlDeviceGetPcieThroughput(devices[i], NVML_PCIE_UTIL_TX_BYTES, &tx); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get PCIe TX throughput: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get PCIe TX throughput: {}", nvmlErrorString(result)); if constexpr(is_init) gpus_slice[i].supported_functions.pcie_txrx = false; } else gpus_slice[i].pcie_tx = (long long)tx; }); @@ -1084,7 +1084,7 @@ namespace Gpu { unsigned int rx; nvmlReturn_t result = nvmlDeviceGetPcieThroughput(devices[i], NVML_PCIE_UTIL_RX_BYTES, &rx); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get PCIe RX throughput: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get PCIe RX throughput: {}", nvmlErrorString(result)); } else gpus_slice[i].pcie_rx = (long long)rx; }); } @@ -1095,7 +1095,7 @@ namespace Gpu { nvmlUtilization_t utilization; result = nvmlDeviceGetUtilizationRates(devices[i], &utilization); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get GPU utilization: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get GPU utilization: {}", nvmlErrorString(result)); if constexpr(is_init) gpus_slice[i].supported_functions.gpu_utilization = false; if constexpr(is_init) gpus_slice[i].supported_functions.mem_utilization = false; } else { @@ -1110,7 +1110,7 @@ namespace Gpu { unsigned int gpu_clock; result = nvmlDeviceGetClockInfo(devices[i], NVML_CLOCK_GRAPHICS, &gpu_clock); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get GPU clock speed: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get GPU clock speed: {}", nvmlErrorString(result)); if constexpr(is_init) gpus_slice[i].supported_functions.gpu_clock = false; } else gpus_slice[i].gpu_clock_speed = (long long)gpu_clock; } @@ -1119,7 +1119,7 @@ namespace Gpu { unsigned int mem_clock; result = nvmlDeviceGetClockInfo(devices[i], NVML_CLOCK_MEM, &mem_clock); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get VRAM clock speed: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get VRAM clock speed: {}", nvmlErrorString(result)); if constexpr(is_init) gpus_slice[i].supported_functions.mem_clock = false; } else gpus_slice[i].mem_clock_speed = (long long)mem_clock; } @@ -1130,7 +1130,7 @@ namespace Gpu { unsigned int power; result = nvmlDeviceGetPowerUsage(devices[i], &power); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get GPU power usage: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get GPU power usage: {}", nvmlErrorString(result)); if constexpr(is_init) gpus_slice[i].supported_functions.pwr_usage = false; } else { gpus_slice[i].pwr_usage = (long long)power; @@ -1142,7 +1142,7 @@ namespace Gpu { nvmlPstates_t pState; result = nvmlDeviceGetPowerState(devices[i], &pState); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get GPU power state: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get GPU power state: {}", nvmlErrorString(result)); if constexpr(is_init) gpus_slice[i].supported_functions.pwr_state = false; } else gpus_slice[i].pwr_state = static_cast(pState); } @@ -1154,7 +1154,7 @@ namespace Gpu { unsigned int temp; nvmlReturn_t result = nvmlDeviceGetTemperature(devices[i], NVML_TEMPERATURE_GPU, &temp); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get GPU temperature: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get GPU temperature: {}", nvmlErrorString(result)); if constexpr(is_init) gpus_slice[i].supported_functions.temp_info = false; } else gpus_slice[i].temp.push_back((long long)temp); } @@ -1166,7 +1166,7 @@ namespace Gpu { nvmlMemory_t memory; result = nvmlDeviceGetMemoryInfo(devices[i], &memory); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get VRAM info: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get VRAM info: {}", nvmlErrorString(result)); if constexpr(is_init) gpus_slice[i].supported_functions.mem_total = false; if constexpr(is_init) gpus_slice[i].supported_functions.mem_used = false; } else { @@ -1184,7 +1184,7 @@ namespace Gpu { nvmlProcessInfo_t* proc_info = 0; result = nvmlDeviceGetComputeRunningProcesses_v3(device, &proc_info_len, proc_info); if (result != NVML_SUCCESS) { - Logger::warning(std::string("NVML: Failed to get compute processes: ") + nvmlErrorString(result)); + Logger::warning("NVML: Failed to get compute processes: {}", nvmlErrorString(result)); } else { for (unsigned int i = 0; i < proc_info_len; ++i) gpus_slice[i].graphics_processes.push_back({proc_info[i].pid, proc_info[i].usedGpuMemory}); @@ -1217,7 +1217,7 @@ namespace Gpu { rsmi_dl_handle = dlopen("librocm_smi64.so", RTLD_LAZY); } if (!rsmi_dl_handle) { - Logger::debug(std::string("Failed to load librocm_smi64.so, AMD GPUs will not be detected: ") + dlerror()); + Logger::debug("Failed to load librocm_smi64.so, AMD GPUs will not be detected: {}", dlerror()); return false; } @@ -1225,7 +1225,7 @@ namespace Gpu { auto sym = dlsym(rsmi_dl_handle, sym_name); auto err = dlerror(); if (err != NULL) { - Logger::error(string("ROCm SMI: Couldn't find function ") + sym_name + ": " + err); + Logger::error("ROCm SMI: Couldn't find function {}: {}", sym_name, err); return (void*)nullptr; } else return sym; }; @@ -1718,7 +1718,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; } @@ -1732,7 +1732,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); } } } @@ -1770,7 +1770,7 @@ namespace Mem { auto promise_res = promises_it->second.get(); if(promise_res.second != -1){ ignore_list.push_back(mountpoint); - Logger::warning("Failed to get disk/partition stats for mount \""+ mountpoint + "\" with statvfs error code: " + to_string(promise_res.second) + ". Ignoring..."); + Logger::warning("Failed to get disk/partition stats for mount \"{}\" with statvfs error code: {}. Ignoring...", mountpoint, promise_res.second); it = disks.erase(it); continue; } @@ -1900,14 +1900,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()); } } @@ -1921,7 +1921,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 ""; } } @@ -1952,7 +1952,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 ""; } } @@ -1961,7 +1961,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 ""; } @@ -2010,7 +2010,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(); } @@ -2079,7 +2079,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; } @@ -2116,7 +2116,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)); } } } @@ -2127,7 +2127,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. diff --git a/src/osx/btop_collect.cpp b/src/osx/btop_collect.cpp index 76a5ddcf9..360f825e4 100644 --- a/src/osx/btop_collect.cpp +++ b/src/osx/btop_collect.cpp @@ -43,15 +43,17 @@ tab-size = 4 #include #include // for inet_ntop #include -#include #include #include #include #include #include +#include #include +#include + #include "../btop_config.hpp" #include "../btop_log.hpp" #include "../btop_shared.hpp" @@ -248,7 +250,7 @@ namespace Cpu { } bool get_sensors() { - Logger::debug("get_sensors(): show_coretemp=" + std::to_string(Config::getB("show_coretemp")) + " check_temp=" + std::to_string(Config::getB("check_temp"))); + Logger::debug("get_sensors(): show_coretemp={} check_temp={}", Config::getB("show_coretemp"), Config::getB("check_temp")); got_sensors = false; if (Config::getB("show_coretemp") and Config::getB("check_temp")) { ThermalSensors sensors; @@ -497,8 +499,8 @@ namespace Cpu { if (cpu.core_percent.at(i).size() > 40) cpu.core_percent.at(i).pop_front(); } catch (const std::exception &e) { - Logger::error("Cpu::collect() : " + (string)e.what()); - throw std::runtime_error("collect() : " + (string)e.what()); + Logger::error("Cpu::collect() : {}", e.what()); + throw std::runtime_error(fmt::format("collect() : {}", e.what())); } } @@ -781,7 +783,7 @@ namespace Mem { continue; struct statvfs vfs; if (statvfs(mountpoint.c_str(), &vfs) < 0) { - Logger::warning("Failed to get disk/partition stats with statvfs() for: " + mountpoint); + Logger::warning("Failed to get disk/partition stats with statvfs() for: {}", mountpoint); continue; } disk.total = vfs.f_blocks * vfs.f_frsize; @@ -855,7 +857,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; } @@ -888,7 +890,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)); } } } @@ -899,7 +901,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_LINK (see man 3 getifaddrs)