Smoothing it out
This commit is contained in:
@@ -13,12 +13,12 @@ namespace jlog
|
||||
public:
|
||||
explicit Logger(const std::string& context, const AnsiColor& color = AnsiColor::FG_DEFAULT);
|
||||
public:
|
||||
void operator () (const std::string& message);
|
||||
void operator () (const std::string& message, const std::source_location& location = std::source_location::current());
|
||||
|
||||
virtual void Log(const std::string &message);
|
||||
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());
|
||||
//virtual void LogTrace(const std::string &message, const std::source_location& location = std::source_location::current());
|
||||
public:
|
||||
void SetEnabled(bool state);
|
||||
void Enable();
|
||||
@@ -38,8 +38,6 @@ namespace jlog
|
||||
std::string logfile = "latest.log";
|
||||
AnsiColor colorcode = AnsiColor::FG_DEFAULT;
|
||||
std::string context;
|
||||
Logger* parent;
|
||||
std::map<std::string, Logger*> children;
|
||||
bool trace = true;
|
||||
};
|
||||
}
|
@@ -8,54 +8,31 @@ namespace jlog
|
||||
{
|
||||
Logger::Logger(const std::string& context, const AnsiColor& color) {
|
||||
this->context = context;
|
||||
this->parent = this;
|
||||
this->colorcode = color;
|
||||
}
|
||||
|
||||
void Logger::Log(const std::string &message) {
|
||||
void Logger::Log(const std::string &message, const std::source_location& location) {
|
||||
if (!enabled)
|
||||
return;
|
||||
|
||||
std::vector<jlog::token> fmt = log_format(this->context, message, this->colorcode);
|
||||
std::vector<jlog::token> fmt;
|
||||
|
||||
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);
|
||||
|
||||
if (this == this->parent) {
|
||||
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
|
||||
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
|
||||
} else {
|
||||
// Using toks2consoleMsg is hacky, but it's a simple solution and it's generic enough
|
||||
this->parent->Log(jlog::toks2consoleMsg(fmt));
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::LogTrace(const std::string &message, const std::source_location& location) {
|
||||
if (!enabled)
|
||||
return;
|
||||
|
||||
//std::vector<jlog::token> fmt = log_format(this->context, message, func, file, line,this->colorcode);
|
||||
std::vector<jlog::token> fmt = log_format(this->context, message, location.function_name(), location.file_name(), location.line(),this->colorcode);
|
||||
|
||||
if (this == this->parent) {
|
||||
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
|
||||
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
|
||||
} else {
|
||||
// Using toks2consoleMsg is hacky, but it's a simple solution and it's generic enough
|
||||
this->parent->Log(jlog::toks2consoleMsg(fmt));
|
||||
}
|
||||
}
|
||||
|
||||
void Logger::SetEnabled(bool state) {
|
||||
this->enabled = state;
|
||||
for (auto const& [_, logger] : this->children) {
|
||||
logger->SetEnabled(state);
|
||||
}
|
||||
}
|
||||
bool Logger::Enabled() { return this->enabled; }
|
||||
|
||||
void Logger::LogFile(const std::string& f) {
|
||||
this->logfile = f;
|
||||
for (auto const& [_, logger] : this->children) {
|
||||
logger->LogFile(f);
|
||||
}
|
||||
}
|
||||
std::string Logger::LogFile() { return this->logfile; }
|
||||
|
||||
@@ -64,8 +41,8 @@ namespace jlog
|
||||
|
||||
std::string Logger::Context() { return this->context; }
|
||||
|
||||
void Logger::operator()(const std::string &message) {
|
||||
Log(message);
|
||||
void Logger::operator()(const std::string &message, const std::source_location& location) {
|
||||
Log(message, location);
|
||||
}
|
||||
|
||||
void Logger::SetTraceback(bool enabled) {
|
||||
|
Reference in New Issue
Block a user