LogEntry class, prettied up code and log output more, OnLogEvent now takes a LogEntry instead of a vector<token>

This commit is contained in:
2024-08-14 15:12:32 -04:00
parent 7aa30a4966
commit c65cc4f965
3 changed files with 16 additions and 59 deletions

View File

@@ -16,14 +16,6 @@ namespace jlog
class LogEntry {
public:
//LogEntry(Color4 context_color, const std::string &context, const std::string &message, const std::source_location &trace, const jlog::Timestamp &timestamp) {
//LogEntry()
// context_color = Colors::White;
// context = "FUCK";
// message = "YOU";
// trace = std::source_location::current();
// timestamp = Timestamp();
//};
LogEntry(Color4 cc, const std::string c, const std::string msg, const std::source_location t, const Timestamp ts);
public:
Color4 ContextColor() { return context_color; }
@@ -31,7 +23,7 @@ namespace jlog
std::string Message() { return message; }
std::source_location Trace() { return trace; }
jlog::Timestamp GetTimeStamp() { return timestamp; };
std::vector<token> Tokenize();
virtual std::vector<token> Tokenize();
public:
bool IncludeTrace;
bool IncludeTimestamp;
@@ -43,19 +35,14 @@ namespace jlog
Timestamp timestamp;
};
///
class Logger {
public:
explicit Logger(const std::string& context, const Color4& color = Colors::LightGray);
public:
Event<std::vector<jlog::token>> OnLogEvent;
Event<LogEntry> OnLogEvent;
public:
void operator () (const std::string& message, const std::source_location& location = std::source_location::current());
virtual void Log(const std::string &message, const std::source_location& location = std::source_location::current());
//virtual void LogTrace(const std::vector<token> tokens);
//virtual void LogTrace(const std::string& context, const std::string &message);
//virtual void LogTrace(const std::string &message, const std::source_location& location = std::source_location::current());
public:
void SetEnabled(bool state);
void Enable();
@@ -64,20 +51,19 @@ namespace jlog
void LogFile(const std::string& f);
std::string LogFile();
// no cc no bullshit
//void SetColorCode(AnsiColor cc = AnsiColor::FG_DEFAULT);
//AnsiColor GetColorCode();
void SetColorCode(Color4 cc = Colors::LightGray);
Color4 GetColorCode();
public:
std::string Context();
void SetTraceback(bool b);
void IncludeTimestamp(bool b);
/**/
protected:
bool enabled = true;
std::string logfile = "latest.log";
//AnsiColor colorcode = AnsiColor::FG_DEFAULT;
Color4 colorcode = Colors::LightGray;
std::string context;
bool trace = true;
bool timestamp = true;
};
}

View File

@@ -61,7 +61,7 @@ int main()
// We can also add event hooks to a logger.
demo(std::format("{}\n>\t{}\n>\t\t{}\n>\n>\t\t{}\n>\t{}",
"We can also add event hooks to a logger.",
"demo.OnLogEvent += [](std::vector<jlog::token> t) {",
"demo.OnLogEvent += [](jlog::LogEntry le) {",
"jlog::token dbg = {.colorCode = jlog::debug.GetColorCode(), .content = \"This message is only seen when the event hook is added.\"};",
"jlog::log_to_console(jlog::toks2consoleMsg(std::vector<jlog::token>{dbg}));",
"}"
@@ -71,7 +71,7 @@ int main()
demo("Before event hook");
// Create and add event hook.
demo.OnLogEvent += [](std::vector<jlog::token> t) {
demo.OnLogEvent += [](jlog::LogEntry le) {
jlog::token dbg = {.colorCode = jlog::debug.GetColorCode(), .content = "This message is only seen when the event hook is added."};
//jlog::log_to_console(jlog::toks2consoleMsg(std::vector<jlog::token>{dbg}));

View File

@@ -6,20 +6,6 @@
namespace jlog
{
//LogEntry::LogEntry(const std::string& message, const std::source_location& trace, const Timestamp& timestamp);
//}
/*
LogEntry::LogEntry(Color4 context_color, const std::string &context, const std::string &message, const std::source_location &trace, const jlog::Timestamp &timestamp) {
this->context_color = context_color;
this->context = context;
this->message = message;
this->trace = trace;
this->timestamp = timestamp;
};
*/
LogEntry::LogEntry(Color4 cc, const std::string c, const std::string msg, const std::source_location t, const Timestamp ts) {
this->context_color = cc;
this->context = c;
@@ -32,7 +18,7 @@ namespace jlog
std::vector<token> tokens;
if (IncludeTimestamp)
tokens.push_back({.content = std::format(
tokens.push_back({.colorCode = Colors::Gray, .content = std::format(
"{}-{}-{} {}:{}:{}.{}",
timestamp.Year(),
timestamp.Month(),
@@ -44,13 +30,15 @@ namespace jlog
tokens.push_back({.colorCode = context_color, .content = context});
if (IncludeTrace)
tokens.push_back({.content = std::format("{}:{}", trace.function_name(), trace.file_name(), trace.line())});
if (IncludeTrace) {
tokens.push_back({.colorCode = Colors::Gray, .content = trace.function_name()});
tokens.push_back({.colorCode = Colors::Gray, .content = std::format("{}:{}",trace.file_name(), trace.line())});
}
tokens.push_back({.content = message, .delimiter = ""});
tokens.push_back({.colorCode = Colors::Gray, .content = ">>", .delimiter = ""});
tokens.push_back({.colorCode = Colors::Gray, .content = message, .delimiter = ""});
//tokens.push_back(contextToken);
//tokens.push_back(messageToken);
return tokens;
}
@@ -65,27 +53,12 @@ namespace jlog
LogEntry logentry = {colorcode, context, message, location, Timestamp()};
logentry.IncludeTrace = this->trace;
logentry.IncludeTimestamp = true;
logentry.IncludeTimestamp = this->timestamp;
//std::vector<jlog::token> fmt;
OnLogEvent(logentry);
/*
if (trace)
fmt = log_format(this->context, message, location.function_name(), location.file_name(), location.line(),this->colorcode);
else
fmt = log_format(this->context, message, this->colorcode);
*/
//OnLogEvent(fmt);
OnLogEvent(logentry.Tokenize());
//jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
//jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
//jlog::log_to_console(jlog::TokensToString(fmt) + '\n');
//jlog::log_to_file(logfile, jlog::TokensToString<WithoutColor>(fmt) + '\n');
LogToConsole(logentry.Tokenize());
LogToConsole("\n");
//LogToConsole(std::vector<token>{jlog::endl});
LogToFile(logfile, logentry.Tokenize());
LogToFile(logfile, "\n");
}
@@ -100,8 +73,6 @@ namespace jlog
}
std::string Logger::LogFile() { return this->logfile; }
//void Logger::SetColorCode(AnsiColor cc) { this->colorcode = cc; }
//AnsiColor Logger::GetColorCode() { return this->colorcode; }
void Logger::SetColorCode(Color4 cc) { this->colorcode = cc; }
Color4 Logger::GetColorCode() { return this->colorcode; }