btop/src/btop_tools.cpp
aristocratos 19bcff894b Squashed commit of the following:
commit 0267eba2bb
Merge: 50bbab0 e81cf2b
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Wed Nov 15 21:43:18 2023 +0100

    Merge pull request #659 from ivanp7/patch-1

    Add alternative key codes for Delete, Insert, Home, End

commit 50bbab0512
Merge: 9edbf27 5a14c7b
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Wed Nov 15 21:35:50 2023 +0100

    Merge pull request #660 from stradicat/feature/elementarish

    Elementarish theme: color update according to Elementary palette

commit 5a14c7b6fa
Merge: 979506f 71eb414
Author: Dennis Mayr <dmayr.dev@gmail.com>
Date:   Wed Nov 15 17:27:34 2023 -0300

    Merge branch 'main' of https://github.com/stradicat/btop

commit 979506f18e
Author: Dennis Mayr <dmayr.dev@gmail.com>
Date:   Wed Nov 8 11:17:47 2023 -0300

    Elementarish theme: color update according to Elementary palette

commit 71eb4142e8
Author: Dennis Mayr <dmayr.dev@gmail.com>
Date:   Wed Nov 8 11:17:47 2023 -0300

    Elementarish theme: color update according to Elementary palette

commit e81cf2b7ff
Author: vân <3432246+ivanp7@users.noreply.github.com>
Date:   Tue Nov 7 15:12:27 2023 +0000

    Add alternative key codes for Insert, Home, End

