Decent-size refactor pt1

This commit is contained in:
2024-08-12 23:21:33 -04:00
parent 1ae346304e
commit 96b759d54e
13 changed files with 241 additions and 265 deletions

View File

@@ -28,7 +28,7 @@ CPMAddPackage(
CPMAddPackage(
NAME mcolor
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-1.zip
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-4.zip
)
if (UNIX)
@@ -49,7 +49,7 @@ install(FILES ${jlog_HEADERS} DESTINATION include/${PROJECT_NAME})
#add_subdirectory(tests)
target_link_libraries(jlog PRIVATE Event mcolor)
target_link_libraries(jlog PUBLIC Event mcolor)
add_executable(LoggerDemo main.cpp)
target_link_libraries(LoggerDemo PUBLIC jlog)

View File

@@ -38,14 +38,14 @@ Using jlog is straightforward:
#include <jlog.h>
int main() {
LOGLEVEL(jlog::severity::debug); // <- see jlog::severity for full list of Log levels
LOGLEVEL(jlog::severity::debug); // <- see jlog::severity for full list of LogTrace levels
INFO("This is barely useful information.");
DEBUG("Debugging Information");
VERBOSE("Yadda Yadda Yadda");
WARNING("Slight miscalculation!");
ERROR("Oops, something went wrong.");
FATAL("Unrecoverable Error!!!");
info("This is barely useful information.");
debug("Debugging Information");
verbose("Yadda Yadda Yadda");
warning("Slight miscalculation!");
error("Oops, something went wrong.");
fatal("Unrecoverable Error!!!");
return 0;
}

View File

@@ -21,7 +21,7 @@ namespace jlog {
std::vector<token> log_format(
const std::string &severity_name,
const std::string &message,
const mcolor::ansiColors::Colors &severity_cc = mcolor::ansiColors::Colors::FG_WHITE);
const AnsiColor &severity_cc = AnsiColor::FG_WHITE);
/// Returns a more detailed formatted sequence of tokens.
/// @param severity_name The severity tag to prefix to the message. Could theoretically also be a context.
@@ -36,13 +36,13 @@ namespace jlog {
const std::string &func,
const std::string &file,
int line,
const mcolor::ansiColors::Colors &severity_cc = mcolor::ansiColors::Colors::FG_WHITE);
const AnsiColor &severity_cc = AnsiColor::FG_WHITE);
/// Returns a token sequence pre-formatted for the INFO Log level.
/// Returns a token sequence pre-formatted for the info LogTrace level.
/// @param message The message to send out.
std::vector<token> info_format(const std::string &message);
/// Returns a token sequence pre-formatted for the INFO Log level.
/// Returns a token sequence pre-formatted for the info LogTrace level.
/// @param message The message to send out.
/// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant.
/// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant.
@@ -53,11 +53,11 @@ namespace jlog {
const std::string &file,
int line);
/// Returns a token sequence pre-formatted for the WARNING Log level.
/// Returns a token sequence pre-formatted for the warning LogTrace level.
/// @param message The message to send out.
std::vector<token> warning_format(const std::string &message);
/// Returns a token sequence pre-formatted for the WARNING Log level.
/// Returns a token sequence pre-formatted for the warning LogTrace level.
/// @param message The message to send out.
/// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant.
/// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant.
@@ -68,11 +68,11 @@ namespace jlog {
const std::string &file,
int line);
/// Returns a token sequence pre-formatted for the ERROR Log level.
/// Returns a token sequence pre-formatted for the error LogTrace level.
/// @param message The message to send out.
std::vector<token> error_format(const std::string &message);
/// Returns a token sequence pre-formatted for the ERROR Log level.
/// Returns a token sequence pre-formatted for the error LogTrace level.
/// @param message The message to send out.
/// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant.
/// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant.
@@ -83,11 +83,11 @@ namespace jlog {
const std::string &file,
int line);
/// Returns a token sequence pre-formatted for the FATAL Log level.
/// Returns a token sequence pre-formatted for the fatal LogTrace level.
/// @param message The message to send out.
std::vector<token> fatal_format(const std::string &message);
/// Returns a token sequence pre-formatted for the FATAL Log level.
/// Returns a token sequence pre-formatted for the fatal LogTrace level.
/// @param message The message to send out.
/// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant.
/// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant.
@@ -98,11 +98,11 @@ namespace jlog {
const std::string &file,
int line);
/// Returns a token sequence pre-formatted for the VERBOSE Log level.
/// Returns a token sequence pre-formatted for the verbose LogTrace level.
/// @param message The message to send out.
std::vector<token> verbose_format(const std::string &message);
/// Returns a token sequence pre-formatted for the VERBOSE Log level.
/// Returns a token sequence pre-formatted for the verbose LogTrace level.
/// @param message The message to send out.
/// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant.
/// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant.
@@ -113,11 +113,11 @@ namespace jlog {
const std::string &file,
int line);
/// Returns a token sequence pre-formatted for the DEBUG Log level.
/// Returns a token sequence pre-formatted for the debug LogTrace level.
/// @param message The message to send out.
std::vector<token> debug_format(const std::string &message);
/// Returns a token sequence pre-formatted for the DEBUG Log level.
/// Returns a token sequence pre-formatted for the debug LogTrace level.
/// @param message The message to send out.
/// @param func The function name/signature to trace back to. Should be provided by a __FUNCTION__ macro variant.
/// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant.

