Added OnLogEvent

This commit is contained in:
2024-08-13 14:25:54 -04:00
parent 168af78441
commit c735e4baa1
3 changed files with 52 additions and 25 deletions

View File

@@ -3,6 +3,8 @@
#include <mcolor.h>
#include <source_location>
#include <map>
#include <jlog/token.hpp>
#include <Event.h>
namespace jlog
{
@@ -12,6 +14,8 @@ namespace jlog
class Logger {
public:
explicit Logger(const std::string& context, const AnsiColor& color = AnsiColor::FG_DEFAULT);
public:
Event<std::vector<token>> OnLogEvent;
public:
void operator () (const std::string& message, const std::source_location& location = std::source_location::current());
@@ -27,8 +31,8 @@ namespace jlog
void LogFile(const std::string& f);
std::string LogFile();
// no cc no bullshit
void ColorCode(AnsiColor cc = AnsiColor::FG_DEFAULT);
AnsiColor ColorCode();
void SetColorCode(AnsiColor cc = AnsiColor::FG_DEFAULT);
AnsiColor GetColorCode();
public:
std::string Context();
void SetTraceback(bool enabled);

View File

@@ -5,44 +5,65 @@
// Contributors: william@redacted.cc maxi@redacted.cc
// This work is dedicated to the public domain.
#include <jlog/jlog.hpp>
#include <jlog/io.hpp>
#include <mcolor.h>
int main()
{
using namespace mcolor;
jlog::info("This dick");
jlog::debug("This dick");
jlog::verbose("This dick");
jlog::warning("This dick");
jlog::error("This dick");
jlog::fatal("This dick");
jlog::info("This dick 1");
jlog::debug("This dick 1");
jlog::verbose("This dick 1");
jlog::warning("This dick 1");
jlog::error("This dick 1");
jlog::fatal("This dick 1");
jlog::SetTracebackOnGlobalLoggers(true);
jlog::SetTracebackOnGlobalLoggers(false);
jlog::info.SetTraceback(true);
jlog::debug.SetTraceback(true);
jlog::verbose.SetTraceback(true);
jlog::info("This dick");
jlog::debug("This dick");
jlog::verbose("This dick");
jlog::warning("This dick");
jlog::error("This dick");
jlog::fatal("This dick");
jlog::info("This dick 2");
jlog::debug("This dick 2");
jlog::verbose("This dick 2");
jlog::warning("This dick 2");
jlog::error("This dick 2");
jlog::fatal("This dick 2");
jlog::info.Disable();
jlog::Logger cock{"COCK"};
//jlog::Logger info{"I"};
jlog::Logger error{"E"};
error.ColorCode(AnsiColor::FG_BRIGHT_RED);
jlog::info("This dick 3");
jlog::debug("This dick 3");
jlog::verbose("This dick 3");
jlog::warning("This dick 3");
jlog::error("This dick 3");
jlog::fatal("This dick 3");
jlog::info.Enable();
cock.Log("dick ass");
//cock.LogTrace("I" "dick ass");
error.Log("dick ass");
jlog::SetTracebackOnGlobalLoggers(false);
jlog::info("This dick 4");
jlog::info.OnLogEvent += [](std::vector<jlog::token> t) {
jlog::token dbg = {.colorCode = jlog::debug.GetColorCode(), .content = "This message is only seen when the event is called."};
jlog::log_to_console(jlog::toks2consoleMsg(std::vector<jlog::token>{dbg}));
};
jlog::info("This dick 4");
jlog::info.OnLogEvent += [](std::vector<jlog::token> t) {
jlog::token dbg = {.colorCode = jlog::debug.GetColorCode(), .content = "This is the 2nd event message"};
jlog::log_to_console(jlog::toks2consoleMsg(std::vector<jlog::token>{dbg}));
};
jlog::info("This dick 4");
return 0;
}

View File

@@ -22,6 +22,8 @@ namespace jlog
else
fmt = log_format(this->context, message, this->colorcode);
OnLogEvent(fmt);
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
}
@@ -36,8 +38,8 @@ namespace jlog
}
std::string Logger::LogFile() { return this->logfile; }
void Logger::ColorCode(AnsiColor cc) { this->colorcode = cc; }
AnsiColor Logger::ColorCode() { return this->colorcode; }
void Logger::SetColorCode(AnsiColor cc) { this->colorcode = cc; }
AnsiColor Logger::GetColorCode() { return this->colorcode; }
std::string Logger::Context() { return this->context; }
@@ -51,5 +53,5 @@ namespace jlog
void Logger::Enable() { SetEnabled(true);}
void Logger::Disable() { SetEnabled(true); }
void Logger::Disable() { SetEnabled(false); }
}