mirror of
https://github.com/aristocratos/btop.git
synced 2024-06-26 18:10:32 +12:00
Improve code in Logger namespace
Using std::call_once instead of boolean Define some log file specific variables only if syslog or journald are not used Rename the privilege RAII wrapper and delete all operators and constructors
This commit is contained in:
parent
c60bd928d4
commit
b31fce884f
|
@ -847,7 +847,9 @@ int main(int argc, char **argv) {
|
|||
}
|
||||
else {
|
||||
Config::conf_file = Config::conf_dir / "btop.conf";
|
||||
#if !(defined(HAVE_JOURNALD) || defined(HAVE_SYSLOG))
|
||||
Logger::logfile = Config::conf_dir / "btop.log";
|
||||
#endif
|
||||
Theme::user_theme_dir = Config::conf_dir / "themes";
|
||||
if (not fs::exists(Theme::user_theme_dir) and not fs::create_directory(Theme::user_theme_dir, ec)) Theme::user_theme_dir.clear();
|
||||
}
|
||||
|
|
|
@ -21,35 +21,42 @@
|
|||
#include "btop_shared.hpp"
|
||||
#include "btop_tools.hpp"
|
||||
|
||||
namespace fs = std::filesystem;
|
||||
|
||||
namespace Logger {
|
||||
using namespace Tools;
|
||||
std::mutex log_mtx{};
|
||||
bool first = true;
|
||||
|
||||
size_t loglevel;
|
||||
fs::path logfile;
|
||||
constexpr const std::string_view tdf = "%Y/%m/%d (%T) | ";
|
||||
std::mutex log_mtx{};
|
||||
|
||||
//* Wrapper for lowering priviliges if using SUID bit and currently isn't using real userid
|
||||
class lose_priv {
|
||||
#if !(defined(HAVE_JOURNALD) || defined(HAVE_SYSLOG))
|
||||
namespace fs = std::filesystem;
|
||||
fs::path logfile;
|
||||
std::once_flag log_file_header{};
|
||||
constexpr const std::string_view tdf = "%Y/%m/%d (%T) | ";
|
||||
#endif
|
||||
|
||||
// Wrapper for lowering privileges if using SUID bit and currently isn't using real userid
|
||||
class DropPrivileges {
|
||||
int status = -1;
|
||||
|
||||
public:
|
||||
lose_priv() {
|
||||
DropPrivileges() noexcept {
|
||||
if (geteuid() != Global::real_uid) {
|
||||
this->status = seteuid(Global::real_uid);
|
||||
}
|
||||
}
|
||||
~lose_priv() {
|
||||
~DropPrivileges() noexcept {
|
||||
if (status == 0) {
|
||||
status = seteuid(Global::set_uid);
|
||||
}
|
||||
}
|
||||
|
||||
DropPrivileges(DropPrivileges&) = delete;
|
||||
DropPrivileges& operator=(DropPrivileges&) = delete;
|
||||
DropPrivileges(DropPrivileges&&) = delete;
|
||||
DropPrivileges&& operator=(DropPrivileges&&) = delete;
|
||||
};
|
||||
|
||||
void set(const string& level) { loglevel = v_index(log_levels, level); }
|
||||
void set(const string& level) noexcept { loglevel = v_index(log_levels, level); }
|
||||
|
||||
void log(const size_t level, const std::string_view msg, [[maybe_unused]] const std::source_location location) {
|
||||
if (loglevel < level) {
|
||||
|
@ -57,7 +64,7 @@ namespace Logger {
|
|||
}
|
||||
|
||||
std::lock_guard lock{log_mtx};
|
||||
lose_priv neutered{};
|
||||
DropPrivileges neutered{};
|
||||
|
||||
#if defined(HAVE_JOURNALD) || defined(HAVE_SYSLOG)
|
||||
int status = LOG_DEBUG;
|
||||
|
@ -101,10 +108,9 @@ namespace Logger {
|
|||
}
|
||||
if (not ec) {
|
||||
std::ofstream lwrite(logfile, std::ios::app);
|
||||
if (first) {
|
||||
first = false;
|
||||
std::call_once(log_file_header, [&lwrite] {
|
||||
fmt::print(lwrite, "\n{}===> btop++ v.{}\n", strf_time(tdf), Global::Version);
|
||||
}
|
||||
});
|
||||
fmt::print(lwrite, "{}{}: {}\n", strf_time(tdf), log_levels.at(level), msg);
|
||||
}
|
||||
else {
|
||||
|
|
|
@ -21,7 +21,7 @@ namespace Logger {
|
|||
extern std::filesystem::path logfile;
|
||||
|
||||
//* Set log level, valid arguments: "DISABLED", "ERROR", "WARNING", "INFO" and "DEBUG"
|
||||
void set(const std::string& level);
|
||||
void set(const std::string& level) noexcept;
|
||||
|
||||
void log(const size_t level, const std::string_view msg, const std::source_location location);
|
||||
|
||||
|
|
Loading…
Reference in a new issue