More logging work

This commit is contained in:
2025-06-26 20:59:57 -04:00
parent 2b9d1c9549
commit 6c0ca2ab1f
3 changed files with 74 additions and 30 deletions

View File

@@ -10,7 +10,7 @@
#include <initializer_list>
namespace Mutil::Console::General {
enum class Codes : char {
enum Codes : char {
BEL = 0x07,
BS,
HT,
@@ -75,6 +75,7 @@ namespace Mutil::Console::Graphics {
SetForegroundColor = 38,
SetBackgroundColor = 48,
};
}
namespace Mutil::Console::Functions {
@@ -98,7 +99,7 @@ namespace Mutil::Console::Functions {
if (empty(args)) { return; };
std::stringstream s;
s << (char)Mutil::Console::General::Codes::ESC << '[';
s << Mutil::Console::General::Codes::ESC << '[';
for (uint8_t a : args) {
//std::cout << (uint16_t)a << std::endl;

View File

@@ -5,61 +5,83 @@
#pragma once
#include <iostream>
#include <sstream>
#include <functional>
#include <format>
namespace Mutil::Logging {
namespace Mutil::Logging {
/// Logger object for constructing loggers.
class LoggerObj {
class LoggerObj : public std::ostream {
private:
/// Output stream, default std::clog.
std::ostream &os = std::clog;
/// Lambda formatting function, primarily for storing a format string in a versatile way, users allowed to supply their own.
std::function<std::string(void)> λ;
std::function<std::string()> λPrefix;
std::function<std::string(const std::string_view)> λMessage;
std::function<std::string()> λSuffix;
public:
/// Default constructor, just simple messages to be written to console
LoggerObj() {};
/// Construct with specified output stream
LoggerObj(std::ostream &os);
/// Construct with a custom, formatted prefix applied to every message, such as "* Mutil Logging >> message".
template<typename... Args>
LoggerObj(const std::format_string<Args...> fmt, Args&&... args) {
λ = [fmt, args...](){
return std::vformat(fmt.get(), std::make_format_args(args...));
};
}
/// Construct with a custom lambda formatting function
LoggerObj(std::function<std::string(void)> λ) : λ(λ) {};
/// Construct with a specified output stream and custom formatting function
LoggerObj(std::ostream &os, std::function<std::string(void)> λ) : os(os), λ(λ) {};
public:
/// If provided only runs formatting function, formatting function may retrieve data for its own messages.
void operator()() {
λ && os << λ();
}
/// Full construction
LoggerObj(std::ostream &os,
std::function<std::string()> λprefix,
std::function<std::string(const std::string_view)> λmessage,
std::function<std::string()> λsuffix
) : os(os), λPrefix(λprefix), λMessage(λmessage), λSuffix(λsuffix) {};
public:
/// Send a simple log message
void operator()(const std::string_view message) {
(λ && os << λ() << message << '\n') ||
os << message << '\n';
//std::ostringstream oss;
λPrefix && os << λPrefix();
(λMessage && os << λMessage(message)) || os << message;
λSuffix && os << λSuffix();
//os << oss.str() << std::endl;
os << std::endl;
}
/// Send a formatted/complex log message
template<typename... Args>
void operator()(const std::format_string<Args...> fmt, Args&&... args) {
(λ && os << λ() << std::vformat(fmt.get(), std::make_format_args(args...)) << '\n') ||
os << std::vformat(fmt.get(), std::make_format_args(args...)) << '\n';
//std::ostringstream oss;
std::string m = std::vformat(fmt.get(), std::make_format_args(args...));
λPrefix && os << λPrefix();
(λMessage && os << λMessage(m)) || os << m;
λSuffix && os << λSuffix();
//os << oss.str() << std::endl;
os << std::endl;
};
/// Send log messages using logger as a stream
template<typename In>
std::ostream& operator<<(In message) {
(λ && os << λ() << message) ||
os << message;
return os;
LoggerObj& operator<<(In message) {
//std::ostringstream oss;
//oss << message;
//operator()(oss.str());
//(λ && os << λ() << message) ||
//os << message;
//return os;
λPrefix && os << λPrefix();
(λMessage && os << λMessage(message)) || os << message;
λSuffix && os << λSuffix();
os << std::endl;
return *this;
};
};
/*
class Info : public LoggerObj {
public:
Info() : LoggerObj("[I] | ") {};
@@ -84,5 +106,6 @@ namespace Mutil::Logging {
public:
Debug() : LoggerObj("[D] | ") {};
};
*/
}

View File

@@ -5,6 +5,7 @@
int main() {
Mutil::Logging::LoggerObj lo;
lo("Hello world!!!!");
/*
Mutil::Logging::Info I;
Mutil::Logging::Error E;
Mutil::Logging::Verbose V;
@@ -13,6 +14,7 @@ int main() {
E("Something...");
V("Something...");
D("Something...");
*/
//std::cout << (char)0x1b << (char)0x5b << "33m" << "BULLOCKS" << '\n';
Mutil::Console::Functions::SelectGraphicsRendition({
Mutil::Console::Graphics::TextStyle::Underline,
@@ -35,9 +37,27 @@ int main() {
Mutil::Console::Graphics::ColorFormat::ColorRGB, 252, 186, 3,
});
return "--> ";
});
}, [] (const std::string_view m) {
Mutil::Console::Functions::SelectGraphicsRendition({
//Mutil::Console::Graphics::TextStyle::Reset,
Mutil::Console::Graphics::TextStyle::Underline,
Mutil::Console::Graphics::TextStyle::Bold,
Mutil::Console::Graphics::Color16::Blue,
});
return std::format("<{}>", m);
}, [] {
Mutil::Console::Functions::SelectGraphicsRendition({
Mutil::Console::Graphics::TextStyle::Reset,
});
return "==";
}
);
l("Hello world");
//l << "Something...." << "ass" << " " << "pussy";
l << "DSDSAD" << "dddddd" << "DDSDDAWSH";
l << "ddddd";
//Desktop::Browser::OpenURL("https://git.redacted.cc/maxine/mutil");
//Mutil::Console::Graphics::SGARG a = Mutil::Console::Graphics::TextStyle::Underline
}