14
include/jlog/io.hpp Normal file
View File

@@ -0,0 +1,14 @@
#pragma once
#include <string>
namespace jlog
{
/// Writes an input string directly to standard output
/// @note Does not append a newline to the message.
void log_to_console(const std::string &message);
/// Writes an input string directly to the specified destination logfile
/// @note Does not append a newline to the message.
void log_to_file(const std::string &filename, const std::string &message);
}

View File

@@ -14,81 +14,21 @@
#include "token.hpp"
#include "formatter.hpp"
#include "source_location"
#include <jlog/logger.hpp>
namespace jlog {
/// Severity levels for logging. Higher numbers filter less messages out.
/// @see LOG_LEVEL macro
enum class severity : uint8_t {
none, // Show no output
info, // show only info messages
fatal, // Show only fatal errors
error, // Show fatal and not-necessarily-fatal errors
warning, // Show warnings additionally
verbose, // Show errors, warnings, and 'verbose' output (i.e. extra information)
debug, // Show debugging messages (Basically everything).
};
/// Writes an input string directly to standard output
/// @note Does not append a newline to the message.
void log_to_console(const std::string &message);
using namespace mcolor;
/// Writes an input string directly to the specified destination logfile
/// @note Does not append a newline to the message.
void log_to_file(const std::string &filename, const std::string &message);
/// Built-in Global Loggers
extern Logger info;
extern Logger warning;
extern Logger error;
extern Logger fatal;
extern Logger verbose;
extern Logger debug;
class Logger {
public:
Logger(const std::string& context, const mcolor::ansiColors::Colors& color = mcolor::ansiColors::Colors::FG_DEFAULT);
public:
void operator () (const std::string& message);
virtual void Log(const std::string &message);
//virtual void Log(const std::vector<token> tokens);
//virtual void Log(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:
virtual void Enabled(bool state);
virtual bool Enabled();
virtual void LogFile(const std::string& f);
virtual std::string LogFile();
// no cc no bullshit
virtual void ColorCode(mcolor::ansiColors::Colors cc = mcolor::ansiColors::Colors::FG_DEFAULT);
virtual mcolor::ansiColors::Colors ColorCode();
public:
virtual std::string Context();
// This whole parent child system is probably overkill for our use cases.
/**/
virtual void SetParent(Logger* p);
virtual Logger* Parent();
virtual void AddChild(Logger* c);
virtual void DelChild(const std::string& context);
virtual bool HasChild(const std::string& context);
virtual bool HasChild(Logger* c);
virtual Logger* GetChild(const std::string& context);
virtual std::map<std::string, Logger*> Children();
void SetTraceback(bool enabled)
{
trace = enabled;
}
/**/
protected:
bool enabled = true;
std::string logfile = "latest.log";
mcolor::ansiColors::Colors colorcode = mcolor::ansiColors::Colors::FG_DEFAULT;
std::string context;
Logger* parent;
std::map<std::string, Logger*> children;
bool trace = true;
};
void SetTracebackOnGlobalLoggers(bool enabled);
}
namespace jlog {
Logger INFO{"INFO", mcolor::ansiColors::Colors::FG_GREEN};
Logger WARNING{"WARNING", mcolor::ansiColors::Colors::FG_YELLOW};
Logger ERROR{"ERROR", mcolor::ansiColors::Colors::FG_RED};
Logger FATAL{"FATAL", mcolor::ansiColors::Colors::FG_BRIGHT_RED};
Logger VERBOSE{"VERBOSE", mcolor::ansiColors::Colors::FG_CYAN};
Logger DEBUG{"DEBUG", mcolor::ansiColors::Colors::FG_MAGENTA};
}

45
include/jlog/logger.hpp Normal file
View File

