Skip to content

Commit

Permalink
Log: Move code to seperate files
Browse files Browse the repository at this point in the history
  • Loading branch information
imwints committed Aug 29, 2023
1 parent 31be436 commit 6c565e0
Show file tree
Hide file tree
Showing 10 changed files with 129 additions and 96 deletions.
9 changes: 5 additions & 4 deletions src/btop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,14 @@ tab-size = 4
#include <semaphore>
#endif

#include "btop_shared.hpp"
#include "btop_tools.hpp"
#include "btop_config.hpp"
#include "btop_input.hpp"
#include "btop_theme.hpp"
#include "btop_draw.hpp"
#include "btop_input.hpp"
#include "btop_log.hpp"
#include "btop_menu.hpp"
#include "btop_shared.hpp"
#include "btop_theme.hpp"
#include "btop_tools.hpp"

using std::atomic;
using std::cout;
Expand Down
1 change: 1 addition & 0 deletions src/btop_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ tab-size = 4
#include <fmt/core.h>

#include "btop_config.hpp"
#include "btop_log.hpp"
#include "btop_shared.hpp"
#include "btop_tools.hpp"

Expand Down
9 changes: 5 additions & 4 deletions src/btop_draw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,14 @@ tab-size = 4
#include <cmath>
#include <ranges>

#include "btop_draw.hpp"
#include "btop_config.hpp"
#include "btop_theme.hpp"
#include "btop_shared.hpp"
#include "btop_tools.hpp"
#include "btop_draw.hpp"
#include "btop_input.hpp"
#include "btop_log.hpp"
#include "btop_menu.hpp"
#include "btop_shared.hpp"
#include "btop_theme.hpp"
#include "btop_tools.hpp"

using std::array;
using std::clamp;
Expand Down
80 changes: 80 additions & 0 deletions src/btop_log.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// SPDX-License-Identifier: Apache-2.0

#include "btop_log.hpp"

#include <filesystem>
#include <iostream>
#include <mutex>
#include <string>

#include "btop_shared.hpp"
#include "btop_tools.hpp"

namespace fs = std::filesystem;

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 size_t 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()});
}
}
} // namespace Logger
27 changes: 27 additions & 0 deletions src/btop_log.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <filesystem>
#include <string>
#include <vector>

namespace Logger {
const std::vector<std::string> log_levels = {
"DISABLED",
"ERROR",
"WARNING",
"INFO",
"DEBUG",
};
extern std::filesystem::path logfile;

//* Set log level, valid arguments: "DISABLED", "ERROR", "WARNING", "INFO" and "DEBUG"
void set(const std::string& level);

void log_write(const size_t level, const std::string& msg);
inline void error(const std::string msg) { log_write(1, msg); }
inline void warning(const std::string msg) { log_write(2, msg); }
inline void info(const std::string msg) { log_write(3, msg); }
inline void debug(const std::string msg) { log_write(4, msg); }
} // namespace Logger
7 changes: 4 additions & 3 deletions src/btop_menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,13 @@ tab-size = 4
#include <cmath>
#include <filesystem>

#include "btop_menu.hpp"
#include "btop_tools.hpp"
#include "btop_config.hpp"
#include "btop_theme.hpp"
#include "btop_draw.hpp"
#include "btop_log.hpp"
#include "btop_menu.hpp"
#include "btop_shared.hpp"
#include "btop_theme.hpp"
#include "btop_tools.hpp"

using robin_hood::unordered_flat_map;
using std::array;
Expand Down
3 changes: 2 additions & 1 deletion src/btop_theme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ tab-size = 4
#include <fstream>
#include <unistd.h>

#include "btop_tools.hpp"
#include "btop_config.hpp"
#include "btop_log.hpp"
#include "btop_theme.hpp"
#include "btop_tools.hpp"

using std::round;
using std::stoi;
Expand Down
65 changes: 3 additions & 62 deletions src/btop_tools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,11 @@ tab-size = 4

#include "robin_hood.h"
#include "widechar_width.hpp"

#include "btop_config.hpp"
#include "btop_log.hpp"
#include "btop_shared.hpp"
#include "btop_tools.hpp"
#include "btop_config.hpp"

using std::cin;
using std::cout;
Expand Down Expand Up @@ -537,64 +539,3 @@ namespace Tools {

}

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 size_t 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()});
}
}
}
21 changes: 0 additions & 21 deletions src/btop_tools.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,24 +337,3 @@ namespace Tools {

}

//* Simple logging implementation
namespace Logger {
const vector<string> log_levels = {
"DISABLED",
"ERROR",
"WARNING",
"INFO",
"DEBUG",
};
extern std::filesystem::path logfile;

//* Set log level, valid arguments: "DISABLED", "ERROR", "WARNING", "INFO" and "DEBUG"
void set(const string& level);

void log_write(const size_t level, const string& msg);
inline void error(const string msg) { log_write(1, msg); }
inline void warning(const string msg) { log_write(2, msg); }
inline void info(const string msg) { log_write(3, msg); }
inline void debug(const string msg) { log_write(4, msg); }
}

3 changes: 2 additions & 1 deletion src/linux/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ tab-size = 4
#include <pwd.h>
#endif

#include "../btop_shared.hpp"
#include "../btop_config.hpp"
#include "../btop_log.hpp"
#include "../btop_shared.hpp"
#include "../btop_tools.hpp"

using std::clamp;
Expand Down

0 comments on commit 6c565e0

Please sign in to comment.