commit f9452ff6d5
Author: vân <3432246+ivanp7@users.noreply.github.com>
Date:   Mon Nov 6 13:31:53 2023 +0000

    Add alternative Delete key code

    Delete key not always produces ^[[3~, on some terminals (like st) it produces ^[[P.

commit 9edbf27f1b
Merge: 2a864f6 ff1f51c
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sat Oct 21 02:09:55 2023 +0200

    Merge pull request #649 from nobounce/workflow-timeout

    Set FreeBSD workflow timeout

commit ff1f51ccbb
Author: Steffen Winter <steffen.winter@proton.me>
Date:   Wed Oct 18 22:26:36 2023 +0200

    Set FreeBSD workflow timeout

    Recently the FreeBSD workflow has started to hang in a boot loop when
    the VM starts up. The issue is being tracked upstream but there is not
    response at the moment.

    To work around this set a timeout to not waste CI minutes. Other
    workflows might also want this change since they don't take 20 minutes
    anyway.

commit 2a864f6f2e
Merge: 636eb25 b2bf8ef
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sat Oct 7 10:40:54 2023 +0200

    Merge pull request #643 from DecklynKern/main

    Fix scrollbar not clearing sometimes.

commit b2bf8ef504
Author: DecklynKern <DecklynKern@gmail.com>
Date:   Fri Oct 6 17:33:38 2023 -0600

    Fix scrollbar not clearing sometimes.

commit 636eb25f5e
Merge: 260c0f6 b5ba2fc
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sat Sep 30 19:51:03 2023 +0200

    Merge pull request #623 from rahulaggarwal965/main

    Add keybind for toggling memory display mode in PROC box

commit b5ba2fc963
Author: Rahul Aggarwal <rahulaggarwal965@gmail.com>
Date:   Wed Sep 20 22:55:56 2023 -0400

    Add keybind for toggling memory display mode in PROC box

commit 260c0f6623
Merge: 52bfff7 e6a06eb
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sat Sep 30 18:56:25 2023 +0200

    Merge pull request #635 from lvxnull/editorconfig

    Add hpp files to .editorconfig

commit e6a06eb729
Author: lvxnull <86745229+lvxnull@users.noreply.github.com>
Date:   Thu Sep 28 19:44:47 2023 +0200

    Add hpp files to .editorconfig

commit 52bfff7ceb
Merge: 1f72e56 19dbbe1
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Sat Sep 30 18:55:08 2023 +0200

    Merge pull request #636 from nobounce/performance-iili

    Minor string initialization improvement

commit 19dbbe1a17
Author: nobounce <steffen.winter@proton.me>
Date:   Fri Sep 29 12:20:59 2023 +0200

    Minor string initialization improvement

commit 1f72e56c7d
Merge: 278a0e6 cdcf8bc
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Fri Sep 29 10:43:21 2023 +0200

    Merge pull request #633 from crestfallnatwork/main

    [fix] Made disks statvfs logic asynchronous.

commit cdcf8bc929
Author: crestfalln <guptahiman01@gmail.com>
Date:   Fri Sep 29 09:07:27 2023 +0530

    fixed bug where updated disks stats overrided disk io data

commit 9b4e85f08d
Author: crestfalln <no-reply@crestfalln.com>
Date:   Thu Sep 28 04:57:05 2023 +0530

    fixed bug where updated disks stats overrided disk io data

commit 889623874e
Author: crestfalln <no-reply@crestfalln.com>
Date:   Wed Sep 27 23:57:06 2023 +0530

    made disks stat logic async

commit 278a0e6b17
Merge: d16adc9 e89519f
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Thu Sep 28 18:32:09 2023 +0200

    Merge pull request #630 from lvxnull/signal-list

    Fix signal list on non-linux/weird linux platforms

commit e89519fbb2
Author: lvxnull <86745229+lvxnull@users.noreply.github.com>
Date:   Sun Sep 24 21:44:38 2023 +0200

    Fix signal list on non-linux/weird linux platforms

commit d16adc9fd0
Merge: 2c3ac48 f34b408
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Thu Sep 28 18:20:42 2023 +0200

    Merge pull request #618 from nobounce/aggregate-child-processes

    Add option to accumulate a child's resources in parent in tree-view

commit f34b40892f
Author: nobounce <steffen.winter@proton.me>
Date:   Sun Sep 24 16:34:50 2023 +0200

    Make process thread count better readable when wider than 5 digits

commit 6027cedd42
Author: nobounce <steffen.winter@proton.me>
Date:   Thu Sep 14 23:27:05 2023 +0200

    Add option to accumulate a child's resources in parent in tree-view

commit 2c3ac4855d
Merge: f90dc37 5c6a281
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Wed Sep 13 21:14:56 2023 +0200

    Merge pull request #589 from nobounce/cmake

    Add CMake support for Linux

commit f90dc37c26
Merge: 0cac861 68a49c1
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Wed Sep 13 20:27:05 2023 +0200

    Merge pull request #610 from SidVeld/feature/horizon-theme

    Horizon theme

commit 5c6a281002
Author: nobounce <steffen.winter@proton.me>
Date:   Tue Aug 29 20:39:00 2023 +0200

    Add CMake support

    Linux is completly supported

    FreeBSD is not able to create a static executable for now. See
    https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=273398

    MacOS was not tested

commit 68a49c10a6
Author: SidVeld <sidveld@gmail.com>
Date:   Wed Sep 6 18:03:31 2023 +0300

    Add horizon theme

commit 0cac861910
Merge: 31be436 f798acd
Author: Jakob P. Liljenberg <admin@qvantnet.com>
Date:   Tue Sep 5 19:27:38 2023 +0200

    Merge pull request #609 from scorpion-26/byteconv

    Fix short conversion of 1000-1023 *iB

commit f798acdaf7
Author: scorpion-26 <dev.scorpion26@gmail.com>
Date:   Tue Sep 5 18:00:47 2023 +0200

    Fix short conversion of 1000-1023*iB

    floating_humanizer([1000-1024], true) with base 8 returns "2K", whereas it should return
    "1.0K" to align with other formats. The conversion is also broken for
    all other units(e.g. 1023M is also broken and returns "2G")

commit 31be4362ce
Author: aristocratos <gnmjpl@gmail.com>
Date:   Sun Aug 27 02:00:07 2023 +0200

    FreeBSD Github action 13.1 -> 13.2 and static libgcc and libstdc++

commit fc523fd1d0
Author: aristocratos <gnmjpl@gmail.com>
Date:   Sun Aug 27 01:36:26 2023 +0200

    Fix for FreeBSD github action not failing "correctly"...
2023-11-25 21:01:11 +01:00

681 lines
18 KiB
C++

/* Copyright 2021 Aristocratos (jakob@qvantnet.com)
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
indent = tab
tab-size = 4
*/
#include <cmath>
#include <codecvt>
#include <iostream>
#include <fstream>
#include <ctime>
#include <sstream>
#include <iomanip>
#include <utility>
#include <ranges>
#include <unistd.h>
#include <termios.h>
#include <sys/ioctl.h>
#include "robin_hood.h"
#include "widechar_width.hpp"
#include "btop_shared.hpp"
#include "btop_tools.hpp"
#include "btop_config.hpp"
using std::cin;
using std::cout;
using std::floor;
using std::flush;
using std::max;
using std::string_view;
using std::to_string;
using robin_hood::unordered_flat_map;
using namespace std::literals; // to use operator""s
namespace fs = std::filesystem;
namespace rng = std::ranges;
//? ------------------------------------------------- NAMESPACES ------------------------------------------------------
//* Collection of escape codes and functions for terminal manipulation
namespace Term {
atomic<bool> initialized{}; // defaults to false
atomic<int> width{}; // defaults to 0
atomic<int> height{}; // defaults to 0
string current_tty;
namespace {
struct termios initial_settings;
//* Toggle terminal input echo
bool echo(bool on=true) {
struct termios settings;
if (tcgetattr(STDIN_FILENO, &settings)) return false;
if (on) settings.c_lflag |= ECHO;
else settings.c_lflag &= ~(ECHO);
return 0 == tcsetattr(STDIN_FILENO, TCSANOW, &settings);
}
//* Toggle need for return key when reading input
bool linebuffered(bool on=true) {
struct termios settings;
if (tcgetattr(STDIN_FILENO, &settings)) return false;
if (on) settings.c_lflag |= ICANON;
else settings.c_lflag &= ~(ICANON);
if (tcsetattr(STDIN_FILENO, TCSANOW, &settings)) return false;
if (on) setlinebuf(stdin);
else setbuf(stdin, nullptr);
return true;
}
}
bool refresh(bool only_check) {
struct winsize w;
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) < 0) return false;
if (width != w.ws_col or height != w.ws_row) {
if (not only_check) {
width = w.ws_col;
height = w.ws_row;
}
return true;
}
return false;
}
auto get_min_size(const string& boxes) -> array<int, 2> {
bool cpu = boxes.find("cpu") != string::npos;
bool mem = boxes.find("mem") != string::npos;
bool net = boxes.find("net") != string::npos;
bool proc = boxes.find("proc") != string::npos;
#ifdef GPU_SUPPORT
int gpu = 0;
if (not Gpu::gpu_names.empty())
for (char i = '0'; i <= '5'; ++i)
gpu += (boxes.find(std::string("gpu") + i) != string::npos);
#endif
int width = 0;
if (mem) width = Mem::min_width;
else if (net) width = Mem::min_width;
width += (proc ? Proc::min_width : 0);
if (cpu and width < Cpu::min_width) width = Cpu::min_width;
#ifdef GPU_SUPPORT
if (gpu != 0 and width < Gpu::min_width) width = Gpu::min_width;
#endif
int height = (cpu ? Cpu::min_height : 0);
if (proc) height += Proc::min_height;
else height += (mem ? Mem::min_height : 0) + (net ? Net::min_height : 0);
#ifdef GPU_SUPPORT
height += Gpu::min_height*gpu;
#endif
return { width, height };
}
bool init() {
if (not initialized) {
initialized = (bool)isatty(STDIN_FILENO);
if (initialized) {
tcgetattr(STDIN_FILENO, &initial_settings);
current_tty = (ttyname(STDIN_FILENO) != nullptr ? static_cast<string>(ttyname(STDIN_FILENO)) : "unknown");
//? Disable stream sync
cin.sync_with_stdio(false);
cout.sync_with_stdio(false);
//? Disable stream ties
cin.tie(nullptr);
cout.tie(nullptr);
echo(false);
linebuffered(false);
refresh();
cout << alt_screen << hide_cursor << mouse_on << flush;
Global::resized = false;
}
}
return initialized;
}
void restore() {
if (initialized) {
tcsetattr(STDIN_FILENO, TCSANOW, &initial_settings);
cout << mouse_off << clear << Fx::reset << normal_screen << show_cursor << flush;
initialized = false;
}
}
}
//? --------------------------------------------------- FUNCTIONS -----------------------------------------------------
// ! Disabled due to issue when compiling with musl, reverted back to using regex
// namespace Fx {
// string uncolor(const string& s) {
// string out = s;
// for (size_t offset = 0, start_pos = 0, end_pos = 0;;) {
// start_pos = (offset == 0) ? out.find('\x1b') : offset;
// if (start_pos == string::npos)
// break;
// offset = start_pos + 1;
// end_pos = out.find('m', offset);
// if (end_pos == string::npos)
// break;
// else if (auto next_pos = out.find('\x1b', offset); not isdigit(out[end_pos - 1]) or end_pos > next_pos) {
// offset = next_pos;
// continue;
// }
// out.erase(start_pos, (end_pos - start_pos)+1);
// offset = 0;
// }
// out.shrink_to_fit();
// return out;
// }
// }
namespace Tools {
size_t wide_ulen(const string& str) {
unsigned int chars = 0;
try {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
auto w_str = conv.from_bytes((str.size() > 10000 ? str.substr(0, 10000).c_str() : str.c_str()));
for (auto c : w_str) {
chars += utf8::wcwidth(c);
}
}
catch (...) {
return ulen(str);
}
return chars;
}
size_t wide_ulen(const std::wstring& w_str) {
unsigned int chars = 0;
for (auto c : w_str) {
chars += utf8::wcwidth(c);
}
return chars;
}
string uresize(string str, const size_t len, bool wide) {
if (len < 1 or str.empty())
return "";
if (wide) {
try {
std::wstring_convert<std::codecvt_utf8<wchar_t>> conv;
auto w_str = conv.from_bytes((str.size() > 10000 ? str.substr(0, 10000).c_str() : str.c_str()));
while (wide_ulen(w_str) > len)
w_str.pop_back();
string n_str = conv.to_bytes(w_str);
return n_str;
}
catch (...) {
return uresize(str, len, false);
}
}
else {
for (size_t x = 0, i = 0; i < str.size(); i++) {
if ((static_cast<unsigned char>(str.at(i)) & 0xC0) != 0x80) x++;
if (x >= len + 1) {
str.resize(i);
break;
}
}
}
str.shrink_to_fit();
return str;
}
string luresize(string str, const size_t len, bool wide) {
if (len < 1 or str.empty())
return "";
for (size_t x = 0, last_pos = 0, i = str.size() - 1; i > 0 ; i--) {
if (wide and static_cast<unsigned char>(str.at(i)) > 0xef) {
x += 2;
last_pos = max((size_t)0, i - 1);
}
else if ((static_cast<unsigned char>(str.at(i)) & 0xC0) != 0x80) {
x++;
last_pos = i;
}
if (x >= len) {
str = str.substr(last_pos);
str.shrink_to_fit();
break;
}
}
return str;
}
string s_replace(const string& str, const string& from, const string& to) {
string out = str;
for (size_t start_pos = out.find(from); start_pos != std::string::npos; start_pos = out.find(from)) {
out.replace(start_pos, from.length(), to);
}
return out;
}
string ltrim(const string& str, const string& t_str) {
std::string_view str_v{str};
while (str_v.starts_with(t_str))
str_v.remove_prefix(t_str.size());
return string{str_v};
}
string rtrim(const string& str, const string& t_str) {
std::string_view str_v{str};
while (str_v.ends_with(t_str))
str_v.remove_suffix(t_str.size());
return string{str_v};
}
auto ssplit(const string& str, const char& delim) -> vector<string> {
vector<string> out;
for (const auto& s : str | rng::views::split(delim)
| rng::views::transform([](auto &&rng) {
return std::string_view(&*rng.begin(), rng::distance(rng));
})) {
if (not s.empty()) out.emplace_back(s);
}
return out;
}
string ljust(string str, const size_t x, bool utf, bool wide, bool limit) {
if (utf) {
if (limit and ulen(str, wide) > x)
return uresize(str, x, wide);
return str + string(max((int)(x - ulen(str)), 0), ' ');
}
else {
if (limit and str.size() > x) {
str.resize(x);
return str;
}
return str + string(max((int)(x - str.size()), 0), ' ');
}
}
string rjust(string str, const size_t x, bool utf, bool wide, bool limit) {
if (utf) {
if (limit and ulen(str, wide) > x)
return uresize(str, x, wide);
return string(max((int)(x - ulen(str)), 0), ' ') + str;
}
else {
if (limit and str.size() > x) {
str.resize(x);
return str;
};
return string(max((int)(x - str.size()), 0), ' ') + str;
}
}
string cjust(string str, const size_t x, bool utf, bool wide, bool limit) {
if (utf) {
if (limit and ulen(str, wide) > x)
return uresize(str, x, wide);
return string(max((int)ceil((double)(x - ulen(str)) / 2), 0), ' ') + str + string(max((int)floor((double)(x - ulen(str)) / 2), 0), ' ');
}
else {
if (limit and str.size() > x) {
str.resize(x);
return str;
}
return string(max((int)ceil((double)(x - str.size()) / 2), 0), ' ') + str + string(max((int)floor((double)(x - str.size()) / 2), 0), ' ');
}
}
string trans(const string& str) {
std::string_view oldstr{str};
string newstr;
newstr.reserve(str.size());
for (size_t pos; (pos = oldstr.find(' ')) != string::npos;) {
newstr.append(oldstr.substr(0, pos));
size_t x = 0;
while (pos + x < oldstr.size() and oldstr.at(pos + x) == ' ') x++;
newstr.append(Mv::r(x));
oldstr.remove_prefix(pos + x);
}
return (newstr.empty()) ? str : newstr + string{oldstr};
}
string sec_to_dhms(size_t seconds, bool no_days, bool no_seconds) {
size_t days = seconds / 86400; seconds %= 86400;
size_t hours = seconds / 3600; seconds %= 3600;
size_t minutes = seconds / 60; seconds %= 60;
string out = (not no_days and days > 0 ? to_string(days) + "d " : "")
+ (hours < 10 ? "0" : "") + to_string(hours) + ':'
+ (minutes < 10 ? "0" : "") + to_string(minutes)
+ (not no_seconds ? ":" + string(std::cmp_less(seconds, 10) ? "0" : "") + to_string(seconds) : "");
return out;
}
string floating_humanizer(uint64_t value, bool shorten, size_t start, bool bit, bool per_second) {
string out;
const size_t mult = (bit) ? 8 : 1;
bool mega = Config::getB("base_10_sizes");
// taking advantage of type deduction for array creation (since C++17)
// combined with string literals (operator""s)
static const array mebiUnits_bit {
"bit"s, "Kib"s, "Mib"s,
"Gib"s, "Tib"s, "Pib"s,
"Eib"s, "Zib"s, "Yib"s,
"Bib"s, "GEb"s
};
static const array mebiUnits_byte {
"Byte"s, "KiB"s, "MiB"s,
"GiB"s, "TiB"s, "PiB"s,
"EiB"s, "ZiB"s, "YiB"s,
"BiB"s, "GEB"s
};
static const array megaUnits_bit {
"bit"s, "Kb"s, "Mb"s,
"Gb"s, "Tb"s, "Pb"s,
"Eb"s, "Zb"s, "Yb"s,
"Bb"s, "Gb"s
};
static const array megaUnits_byte {
"Byte"s, "KB"s, "MB"s,
"GB"s, "TB"s, "PB"s,
"EB"s, "ZB"s, "YB"s,
"BB"s, "GB"s
};
const auto& units = (bit) ? ( mega ? megaUnits_bit : mebiUnits_bit) : ( mega ? megaUnits_byte : mebiUnits_byte);
value *= 100 * mult;
if (mega) {
while (value >= 100000) {
value /= 1000;
if (value < 100) {
out = to_string(value);
break;
}
start++;
}
}
else {
while (value >= 102400) {
value >>= 10;
if (value < 100) {
out = to_string(value);
break;
}
start++;
}
}
if (out.empty()) {
out = to_string(value);
if (not mega and out.size() == 4 and start > 0) {
out.pop_back();
out.insert(2, ".");
}
else if (out.size() == 3 and start > 0) {
out.insert(1, ".");
}
else if (out.size() >= 2) {
out.resize(out.size() - 2);
}
}
if (shorten) {
auto f_pos = out.find('.');
if (f_pos == 1 and out.size() > 3) {
out = to_string(round(stod(out) * 10) / 10).substr(0,3);
}
else if (f_pos != string::npos) {
out = to_string((int)round(stod(out)));
}
if (out.size() > 3) {
out = to_string((int)(out[0] - '0')) + ".0";
start++;
}
out.push_back(units[start][0]);
}
else out += " " + units[start];
if (per_second) out += (bit) ? "ps" : "/s";
return out;
}
std::string operator*(const string& str, int64_t n) {
if (n < 1 or str.empty()) {
return "";
}
else if (n == 1) {
return str;
}
string new_str;
new_str.reserve(str.size() * n);
for (; n > 0; n--)
new_str.append(str);
return new_str;
}
string strf_time(const string& strf) {
auto in_time_t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
std::tm bt {};
std::stringstream ss;
ss << std::put_time(localtime_r(&in_time_t, &bt), strf.c_str());
return ss.str();
}
void atomic_wait(const atomic<bool>& atom, bool old) noexcept {
while (atom.load(std::memory_order_relaxed) == old ) busy_wait();
}
void atomic_wait_for(const atomic<bool>& atom, bool old, const uint64_t wait_ms) noexcept {
const uint64_t start_time = time_ms();
while (atom.load(std::memory_order_relaxed) == old and (time_ms() - start_time < wait_ms)) sleep_ms(1);
}
atomic_lock::atomic_lock(atomic<bool>& atom, bool wait) : atom(atom) {
if (wait) while (not this->atom.compare_exchange_strong(this->not_true, true));
else this->atom.store(true);
}
atomic_lock::~atomic_lock() {
this->atom.store(false);
}
string readfile(const std::filesystem::path& path, const string& fallback) {
if (not fs::exists(path)) return fallback;
string out;
try {
std::ifstream file(path);
for (string readstr; getline(file, readstr); out += readstr);
}
catch (const std::exception& e) {
Logger::error("readfile() : Exception when reading " + string{path} + " : " + e.what());
return fallback;
}
return (out.empty() ? fallback : out);
}
auto celsius_to(const long long& celsius, const string& scale) -> tuple<long long, string> {
if (scale == "celsius")
return {celsius, "°C"};
else if (scale == "fahrenheit")
return {(long long)round((double)celsius * 1.8 + 32), "°F"};
else if (scale == "kelvin")
return {(long long)round((double)celsius + 273.15), "K "};
else if (scale == "rankine")
return {(long long)round((double)celsius * 1.8 + 491.67), "°R"};
return {0, ""};
}
string hostname() {
char host[HOST_NAME_MAX];
gethostname(host, HOST_NAME_MAX);
return string{host};
}
string username() {
auto user = getenv("LOGNAME");
if (user == nullptr or strlen(user) == 0) user = getenv("USER");
return (user != nullptr ? user : "");
}
DebugTimer::DebugTimer(const string name, bool start, bool delayed_report) : name(name), delayed_report(delayed_report) {
if (start)
this->start();
}
DebugTimer::~DebugTimer() {
if (running)
this->stop(true);
this->force_report();
}
void DebugTimer::start() {
if (running) return;
running = true;
start_time = time_micros();
}
void DebugTimer::stop(bool report) {
if (not running) return;
running = false;
elapsed_time = time_micros() - start_time;
if (report) this->report();
}
void DebugTimer::reset(bool restart) {
running = false;
start_time = 0;
elapsed_time = 0;
if (restart) this->start();
}
void DebugTimer::stop_rename_reset(const string &new_name, bool report, bool restart) {
this->stop(report);
name = new_name;
this->reset(restart);
}
void DebugTimer::report() {
string report_line;
if (start_time == 0 and elapsed_time == 0)
report_line = fmt::format("DebugTimer::report() warning -> Timer [{}] has not been started!", name);
else if (running)
report_line = fmt::format(custom_locale, "Timer [{}] (running) currently at {:L} μs", name, time_micros() - start_time);
else
report_line = fmt::format(custom_locale, "Timer [{}] took {:L} μs", name, elapsed_time);
if (delayed_report)
report_buffer.emplace_back(report_line);
else
Logger::log_write(log_level, report_line);
}
void DebugTimer::force_report() {
if (report_buffer.empty()) return;
for (const auto& line : report_buffer)
Logger::log_write(log_level, line);
report_buffer.clear();
}
uint64_t DebugTimer::elapsed() {
if (running)
return time_micros() - start_time;
return elapsed_time;
}
bool DebugTimer::is_running() {
return running;
}
}
namespace Logger {
using namespace Tools;
std::atomic<bool> busy (false);
bool first = true;
const string tdf = "%Y/%m/%d (%T) | ";
size_t loglevel;
fs::path logfile;
//* Wrapper for lowering priviliges if using SUID bit and currently isn't using real userid
class lose_priv {
int status = -1;
public:
lose_priv() {
if (geteuid() != Global::real_uid) {
this->status = seteuid(Global::real_uid);
}
}
~lose_priv() {
if (status == 0) {
status = seteuid(Global::set_uid);
}
}
};
void set(const string& level) {
loglevel = v_index(log_levels, level);
}
void log_write(const Level level, const string& msg) {
if (loglevel < level or logfile.empty()) return;
atomic_lock lck(busy, true);
lose_priv neutered{};
std::error_code ec;
try {
if (fs::exists(logfile) and fs::file_size(logfile, ec) > 1024 << 10 and not ec) {
auto old_log = logfile;
old_log += ".1";
if (fs::exists(old_log))
fs::remove(old_log, ec);
if (not ec)
fs::rename(logfile, old_log, ec);
}
if (not ec) {
std::ofstream lwrite(logfile, std::ios::app);
if (first) {
first = false;
lwrite << "\n" << strf_time(tdf) << "===> btop++ v." << Global::Version << "\n";
}
lwrite << strf_time(tdf) << log_levels.at(level) << ": " << msg << "\n";
}
else logfile.clear();
}
catch (const std::exception& e) {
logfile.clear();
throw std::runtime_error("Exception in Logger::log_write() : " + string{e.what()});
}
}
}