Skip to content

Commit

Permalink
Improve code in Logger namespace
Browse files Browse the repository at this point in the history
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
  • Loading branch information
imwints committed Sep 19, 2023
1 parent 3640f01 commit d2f7262
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 14 deletions.
2 changes: 2 additions & 0 deletions src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -771,7 +771,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();
}
Expand Down
32 changes: 19 additions & 13 deletions src/btop_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,50 @@
#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;
std::mutex log_mtx{};

#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 priviliges if using SUID bit and currently isn't using real userid
class lose_priv {
// 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) {
return;
}

std::lock_guard lock{log_mtx};
lose_priv neutered{};
DropPrivileges neutered{};

#if defined(HAVE_JOURNALD) || defined(HAVE_SYSLOG)
int status = LOG_DEBUG;
Expand Down Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion src/btop_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit d2f7262

Please sign in to comment.