mirror of
https://github.com/aristocratos/btop.git
synced 2024-06-02 02:24:54 +12:00
Move logging code to seperate files
Avoids unnecessary recompilation
This commit is contained in:
parent
ebc46ca12c
commit
f09c034339
|
@ -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;
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -23,13 +23,14 @@ tab-size = 4
|
|||
#include <stdexcept>
|
||||
#include <string>
|
||||
|
||||
#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;
|
||||
|
|
80
src/btop_log.cpp
Normal file
80
src/btop_log.cpp
Normal file
|
@ -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
src/btop_log.hpp
Normal file
27
src/btop_log.hpp
Normal file
|
@ -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
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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;
|
||||
|
@ -596,13 +598,13 @@ namespace Tools {
|
|||
if (delayed_report)
|
||||
report_buffer.emplace_back(report_line);
|
||||
else
|
||||
Logger::log_write(log_level, report_line);
|
||||
Logger::debug(report_line);
|
||||
}
|
||||
|
||||
void DebugTimer::force_report() {
|
||||
if (report_buffer.empty()) return;
|
||||
for (const auto& line : report_buffer)
|
||||
Logger::log_write(log_level, line);
|
||||
Logger::debug(line);
|
||||
report_buffer.clear();
|
||||
}
|
||||
|
||||
|
@ -617,64 +619,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 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()});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -340,38 +340,7 @@ namespace Tools {
|
|||
|
||||
//* Convert a celsius value to celsius, fahrenheit, kelvin or rankin and return tuple with new value and unit.
|
||||
auto celsius_to(const long long& celsius, const string& scale) -> tuple<long long, string>;
|
||||
}
|
||||
|
||||
//* Simple logging implementation
|
||||
namespace Logger {
|
||||
const vector<string> log_levels = {
|
||||
"DISABLED",
|
||||
"ERROR",
|
||||
"WARNING",
|
||||
"INFO",
|
||||
"DEBUG",
|
||||
};
|
||||
extern std::filesystem::path logfile;
|
||||
|
||||
enum Level : size_t {
|
||||
DISABLED = 0,
|
||||
ERROR = 1,
|
||||
WARNING = 2,
|
||||
INFO = 3,
|
||||
DEBUG = 4,
|
||||
};
|
||||
|
||||
//* Set log level, valid arguments: "DISABLED", "ERROR", "WARNING", "INFO" and "DEBUG"
|
||||
void set(const string& level);
|
||||
|
||||
void log_write(const Level level, const string& msg);
|
||||
inline void error(const string msg) { log_write(ERROR, msg); }
|
||||
inline void warning(const string msg) { log_write(WARNING, msg); }
|
||||
inline void info(const string msg) { log_write(INFO, msg); }
|
||||
inline void debug(const string msg) { log_write(DEBUG, msg); }
|
||||
}
|
||||
|
||||
namespace Tools {
|
||||
//* Creates a named timer that is started on construct (by default) and reports elapsed time in microseconds to Logger::debug() on destruct if running
|
||||
//* Unless delayed_report is set to false, all reporting is buffered and delayed until DebugTimer is destructed or .force_report() is called
|
||||
//* Usage example: Tools::DebugTimer timer(name:"myTimer", [start:true], [delayed_report:true]) // Create timer and start
|
||||
|
@ -386,7 +355,6 @@ namespace Tools {
|
|||
public:
|
||||
string name{};
|
||||
bool delayed_report{};
|
||||
Logger::Level log_level = Logger::DEBUG;
|
||||
DebugTimer() = default;
|
||||
DebugTimer(const string name, bool start = true, bool delayed_report = true);
|
||||
~DebugTimer();
|
||||
|
@ -404,5 +372,3 @@ namespace Tools {
|
|||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -60,6 +60,7 @@ tab-size = 4
|
|||
#include <memory>
|
||||
|
||||
#include "../btop_config.hpp"
|
||||
#include "../btop_log.hpp"
|
||||
#include "../btop_shared.hpp"
|
||||
#include "../btop_tools.hpp"
|
||||
|
||||
|
|
|
@ -40,8 +40,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;
|
||||
|
|
|
@ -53,6 +53,7 @@ tab-size = 4
|
|||
#include <string>
|
||||
|
||||
#include "../btop_config.hpp"
|
||||
#include "../btop_log.hpp"
|
||||
#include "../btop_shared.hpp"
|
||||
#include "../btop_tools.hpp"
|
||||
|
||||
|
|
Loading…
Reference in a new issue