feat: Update 4 files
All checks were successful
Build-Test-Publish / build (linux/amd64) (push) Successful in 1m12s
Build-Test-Publish / build (linux/arm64) (push) Successful in 3m49s

This commit is contained in:
Your Name
2025-09-01 20:05:35 +12:00
parent aea0f0f307
commit 622ea5d83d
4 changed files with 84 additions and 7 deletions

View File

@@ -6,6 +6,16 @@ namespace dropshell
{
// Mutex to synchronize output
std::mutex output_mutex;
// Global log level
enum class LogLevel {
DEBUG = 0,
INFO = 1,
WARNING = 2,
ERROR = 3
};
static LogLevel current_log_level = LogLevel::INFO;
// ANSI colour codes
constexpr const char *GREY = "\033[90m";
@@ -29,16 +39,30 @@ namespace dropshell
{"[WRN]", WARNING_COLOUR},
{"[ERR]", ERROR_COLOUR}};
// Null streambuf that discards all output
class NullStreambuf : public std::streambuf
{
protected:
int overflow(int c) override
{
return c; // Just discard the character
}
};
// Custom streambuf to prefix and colour each line
class PrefixStreambuf : public std::streambuf
{
public:
PrefixStreambuf(std::ostream &dest, const char *tag, const char *colour)
: dest_(dest), tag_(tag), colour_(colour), at_line_start_(true) {}
PrefixStreambuf(std::ostream &dest, const char *tag, const char *colour, LogLevel level)
: dest_(dest), tag_(tag), colour_(colour), level_(level), at_line_start_(true) {}
protected:
int overflow(int c) override
{
// Check if this log level should be output
if (level_ < current_log_level)
return c; // Discard if below current log level
std::lock_guard<std::mutex> lock(output_mutex);
if (c == EOF)
return !EOF;
@@ -60,13 +84,14 @@ namespace dropshell
std::ostream &dest_;
const char *tag_;
const char *colour_;
LogLevel level_;
bool at_line_start_;
};
PrefixStreambuf debug_buf(std::clog, stream_infos[0].tag, stream_infos[0].colour);
PrefixStreambuf info_buf(std::clog, stream_infos[1].tag, stream_infos[1].colour);
PrefixStreambuf warning_buf(std::clog, stream_infos[2].tag, stream_infos[2].colour);
PrefixStreambuf error_buf(std::cerr, stream_infos[3].tag, stream_infos[3].colour);
PrefixStreambuf debug_buf(std::clog, stream_infos[0].tag, stream_infos[0].colour, LogLevel::DEBUG);
PrefixStreambuf info_buf(std::clog, stream_infos[1].tag, stream_infos[1].colour, LogLevel::INFO);
PrefixStreambuf warning_buf(std::clog, stream_infos[2].tag, stream_infos[2].colour, LogLevel::WARNING);
PrefixStreambuf error_buf(std::cerr, stream_infos[3].tag, stream_infos[3].colour, LogLevel::ERROR);
std::ostream debug_stream(&debug_buf);
std::ostream info_stream(&info_buf);
@@ -130,4 +155,18 @@ namespace dropshell
SetColour(sColour::RESET, os_);
}
void set_log_level(const std::string& level)
{
if (level == "debug")
current_log_level = LogLevel::DEBUG;
else if (level == "info")
current_log_level = LogLevel::INFO;
else if (level == "warning")
current_log_level = LogLevel::WARNING;
else if (level == "error")
current_log_level = LogLevel::ERROR;
else
current_log_level = LogLevel::INFO; // Default to info if unknown
}
} // namespace dropshell

View File

@@ -72,6 +72,9 @@ std::ostream& colourstream(sColour colour);
// Set colour for a stream
void SetColour(sColour colour, std::ostream& os = std::cerr);
// Set the global log level
void set_log_level(const std::string& level);
class SwitchColour
{
public: