mirror of https://github.com/aristocratos/btop.git
Added error logger
This commit is contained in:
parent
b5e709da66
commit
342f0af6a8
54
btop.cpp
54
btop.cpp
|
@ -29,6 +29,19 @@ tab-size = 4
|
||||||
#include <filesystem>
|
#include <filesystem>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
namespace Global {
|
||||||
|
const std::vector<std::array<std::string, 2>> Banner_src = {
|
||||||
|
{"#E62525", "██████╗ ████████╗ ██████╗ ██████╗"},
|
||||||
|
{"#CD2121", "██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗ ██╗ ██╗"},
|
||||||
|
{"#B31D1D", "██████╔╝ ██║ ██║ ██║██████╔╝ ██████╗██████╗"},
|
||||||
|
{"#9A1919", "██╔══██╗ ██║ ██║ ██║██╔═══╝ ╚═██╔═╝╚═██╔═╝"},
|
||||||
|
{"#801414", "██████╔╝ ██║ ╚██████╔╝██║ ╚═╝ ╚═╝"},
|
||||||
|
{"#000000", "╚═════╝ ╚═╝ ╚═════╝ ╚═╝"},
|
||||||
|
};
|
||||||
|
|
||||||
|
const std::string Version = "0.0.10";
|
||||||
|
}
|
||||||
|
|
||||||
#include <btop_globs.h>
|
#include <btop_globs.h>
|
||||||
#include <btop_tools.h>
|
#include <btop_tools.h>
|
||||||
#include <btop_config.h>
|
#include <btop_config.h>
|
||||||
|
@ -53,7 +66,7 @@ tab-size = 4
|
||||||
#error OSX support not yet implemented!
|
#error OSX support not yet implemented!
|
||||||
#endif
|
#endif
|
||||||
#else
|
#else
|
||||||
#error Platform not supported or could not determine platform!
|
#error Platform not supported!
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
using std::string, std::vector, std::array, std::map, std::atomic, std::endl, std::cout, std::views::iota, std::list, std::accumulate;
|
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;
|
using namespace Tools;
|
||||||
|
|
||||||
|
|
||||||
//? ------------------------------------------------- GLOBALS ---------------------------------------------------------
|
|
||||||
|
|
||||||
namespace Global {
|
namespace Global {
|
||||||
const vector<array<string, 2>> Banner_src = {
|
|
||||||
{"#E62525", "██████╗ ████████╗ ██████╗ ██████╗"},
|
|
||||||
{"#CD2121", "██╔══██╗╚══██╔══╝██╔═══██╗██╔══██╗ ██╗ ██╗"},
|
|
||||||
{"#B31D1D", "██████╔╝ ██║ ██║ ██║██████╔╝ ██████╗██████╗"},
|
|
||||||
{"#9A1919", "██╔══██╗ ██║ ██║ ██║██╔═══╝ ╚═██╔═╝╚═██╔═╝"},
|
|
||||||
{"#801414", "██████╔╝ ██║ ╚██████╔╝██║ ╚═╝ ╚═╝"},
|
|
||||||
{"#000000", "╚═════╝ ╚═╝ ╚═════╝ ╚═╝"},
|
|
||||||
};
|
|
||||||
|
|
||||||
const string Version = "0.0.1";
|
|
||||||
|
|
||||||
string banner;
|
string banner;
|
||||||
const uint banner_width = 49;
|
const uint banner_width = 49;
|
||||||
|
|
||||||
fs::path conf_dir;
|
fs::path conf_dir;
|
||||||
fs::path conf_file;
|
fs::path conf_file;
|
||||||
fs::path logfile;
|
|
||||||
fs::path theme_folder;
|
fs::path theme_folder;
|
||||||
fs::path user_theme_folder;
|
fs::path user_theme_folder;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//? --------------------------------------- FUNCTIONS, STRUCTS & CLASSES ----------------------------------------------
|
|
||||||
|
|
||||||
//* A simple argument parser
|
//* A simple argument parser
|
||||||
void argumentParser(int argc, char **argv){
|
void argumentParser(int argc, char **argv){
|
||||||
string argument;
|
string argument;
|
||||||
|
@ -156,10 +153,6 @@ int main(int argc, char **argv){
|
||||||
cout.setf(std::ios::boolalpha);
|
cout.setf(std::ios::boolalpha);
|
||||||
if (argc > 1) argumentParser(argc, argv);
|
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)
|
#if defined(LINUX)
|
||||||
//? Linux init
|
//? Linux init
|
||||||
Global::proc_path = (fs::is_directory(fs::path("/proc")) && access("/proc", R_OK) != -1) ? "/proc" : "";
|
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 {
|
else {
|
||||||
Global::conf_file = Global::conf_dir / "btop.conf";
|
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";
|
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();
|
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
|
//? Initialize terminal and set options
|
||||||
if (!Term::init()) {
|
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;
|
cout << "btop++ needs an interactive shell to run." << endl;
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
@ -216,10 +218,12 @@ int main(int argc, char **argv){
|
||||||
|
|
||||||
//* ------------------------------------------------ TESTING ------------------------------------------------------
|
//* ------------------------------------------------ TESTING ------------------------------------------------------
|
||||||
|
|
||||||
int debug = 0;
|
int debug = 1;
|
||||||
int tests = 0;
|
int tests = 0;
|
||||||
bool debuginit = false;
|
bool debuginit = false;
|
||||||
|
|
||||||
|
if (debug > 0) { Logger::loglevel = 4; Logger::debug("Running in debug mode!");}
|
||||||
|
|
||||||
// cout << Theme("main_bg") << Term::clear << flush;
|
// cout << Theme("main_bg") << Term::clear << flush;
|
||||||
bool thread_test = false;
|
bool thread_test = false;
|
||||||
|
|
||||||
|
@ -298,7 +302,6 @@ int main(int argc, char **argv){
|
||||||
cout << "Up for " << sec_to_dhms(round(system_uptime())) << endl;
|
cout << "Up for " << sec_to_dhms(round(system_uptime())) << endl;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//*------>>>>>> Proc testing
|
//*------>>>>>> Proc testing
|
||||||
|
|
||||||
|
|
||||||
|
@ -454,7 +457,6 @@ int main(int argc, char **argv){
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (debug == 1) Input::wait();
|
|
||||||
Term::restore();
|
Term::restore();
|
||||||
if (!debuginit) cout << Term::normal_screen << Term::show_cursor << flush;
|
if (!debuginit) cout << Term::normal_screen << Term::show_cursor << flush;
|
||||||
return 0;
|
return 0;
|
||||||
|
|
|
@ -295,8 +295,7 @@ namespace Proc {
|
||||||
|
|
||||||
|
|
||||||
//* Sort processes vector
|
//* 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) {
|
switch (sortint) {
|
||||||
case 0: return (reverse) ? a.pid < b.pid : a.pid > b.pid;
|
case 0: return (reverse) ? a.pid < b.pid : a.pid > b.pid;
|
||||||
case 1: return (reverse) ? a.name < b.name : a.name > b.name;
|
case 1: return (reverse) ? a.name < b.name : a.name > b.name;
|
||||||
|
@ -341,13 +340,21 @@ namespace Proc {
|
||||||
//* Initialize needed variables for collect
|
//* Initialize needed variables for collect
|
||||||
void init(){
|
void init(){
|
||||||
tstamp = time_ms();
|
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
|
page_size = sysconf(_SC_PAGE_SIZE);
|
||||||
if (page_size <= 0) page_size = 4096;
|
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;
|
uint i = 0;
|
||||||
for (auto& item : sort_array) sort_map[item] = i++;
|
for (auto& item : sort_array) sort_map[item] = i++;
|
||||||
|
|
|
@ -23,12 +23,15 @@ tab-size = 4
|
||||||
#include <cmath>
|
#include <cmath>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <fstream>
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <ctime>
|
#include <ctime>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <iomanip>
|
#include <iomanip>
|
||||||
#include <regex>
|
#include <regex>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
#include <atomic>
|
||||||
|
#include <filesystem>
|
||||||
|
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <termios.h>
|
#include <termios.h>
|
||||||
|
@ -36,7 +39,8 @@ tab-size = 4
|
||||||
|
|
||||||
#include <btop_globs.h>
|
#include <btop_globs.h>
|
||||||
|
|
||||||
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 ------------------------------------------------------
|
//? ------------------------------------------------- NAMESPACES ------------------------------------------------------
|
||||||
|
|
||||||
|
@ -449,7 +453,53 @@ namespace Tools {
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace Logger {
|
namespace Logger {
|
||||||
string cur_date = Tools::strf_time("%Y-%m-%d");
|
namespace {
|
||||||
|
std::atomic<bool> busy (false);
|
||||||
|
fs::path logfile;
|
||||||
|
uint loglevel = 2;
|
||||||
|
bool first = true;
|
||||||
|
string tdf = "%Y/%m/%d (%T) | ";
|
||||||
|
unordered_map<uint, string> 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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue