From 342f0af6a85d1136b1b1e78f38d50a1300d74490 Mon Sep 17 00:00:00 2001 From: aristocratos Date: Sun, 23 May 2021 22:25:07 +0200 Subject: [PATCH] Added error logger --- btop.cpp | 54 +++++++++++++++++++++++++----------------------- src/btop_linux.h | 21 ++++++++++++------- src/btop_tools.h | 54 ++++++++++++++++++++++++++++++++++++++++++++++-- 3 files changed, 94 insertions(+), 35 deletions(-) diff --git a/btop.cpp b/btop.cpp index 66e3192..7ee2905 100644 --- a/btop.cpp +++ b/btop.cpp @@ -29,6 +29,19 @@ tab-size = 4 #include #include +namespace Global { + const std::vector> Banner_src = { + {"#E62525", "██████╗ ████████╗ ██████╗ ██████╗"}, + {"#CD2121", "██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗ ██╗ ██╗"}, + {"#B31D1D", "██████╔╝ ██║ ██║ ██║██████╔╝ ██████╗██████╗"}, + {"#9A1919", "██╔══██╗ ██║ ██║ ██║██╔═══╝ ╚═██╔═╝╚═██╔═╝"}, + {"#801414", "██████╔╝ ██║ ╚██████╔╝██║ ╚═╝ ╚═╝"}, + {"#000000", "╚═════╝ ╚═╝ ╚═════╝ ╚═╝"}, + }; + + const std::string Version = "0.0.10"; +} + #include #include #include @@ -53,7 +66,7 @@ tab-size = 4 #error OSX support not yet implemented! #endif #else - #error Platform not supported or could not determine platform! + #error Platform not supported! #endif using std::string, std::vector, std::array, std::map, std::atomic, std::endl, std::cout, std::views::iota, std::list, std::accumulate; @@ -62,33 +75,17 @@ namespace fs = std::filesystem; using namespace Tools; -//? ------------------------------------------------- GLOBALS --------------------------------------------------------- - namespace Global { - const vector> Banner_src = { - {"#E62525", "██████╗ ████████╗ ██████╗ ██████╗"}, - {"#CD2121", "██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗ ██╗ ██╗"}, - {"#B31D1D", "██████╔╝ ██║ ██║ ██║██████╔╝ ██████╗██████╗"}, - {"#9A1919", "██╔══██╗ ██║ ██║ ██║██╔═══╝ ╚═██╔═╝╚═██╔═╝"}, - {"#801414", "██████╔╝ ██║ ╚██████╔╝██║ ╚═╝ ╚═╝"}, - {"#000000", "╚═════╝ ╚═╝ ╚═════╝ ╚═╝"}, - }; - - const string Version = "0.0.1"; - string banner; const uint banner_width = 49; fs::path conf_dir; fs::path conf_file; - fs::path logfile; fs::path theme_folder; fs::path user_theme_folder; } -//? --------------------------------------- FUNCTIONS, STRUCTS & CLASSES ---------------------------------------------- - //* A simple argument parser void argumentParser(int argc, char **argv){ string argument; @@ -156,10 +153,6 @@ int main(int argc, char **argv){ cout.setf(std::ios::boolalpha); if (argc > 1) argumentParser(argc, argv); - if (!string(getenv("LANG")).ends_with("UTF-8") && !string(getenv("LANG")).ends_with("utf-8")) { - cout << "WARNING: No UTF-8 locale was detected! Symbols might not look as intended." << endl; - } - #if defined(LINUX) //? Linux init Global::proc_path = (fs::is_directory(fs::path("/proc")) && access("/proc", R_OK) != -1) ? "/proc" : ""; @@ -182,7 +175,7 @@ int main(int argc, char **argv){ } else { Global::conf_file = Global::conf_dir / "btop.conf"; - Global::logfile = Global::conf_dir / "btop.log"; + Logger::logfile = Global::conf_dir / "btop.log"; Global::user_theme_folder = Global::conf_dir / "themes"; if (!fs::exists(Global::user_theme_folder) && !fs::create_directory(Global::user_theme_folder)) Global::user_theme_folder.clear(); } @@ -194,10 +187,19 @@ int main(int argc, char **argv){ } } + string err_msg; + + if (!string(getenv("LANG")).ends_with("UTF-8") && !string(getenv("LANG")).ends_with("utf-8")) { + err_msg = "No UTF-8 locale was detected! Symbols might not look as intended."; + Logger::warning(err_msg); + cout << "WARNING: " << err_msg << endl; + } //? Initialize terminal and set options if (!Term::init()) { - cout << "ERROR: No tty detected!" << endl; + err_msg = "No tty detected!"; + Logger::error(err_msg + " Quitting."); + cout << "ERROR: " << err_msg << endl; cout << "btop++ needs an interactive shell to run." << endl; exit(1); } @@ -216,10 +218,12 @@ int main(int argc, char **argv){ //* ------------------------------------------------ TESTING ------------------------------------------------------ - int debug = 0; + int debug = 1; int tests = 0; bool debuginit = false; + if (debug > 0) { Logger::loglevel = 4; Logger::debug("Running in debug mode!");} + // cout << Theme("main_bg") << Term::clear << flush; bool thread_test = false; @@ -298,7 +302,6 @@ int main(int argc, char **argv){ cout << "Up for " << sec_to_dhms(round(system_uptime())) << endl; - //*------>>>>>> Proc testing @@ -454,7 +457,6 @@ int main(int argc, char **argv){ } } - if (debug == 1) Input::wait(); Term::restore(); if (!debuginit) cout << Term::normal_screen << Term::show_cursor << flush; return 0; diff --git a/src/btop_linux.h b/src/btop_linux.h index 5cf64d2..d325eb8 100644 --- a/src/btop_linux.h +++ b/src/btop_linux.h @@ -295,8 +295,7 @@ namespace Proc { //* Sort processes vector - std::ranges::sort(procs, [&sortint, &reverse](proc_info& a, proc_info& b) - { + std::ranges::sort(procs, [&sortint, &reverse](proc_info& a, proc_info& b) { switch (sortint) { case 0: return (reverse) ? a.pid < b.pid : a.pid > b.pid; case 1: return (reverse) ? a.name < b.name : a.name > b.name; @@ -341,13 +340,21 @@ namespace Proc { //* Initialize needed variables for collect void init(){ tstamp = time_ms(); - passwd_path = (access("/etc/passwd", R_OK) != -1) ? fs::path("/etc/passwd") : passwd_path; //! add logger error + passwd_path = (access("/etc/passwd", R_OK) != -1) ? fs::path("/etc/passwd") : passwd_path; + if (passwd_path.empty()) Logger::warning("Could not read /etc/passwd, will show UID instead of username."); - page_size = sysconf(_SC_PAGE_SIZE); //! add logger error - if (page_size <= 0) page_size = 4096; + page_size = sysconf(_SC_PAGE_SIZE); + if (page_size <= 0) { + page_size = 4096; + Logger::warning("Could not get system page size. Defaulting to 4096, processes memory usage might be incorrect."); + } - clk_tck = sysconf(_SC_CLK_TCK); //! add logger error - if (clk_tck <= 0) clk_tck = 100; + + clk_tck = sysconf(_SC_CLK_TCK); + if (clk_tck <= 0) { + clk_tck = 100; + Logger::warning("Could not get system clocks per second. Defaulting to 100, processes cpu usage might be incorrect."); + } uint i = 0; for (auto& item : sort_array) sort_map[item] = i++; diff --git a/src/btop_tools.h b/src/btop_tools.h index a127acf..27217ad 100644 --- a/src/btop_tools.h +++ b/src/btop_tools.h @@ -23,12 +23,15 @@ tab-size = 4 #include #include #include +#include #include #include #include #include #include #include +#include +#include #include #include @@ -36,7 +39,8 @@ tab-size = 4 #include -using std::string, std::vector, std::regex, std::max, std::to_string, std::cin; +using std::string, std::vector, std::regex, std::max, std::to_string, std::cin, std::atomic; +namespace fs = std::filesystem; //? ------------------------------------------------- NAMESPACES ------------------------------------------------------ @@ -449,7 +453,53 @@ namespace Tools { } namespace Logger { - string cur_date = Tools::strf_time("%Y-%m-%d"); + namespace { + std::atomic busy (false); + fs::path logfile; + uint loglevel = 2; + bool first = true; + string tdf = "%Y/%m/%d (%T) | "; + unordered_map log_levels = { + { 0, "DISABLED" }, + { 1, "ERROR" }, + { 2, "WARNING" }, + { 3, "INFO" }, + { 4, "DEBUG" } + }; + } + + void log_write(uint level, string& msg){ + if (logfile.empty() || loglevel < level) return; + while (busy.load()) Tools::sleep_ms(1); + busy.store(true); + if (fs::exists(logfile) && fs::file_size(logfile) > 1024 << 10) { + auto old_log = logfile; + old_log += ".1"; + if (fs::exists(old_log)) fs::remove(old_log); + fs::rename(logfile, old_log); + } + std::ofstream lwrite(logfile, std::ios::app); + if (first) { first = false; lwrite << "\n" << Tools::strf_time(tdf) << "----> btop++ v." << Global::Version << "\n";} + lwrite << Tools::strf_time(tdf) << log_levels[level] << ": " << msg << "\n"; + lwrite.close(); + busy.store(false); + } + + void error(string msg){ + log_write(1, msg); + } + + void warning(string msg){ + log_write(2, msg); + } + + void info(string msg){ + log_write(3, msg); + } + + void debug(string msg){ + log_write(4, msg); + } }