Compare commits
5 Commits
nomacros
...
Prerelease
Author | SHA1 | Date | |
---|---|---|---|
c65cc4f965 | |||
7aa30a4966 | |||
b69ba9650b | |||
|
cde1aa507b | ||
|
bfb6fc0702 |
@@ -1,29 +1,48 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
#include <mcolor.h>
|
#include <mcolor.h>
|
||||||
|
#include "token.hpp"
|
||||||
|
#include "Color4.hpp"
|
||||||
#include "Colors.hpp"
|
#include "Colors.hpp"
|
||||||
#include <source_location>
|
#include <source_location>
|
||||||
#include <map>
|
#include <map>
|
||||||
#include <jlog/token.hpp>
|
#include <jlog/token.hpp>
|
||||||
#include <Event.h>
|
#include <Event.h>
|
||||||
|
#include "formatter.hpp"
|
||||||
|
|
||||||
namespace jlog
|
namespace jlog
|
||||||
{
|
{
|
||||||
using namespace mcolor;
|
using namespace mcolor;
|
||||||
|
|
||||||
///
|
class LogEntry {
|
||||||
|
public:
|
||||||
|
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; }
|
||||||
|
std::string Context() { return context; }
|
||||||
|
std::string Message() { return message; }
|
||||||
|
std::source_location Trace() { return trace; }
|
||||||
|
jlog::Timestamp GetTimeStamp() { return timestamp; };
|
||||||
|
virtual std::vector<token> Tokenize();
|
||||||
|
public:
|
||||||
|
bool IncludeTrace;
|
||||||
|
bool IncludeTimestamp;
|
||||||
|
private:
|
||||||
|
Color4 context_color;
|
||||||
|
std::string context;
|
||||||
|
std::string message;
|
||||||
|
std::source_location trace;
|
||||||
|
Timestamp timestamp;
|
||||||
|
};
|
||||||
|
|
||||||
class Logger {
|
class Logger {
|
||||||
public:
|
public:
|
||||||
explicit Logger(const std::string& context, const Color4& color = Colors::LightGray);
|
explicit Logger(const std::string& context, const Color4& color = Colors::LightGray);
|
||||||
public:
|
public:
|
||||||
Event<std::vector<token>> OnLogEvent;
|
Event<LogEntry> OnLogEvent;
|
||||||
public:
|
public:
|
||||||
void operator () (const std::string& message, const std::source_location& location = std::source_location::current());
|
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 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:
|
public:
|
||||||
void SetEnabled(bool state);
|
void SetEnabled(bool state);
|
||||||
void Enable();
|
void Enable();
|
||||||
@@ -32,20 +51,19 @@ namespace jlog
|
|||||||
void LogFile(const std::string& f);
|
void LogFile(const std::string& f);
|
||||||
std::string LogFile();
|
std::string LogFile();
|
||||||
// no cc no bullshit
|
// no cc no bullshit
|
||||||
//void SetColorCode(AnsiColor cc = AnsiColor::FG_DEFAULT);
|
|
||||||
//AnsiColor GetColorCode();
|
|
||||||
void SetColorCode(Color4 cc = Colors::LightGray);
|
void SetColorCode(Color4 cc = Colors::LightGray);
|
||||||
Color4 GetColorCode();
|
Color4 GetColorCode();
|
||||||
public:
|
public:
|
||||||
std::string Context();
|
std::string Context();
|
||||||
void SetTraceback(bool enabled);
|
void SetTraceback(bool b);
|
||||||
|
void IncludeTimestamp(bool b);
|
||||||
/**/
|
/**/
|
||||||
protected:
|
protected:
|
||||||
bool enabled = true;
|
bool enabled = true;
|
||||||
std::string logfile = "latest.log";
|
std::string logfile = "latest.log";
|
||||||
//AnsiColor colorcode = AnsiColor::FG_DEFAULT;
|
|
||||||
Color4 colorcode = Colors::LightGray;
|
Color4 colorcode = Colors::LightGray;
|
||||||
std::string context;
|
std::string context;
|
||||||
bool trace = true;
|
bool trace = true;
|
||||||
|
bool timestamp = true;
|
||||||
};
|
};
|
||||||
}
|
}
|
4
main.cpp
4
main.cpp
@@ -61,7 +61,7 @@ int main()
|
|||||||
// We can also add event hooks to a logger.
|
// We can also add event hooks to a logger.
|
||||||
demo(std::format("{}\n>\t{}\n>\t\t{}\n>\n>\t\t{}\n>\t{}",
|
demo(std::format("{}\n>\t{}\n>\t\t{}\n>\n>\t\t{}\n>\t{}",
|
||||||
"We can also add event hooks to a logger.",
|
"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::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}));",
|
"jlog::log_to_console(jlog::toks2consoleMsg(std::vector<jlog::token>{dbg}));",
|
||||||
"}"
|
"}"
|
||||||
@@ -71,7 +71,7 @@ int main()
|
|||||||
demo("Before event hook");
|
demo("Before event hook");
|
||||||
|
|
||||||
// Create and add 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::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}));
|
//jlog::log_to_console(jlog::toks2consoleMsg(std::vector<jlog::token>{dbg}));
|
||||||
|
@@ -38,6 +38,8 @@ namespace jlog {
|
|||||||
auto ms = time.subseconds();
|
auto ms = time.subseconds();
|
||||||
return std::format("{}-{}-{} {}:{}:{}.{}", y, m, d, h.count(), M.count(), s.count(), ms.count());
|
return std::format("{}-{}-{} {}:{}:{}.{}", y, m, d, h.count(), M.count(), s.count(), ms.count());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
//[2024-Aug-14 12:0:58.815]
|
//[2024-Aug-14 12:0:58.815]
|
||||||
std::vector<token> timestamp_format(Timestamp tstamp) {
|
std::vector<token> timestamp_format(Timestamp tstamp) {
|
||||||
return std::vector<token>{{.content = std::format("{}-{}-{} {}:{}:{}.{}", tstamp.Year(), tstamp.Month(), tstamp.Day(), tstamp.Hour().count(), tstamp.Minute().count(), tstamp.Second().count(), tstamp.Millisecond().count())}};
|
return std::vector<token>{{.content = std::format("{}-{}-{} {}:{}:{}.{}", tstamp.Year(), tstamp.Month(), tstamp.Day(), tstamp.Hour().count(), tstamp.Minute().count(), tstamp.Second().count(), tstamp.Millisecond().count())}};
|
||||||
|
@@ -6,6 +6,42 @@
|
|||||||
|
|
||||||
namespace jlog
|
namespace jlog
|
||||||
{
|
{
|
||||||
|
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;
|
||||||
|
this->message = msg;
|
||||||
|
this->trace = t;
|
||||||
|
this->timestamp = ts;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::vector<token> LogEntry::Tokenize() {
|
||||||
|
std::vector<token> tokens;
|
||||||
|
|
||||||
|
if (IncludeTimestamp)
|
||||||
|
tokens.push_back({.colorCode = Colors::Gray, .content = std::format(
|
||||||
|
"{}-{}-{} {}:{}:{}.{}",
|
||||||
|
timestamp.Year(),
|
||||||
|
timestamp.Month(),
|
||||||
|
timestamp.Day(),
|
||||||
|
timestamp.Hour().count(),
|
||||||
|
timestamp.Minute().count(),
|
||||||
|
timestamp.Second().count(),
|
||||||
|
timestamp.Millisecond().count())});
|
||||||
|
|
||||||
|
tokens.push_back({.colorCode = context_color, .content = context});
|
||||||
|
|
||||||
|
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({.colorCode = Colors::Gray, .content = ">>", .delimiter = ""});
|
||||||
|
|
||||||
|
tokens.push_back({.colorCode = Colors::Gray, .content = message, .delimiter = ""});
|
||||||
|
|
||||||
|
return tokens;
|
||||||
|
}
|
||||||
|
|
||||||
Logger::Logger(const std::string& context, const Color4& color) {
|
Logger::Logger(const std::string& context, const Color4& color) {
|
||||||
this->context = context;
|
this->context = context;
|
||||||
this->colorcode = color;
|
this->colorcode = color;
|
||||||
@@ -15,23 +51,15 @@ namespace jlog
|
|||||||
if (!enabled)
|
if (!enabled)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
std::vector<jlog::token> fmt;
|
LogEntry logentry = {colorcode, context, message, location, Timestamp()};
|
||||||
|
logentry.IncludeTrace = this->trace;
|
||||||
|
logentry.IncludeTimestamp = this->timestamp;
|
||||||
|
|
||||||
if (trace)
|
OnLogEvent(logentry);
|
||||||
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);
|
LogToConsole(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(fmt);
|
|
||||||
LogToConsole("\n");
|
LogToConsole("\n");
|
||||||
//LogToConsole(std::vector<token>{jlog::endl});
|
LogToFile(logfile, logentry.Tokenize());
|
||||||
LogToFile(logfile, fmt);
|
|
||||||
LogToFile(logfile, "\n");
|
LogToFile(logfile, "\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,8 +73,6 @@ namespace jlog
|
|||||||
}
|
}
|
||||||
std::string Logger::LogFile() { return this->logfile; }
|
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; }
|
void Logger::SetColorCode(Color4 cc) { this->colorcode = cc; }
|
||||||
Color4 Logger::GetColorCode() { return this->colorcode; }
|
Color4 Logger::GetColorCode() { return this->colorcode; }
|
||||||
|
|
||||||
@@ -56,8 +82,8 @@ namespace jlog
|
|||||||
Log(message, location);
|
Log(message, location);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Logger::SetTraceback(bool enabled) {
|
void Logger::SetTraceback(bool b) {
|
||||||
trace = enabled;
|
trace = b;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Logger::Enable() { SetEnabled(true);}
|
void Logger::Enable() { SetEnabled(true);}
|
||||||
|
Reference in New Issue
Block a user