@@ -0,0 +1,45 @@
#pragma once
#include <mcolor.h>
#include <source_location>
#include <map>
namespace jlog
{
using namespace mcolor;
///
class Logger {
public:
explicit Logger(const std::string& context, const AnsiColor& color = AnsiColor::FG_DEFAULT);
public:
void operator () (const std::string& message);
virtual void Log(const std::string &message);
//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();
void Disable();
bool Enabled();
void LogFile(const std::string& f);
std::string LogFile();
// no cc no bullshit
void ColorCode(AnsiColor cc = AnsiColor::FG_DEFAULT);
AnsiColor ColorCode();
public:
std::string Context();
void SetTraceback(bool enabled);
/**/
protected:
bool enabled = true;
std::string logfile = "latest.log";
AnsiColor colorcode = AnsiColor::FG_DEFAULT;
std::string context;
Logger* parent;
std::map<std::string, Logger*> children;
bool trace = true;
};
}

View File

@@ -1,12 +1,16 @@
#pragma once
#include <functional>
#include <mcolor.h>
namespace jlog {
using namespace mcolor;
/// A single piece of a logging message, with color code, content, and delimiter
/// These are strung together to build full logger messages in a flexible manner.
struct token {
mcolor::ansiColors::Colors colorCode = mcolor::ansiColors::Colors::FG_DEFAULT;
AnsiColor colorCode = AnsiColor::FG_DEFAULT;
std::string content = "";
std::string delimiter = "[]";
};

View File

@@ -5,37 +5,44 @@
// Contributors: william@redacted.cc maxi@redacted.cc
// This work is dedicated to the public domain.
#include <jlog/jlog.hpp>
#include "map"
#include <mcolor.h>
int main()
{
jlog::INFO("This dick");
jlog::DEBUG("This dick");
jlog::VERBOSE.Log("This dick");
jlog::WARNING.Log("This dick");
jlog::ERROR.Log("This dick");
jlog::FATAL.Log("This dick");
using namespace mcolor;
jlog::INFO("This dick");
jlog::DEBUG.LogTrace("This dick");
jlog::VERBOSE.LogTrace("This dick");
jlog::WARNING.LogTrace("This dick");
jlog::ERROR.LogTrace("This dick");
jlog::FATAL.LogTrace("This dick");
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::SetTracebackOnGlobalLoggers(true);
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::Logger cock{"COCK"};
//jlog::Logger info{"I"};
jlog::Logger error{"E"};
error.ColorCode(mcolor::ansiColors::Colors::FG_BRIGHT_RED);
//cock.AddChild(&info);
cock.AddChild(&error);
error.ColorCode(AnsiColor::FG_BRIGHT_RED);
cock.Log("dick ass");
//cock.Log("I" "dick ass");
//cock.LogTrace("I" "dick ass");
error.Log("dick ass");
error.LogTrace("dick ass" " " + error.Parent()->Context());
return 0;
}

View File

@@ -36,7 +36,7 @@ namespace jlog {
std::vector<token> log_format(
const std::string& severity_name,
const std::string& message,
const mcolor::ansiColors::Colors& severity_cc)
const AnsiColor& severity_cc)
{
auto severity = token{.colorCode = severity_cc, .content = severity_name};
auto content = token{.content = message, .delimiter = ""};
@@ -49,7 +49,7 @@ namespace jlog {
const std::string& func,
const std::string& file,
int line,
const mcolor::ansiColors::Colors& severity_cc)
const AnsiColor& severity_cc)
{
std::vector<token> tokens;
auto timestamp = token{.content = jlog::get_timestamp()};

22
src/jlog/io.cpp Normal file
View File

@@ -0,0 +1,22 @@
#include <jlog/io.hpp>
#include <fstream>
#include <iostream>
#include <chrono>
namespace jlog
{
void log_to_console(const std::string& message)
{
#ifdef WIN32
mcolor::windowsSaneify();
#endif
std::cout << message;
}
void log_to_file(const std::string& filename, const std::string& message)
{
std::ofstream latest_log(filename, std::ios_base::app);
latest_log << message;
latest_log.close();
}
}

View File

@@ -12,152 +12,18 @@
namespace jlog
{
Logger info {"info", AnsiColor::FG_GREEN};
Logger warning {"warning", AnsiColor::FG_YELLOW};
Logger error {"error", AnsiColor::FG_RED};
Logger fatal {"fatal", AnsiColor::FG_BRIGHT_RED};
Logger verbose {"verbose", AnsiColor::FG_CYAN};
Logger debug {"debug", AnsiColor::FG_MAGENTA};
void log_to_console(const std::string& message)
{
#ifdef WIN32
mcolor::windowsSaneify();
#endif
std::cout << message;
void SetTracebackOnGlobalLoggers(bool enabled) {
info.SetTraceback(enabled);
warning.SetTraceback(enabled);
error.SetTraceback(enabled);
fatal.SetTraceback(enabled);
verbose.SetTraceback(enabled);
}
void log_to_file(const std::string& filename, const std::string& message)
{
std::ofstream latest_log(filename, std::ios_base::app);
latest_log << message;
latest_log.close();
}
Logger::Logger(const std::string& context, const mcolor::ansiColors::Colors& color) {
this->context = context;
this->parent = this;
this->children = {};
this->colorcode = color;
}
void Logger::Log(const std::string &message) {
if (!enabled)
return;
std::vector<jlog::token> 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::Log(const std::string& context, const std::string &message) {
Logger* c = GetChild(context);
c->Log(message);
}
*/
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::Enabled(bool state) {
this->enabled = state;
for (auto const& [_, logger] : this->children) {
logger->Enabled(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; }
void Logger::ColorCode(mcolor::ansiColors::Colors cc) { this->colorcode = cc; }
mcolor::ansiColors::Colors Logger::ColorCode() { return this->colorcode; }
std::string Logger::Context() { return this->context; }
// DOnt be stupid
void Logger::SetParent(Logger *p) {
//this->parent->DelChild(this->context);
this->parent = p;
// We make the child inherit the parents traits.
// Is this strictly required? No.
// Should probably mention this may cause side effects.
this->Enabled(p->Enabled());
this->LogFile(p->LogFile());
//p->AddChild(this);
}
Logger* Logger::Parent() { return this->parent; }
void Logger::AddChild(Logger *c) {
c->SetParent(this);
this->children[c->Context()] = c;
}
// Child will still keep whatever it previously inherited from the parent
// C'mon you mean to tell me when your parent disowns you ya gotta give back all
// the shit you inherited from them? I guess it would be pretty cool to give back
// debt.
void Logger::DelChild(const std::string& context) {
auto c = this->children.find(context);
if (c != this->children.end()) {
this->children.erase(context);
c->second->SetParent(c->second);
} else {
throw ("Child does not exist.");
}
}
bool Logger::HasChild(const std::string& context) {
auto c = this->children.find(context);
return c != this->children.end();
}
bool Logger::HasChild(Logger* c) {
auto result = std::find_if(
this->children.begin(), this->children.end(),
[c](const auto& mo) {return mo.second == c; });
return result != this->children.end();
}
Logger* Logger::GetChild(const std::string& context) {
if (!HasChild(context)) {
throw ("Child does not exist.");
}
// Yes yes I know we're accessing the map twice making it slower
// so fuck off this is more flexible.
auto c = this->children.find(context);
return c->second;
}
std::map<std::string, Logger*> Logger::Children() {
return this->children;
}
void Logger::operator()(const std::string &message) {
Log(message);
}
}

78
src/jlog/logger.cpp Normal file
View File

@@ -0,0 +1,78 @@
#include <jlog/logger.hpp>
#include <jlog/jlog.hpp>
#include <jlog/formatter.hpp>
#include <jlog/token.hpp>
#include "jlog/io.hpp"
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) {
if (!enabled)
return;
std::vector<jlog::token> 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; }
void Logger::ColorCode(AnsiColor cc) { this->colorcode = cc; }
AnsiColor Logger::ColorCode() { return this->colorcode; }
std::string Logger::Context() { return this->context; }
void Logger::operator()(const std::string &message) {
Log(message);
}
void Logger::SetTraceback(bool enabled) {
trace = enabled;
}
void Logger::Enable() { SetEnabled(true);}
void Logger::Disable() { SetEnabled(true); }
}

View File

@@ -2,8 +2,8 @@
#include <vector>
#include <functional>
#include <format>
#include "mcolor.h"
#include "jlog/token.hpp"
#include <mcolor.h>
#include <jlog/token.hpp>
namespace jlog {
std::string toks2msg(std::vector <token> tokens, std::function<std::string(token)> formatter) {
@@ -25,11 +25,11 @@ namespace jlog {
std::string consoleMsgFormatter(token t) {
if (!t.delimiter.empty()) {
return std::format("{}{}{}{}{} ", mcolor::toEscapeCode(t.colorCode), t.delimiter[0], t.content,
t.delimiter[1], mcolor::toEscapeCode(mcolor::ansiColors::Colors::RESET));
t.delimiter[1], mcolor::toEscapeCode(AnsiColor::RESET));
}
return std::format("{}{}{} ", mcolor::toEscapeCode(t.colorCode), t.content,
mcolor::toEscapeCode(mcolor::ansiColors::Colors::RESET));
mcolor::toEscapeCode(AnsiColor::RESET));
}
std::string logfileMsgFormatter(token t) {