dropshell release 2025.0518.1355
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
Some checks failed
Dropshell Test / Build_and_Test (push) Has been cancelled
This commit is contained in:
@ -2,56 +2,63 @@
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
|
||||
namespace {
|
||||
namespace dropshell
|
||||
{
|
||||
// Mutex to synchronize output
|
||||
std::mutex output_mutex;
|
||||
|
||||
// ANSI colour codes
|
||||
constexpr const char* GREY = "\033[90m";
|
||||
constexpr const char* RESET = "\033[0m";
|
||||
constexpr const char* DEBUG_COLOUR = "\033[36m"; // Cyan
|
||||
constexpr const char* INFO_COLOUR = "\033[32m"; // Green
|
||||
constexpr const char* WARNING_COLOUR = "\033[33m"; // Yellow
|
||||
constexpr const char* ERROR_COLOUR = "\033[31m"; // Red
|
||||
constexpr const char *GREY = "\033[90m";
|
||||
constexpr const char *RESET = "\033[0m";
|
||||
constexpr const char *DEBUG_COLOUR = "\033[36m"; // Cyan
|
||||
constexpr const char *INFO_COLOUR = "\033[32m"; // Green
|
||||
constexpr const char *WARNING_COLOUR = "\033[33m"; // Yellow
|
||||
constexpr const char *ERROR_COLOUR = "\033[31m"; // Red
|
||||
|
||||
// Tag and colour for each stream
|
||||
struct StreamInfo {
|
||||
const char* tag;
|
||||
const char* colour;
|
||||
struct StreamInfo
|
||||
{
|
||||
const char *tag;
|
||||
const char *colour;
|
||||
};
|
||||
|
||||
const StreamInfo stream_infos[] = {
|
||||
{"[DBG]", DEBUG_COLOUR},
|
||||
{"[INF]", INFO_COLOUR},
|
||||
{"[WRN]", WARNING_COLOUR},
|
||||
{"[ERR]", ERROR_COLOUR}
|
||||
};
|
||||
{"[ERR]", ERROR_COLOUR}};
|
||||
|
||||
// Custom streambuf to prefix and colour each line
|
||||
class PrefixStreambuf : public std::streambuf {
|
||||
class PrefixStreambuf : public std::streambuf
|
||||
{
|
||||
public:
|
||||
PrefixStreambuf(std::ostream& dest, const char* tag, const char* colour)
|
||||
PrefixStreambuf(std::ostream &dest, const char *tag, const char *colour)
|
||||
: dest_(dest), tag_(tag), colour_(colour), at_line_start_(true) {}
|
||||
|
||||
protected:
|
||||
int overflow(int c) override {
|
||||
int overflow(int c) override
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(output_mutex);
|
||||
if (c == EOF) return !EOF;
|
||||
if (at_line_start_ && c != '\n') {
|
||||
if (c == EOF)
|
||||
return !EOF;
|
||||
if (at_line_start_ && c != '\n')
|
||||
{
|
||||
dest_ << GREY << tag_ << RESET << ' ' << colour_;
|
||||
at_line_start_ = false;
|
||||
}
|
||||
dest_.put(static_cast<char>(c));
|
||||
if (c == '\n') {
|
||||
if (c == '\n')
|
||||
{
|
||||
dest_ << RESET;
|
||||
at_line_start_ = true;
|
||||
}
|
||||
return c;
|
||||
}
|
||||
|
||||
private:
|
||||
std::ostream& dest_;
|
||||
const char* tag_;
|
||||
const char* colour_;
|
||||
std::ostream &dest_;
|
||||
const char *tag_;
|
||||
const char *colour_;
|
||||
bool at_line_start_;
|
||||
};
|
||||
|
||||
@ -64,15 +71,33 @@ namespace {
|
||||
std::ostream info_stream(&info_buf);
|
||||
std::ostream warning_stream(&warning_buf);
|
||||
std::ostream error_stream(&error_buf);
|
||||
}
|
||||
|
||||
std::ostream& debug = debug_stream;
|
||||
std::ostream& info = info_stream;
|
||||
std::ostream& warning = warning_stream;
|
||||
std::ostream& error = error_stream;
|
||||
std::ostream &debug = debug_stream;
|
||||
std::ostream &info = info_stream;
|
||||
std::ostream &warning = warning_stream;
|
||||
std::ostream &error = error_stream;
|
||||
|
||||
void SetColour(sColour colour, std::ostream& os) {
|
||||
switch (colour) {
|
||||
std::ostream &colourstream(sColour colour)
|
||||
{
|
||||
switch (colour)
|
||||
{
|
||||
case sColour::DEBUG:
|
||||
return debug_stream;
|
||||
case sColour::INFO:
|
||||
return info_stream;
|
||||
case sColour::WARNING:
|
||||
return warning_stream;
|
||||
case sColour::ERROR:
|
||||
return error_stream;
|
||||
default:
|
||||
return info_stream;
|
||||
}
|
||||
}
|
||||
|
||||
void SetColour(sColour colour, std::ostream &os)
|
||||
{
|
||||
switch (colour)
|
||||
{
|
||||
case sColour::RESET:
|
||||
os << RESET;
|
||||
break;
|
||||
@ -88,32 +113,17 @@ void SetColour(sColour colour, std::ostream& os) {
|
||||
case sColour::ERROR:
|
||||
os << ERROR_COLOUR;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void PrintDebug(const std::string& msg) {
|
||||
std::lock_guard<std::mutex> lock(output_mutex);
|
||||
debug << msg << '\n';
|
||||
}
|
||||
void PrintInfo(const std::string& msg) {
|
||||
std::lock_guard<std::mutex> lock(output_mutex);
|
||||
info << msg << '\n';
|
||||
}
|
||||
void PrintWarning(const std::string& msg) {
|
||||
std::lock_guard<std::mutex> lock(output_mutex);
|
||||
warning << msg << '\n';
|
||||
}
|
||||
void PrintError(const std::string& msg) {
|
||||
std::lock_guard<std::mutex> lock(output_mutex);
|
||||
error << msg << '\n';
|
||||
}
|
||||
SwitchColour::SwitchColour(sColour colour, std::ostream &os) : os_(os), colour_(colour)
|
||||
{
|
||||
SetColour(colour_, os_);
|
||||
}
|
||||
|
||||
SwitchColour::SwitchColour(sColour colour, std::ostream& os) : os_(os), colour_(colour)
|
||||
{
|
||||
SetColour(colour_, os_);
|
||||
}
|
||||
SwitchColour::~SwitchColour()
|
||||
{
|
||||
SetColour(sColour::RESET, os_);
|
||||
}
|
||||
|
||||
SwitchColour::~SwitchColour()
|
||||
{
|
||||
SetColour(sColour::RESET, os_);
|
||||
}
|
||||
} // namespace dropshell
|
Reference in New Issue
Block a user