Stylistic adjustments

This commit is contained in:
2024-08-12 14:08:15 -04:00
parent 2795756235
commit 1ae346304e
5 changed files with 46 additions and 177 deletions

View File

@@ -38,7 +38,7 @@ Using jlog is straightforward:
#include <jlog.h> #include <jlog.h>
int main() { 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 Log levels
INFO("This is barely useful information."); INFO("This is barely useful information.");
DEBUG("Debugging Information"); DEBUG("Debugging Information");

View File

@@ -38,11 +38,11 @@ namespace jlog {
int line, int line,
const mcolor::ansiColors::Colors &severity_cc = mcolor::ansiColors::Colors::FG_WHITE); const mcolor::ansiColors::Colors &severity_cc = mcolor::ansiColors::Colors::FG_WHITE);
/// Returns a token sequence pre-formatted for the INFO log level. /// Returns a token sequence pre-formatted for the INFO Log level.
/// @param message The message to send out. /// @param message The message to send out.
std::vector<token> info_format(const std::string &message); 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 Log level.
/// @param message The message to send out. /// @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 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. /// @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, const std::string &file,
int line); int line);
/// Returns a token sequence pre-formatted for the WARNING log level. /// Returns a token sequence pre-formatted for the WARNING Log level.
/// @param message The message to send out. /// @param message The message to send out.
std::vector<token> warning_format(const std::string &message); 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 Log level.
/// @param message The message to send out. /// @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 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. /// @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, const std::string &file,
int line); int line);
/// Returns a token sequence pre-formatted for the ERROR log level. /// Returns a token sequence pre-formatted for the ERROR Log level.
/// @param message The message to send out. /// @param message The message to send out.
std::vector<token> error_format(const std::string &message); 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 Log level.
/// @param message The message to send out. /// @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 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. /// @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, const std::string &file,
int line); int line);
/// Returns a token sequence pre-formatted for the FATAL log level. /// Returns a token sequence pre-formatted for the FATAL Log level.
/// @param message The message to send out. /// @param message The message to send out.
std::vector<token> fatal_format(const std::string &message); 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 Log level.
/// @param message The message to send out. /// @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 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. /// @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, const std::string &file,
int line); int line);
/// Returns a token sequence pre-formatted for the VERBOSE log level. /// Returns a token sequence pre-formatted for the VERBOSE Log level.
/// @param message The message to send out. /// @param message The message to send out.
std::vector<token> verbose_format(const std::string &message); 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 Log level.
/// @param message The message to send out. /// @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 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. /// @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, const std::string &file,
int line); int line);
/// Returns a token sequence pre-formatted for the DEBUG log level. /// Returns a token sequence pre-formatted for the DEBUG Log level.
/// @param message The message to send out. /// @param message The message to send out.
std::vector<token> debug_format(const std::string &message); 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 Log level.
/// @param message The message to send out. /// @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 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. /// @param file The file name to trace back to. Should be provided by a __FILE__ macro variant.

View File

@@ -36,28 +36,13 @@ namespace jlog {
/// @note Does not append a newline to the message. /// @note Does not append a newline to the message.
void log_to_file(const std::string &filename, const std::string &message); void log_to_file(const std::string &filename, const std::string &message);
class LoggerInterface {
public:
//Logger(const std::string& context, const mcolor::ansiColors::Colors&);
virtual void log(const std::string &message) = 0;
virtual void log_trace(const std::string &message, const std::source_location& location = std::source_location::current()) = 0;
};
class ManagerInterface : public LoggerInterface {
public:
virtual std::string Context() = 0;
virtual void SetParent(ManagerInterface* p) = 0;
virtual ManagerInterface Parent() = 0;
virtual void AddChild(ManagerInterface* c) = 0;
virtual void DelChild(const std::string& context) = 0;
virtual std::map<std::string, ManagerInterface*> Children() = 0;
};
class Logger { class Logger {
public: public:
Logger(const std::string& context); Logger(const std::string& context, const mcolor::ansiColors::Colors& color = mcolor::ansiColors::Colors::FG_DEFAULT);
public: public:
void operator () (const std::string& message);
virtual void Log(const std::string &message); virtual void Log(const std::string &message);
//virtual void Log(const std::vector<token> tokens); //virtual void Log(const std::vector<token> tokens);
//virtual void Log(const std::string& context, const std::string &message); //virtual void Log(const std::string& context, const std::string &message);
@@ -82,41 +67,28 @@ namespace jlog {
virtual bool HasChild(Logger* c); virtual bool HasChild(Logger* c);
virtual Logger* GetChild(const std::string& context); virtual Logger* GetChild(const std::string& context);
virtual std::map<std::string, Logger*> Children(); virtual std::map<std::string, Logger*> Children();
void SetTraceback(bool enabled)
{
trace = enabled;
}
/**/ /**/
private: protected:
bool enabled = true; bool enabled = true;
std::string logfile = "latest.log"; std::string logfile = "latest.log";
mcolor::ansiColors::Colors colorcode = mcolor::ansiColors::Colors::FG_DEFAULT; mcolor::ansiColors::Colors colorcode = mcolor::ansiColors::Colors::FG_DEFAULT;
private:
std::string context; std::string context;
Logger* parent; Logger* parent;
std::map<std::string, Logger*> children; std::map<std::string, Logger*> children;
bool trace = true;
}; };
} }
namespace jlog::GlobalLogger { namespace jlog {
class GlobalLogger : jlog::LoggerInterface {
public:
GlobalLogger(const std::string& context, const mcolor::ansiColors::Colors& colorcode);
void log(const std::string &message);
void log_trace(const std::string &message, const std::source_location& location = std::source_location::current());
std::string Context() { return context; };
public:
bool enabled = true;
//bool record_console = true;
//bool record_file = true;
// Changing this is bad practice, but we're allowing it for niche cases
std::string logfile = "latest.log";
private:
std::string context;
mcolor::ansiColors::Colors colorcode;
}; Logger INFO{"INFO", mcolor::ansiColors::Colors::FG_GREEN};
Logger WARNING{"WARNING", mcolor::ansiColors::Colors::FG_YELLOW};
GlobalLogger INFO{"INFO", mcolor::ansiColors::Colors::FG_GREEN}; Logger ERROR{"ERROR", mcolor::ansiColors::Colors::FG_RED};
GlobalLogger WARNING{"WARNING", mcolor::ansiColors::Colors::FG_YELLOW}; Logger FATAL{"FATAL", mcolor::ansiColors::Colors::FG_BRIGHT_RED};
GlobalLogger ERROR{"ERROR", mcolor::ansiColors::Colors::FG_RED}; Logger VERBOSE{"VERBOSE", mcolor::ansiColors::Colors::FG_CYAN};
GlobalLogger FATAL{"FATAL", mcolor::ansiColors::Colors::FG_BRIGHT_RED}; Logger DEBUG{"DEBUG", mcolor::ansiColors::Colors::FG_MAGENTA};
GlobalLogger VERBOSE{"VERBOSE", mcolor::ansiColors::Colors::FG_CYAN};
GlobalLogger DEBUG{"DEBUG", mcolor::ansiColors::Colors::FG_MAGENTA};
} }

104
main.cpp
View File

@@ -7,96 +7,23 @@
#include <jlog/jlog.hpp> #include <jlog/jlog.hpp>
#include "map" #include "map"
/*
namespace CoolProject {}
namespace CoolProject::Logger {
jlog::severity LEVEL = jlog::severity::debug;
class Logger : jlog::Logger {
public:
Logger(const std::string& context, const mcolor::ansiColors::Colors& colorcode, const jlog::severity& level);
void log(const std::string &message);
void log_trace(const std::string &message, const std::source_location& location = std::source_location::current());
//bool Enabled() { return enabled; };
std::string Context() { return context; };
jlog::severity Severity() { return severity; };
public:
bool enabled = true;
//bool record_console = true;
//bool record_file = true;
// Changing this is bad practice, but we're allowing it for niche cases
std::string logfile = "latest.log";
private:
std::string context;
mcolor::ansiColors::Colors colorcode;
jlog::severity severity;
};
Logger INFO{"INFO", mcolor::ansiColors::Colors::FG_GREEN, jlog::severity::info};
Logger WARNING{"WARNING", mcolor::ansiColors::Colors::FG_YELLOW, jlog::severity::warning};
Logger ERROR{"ERROR", mcolor::ansiColors::Colors::FG_RED, jlog::severity::error};
Logger FATAL{"FATAL", mcolor::ansiColors::Colors::FG_BRIGHT_RED, jlog::severity::fatal};
Logger VERBOSE{"VERBOSE", mcolor::ansiColors::Colors::FG_CYAN, jlog::severity::verbose};
Logger DEBUG{"DEBUG", mcolor::ansiColors::Colors::FG_MAGENTA, jlog::severity::debug};
Logger::Logger(const std::string &context, const mcolor::ansiColors::Colors& colorcode, const jlog::severity& level) {
this->context = context;
this->colorcode = colorcode;
this->severity = level;
}
void Logger::log(const std::string &message) {
if ((!enabled) || (severity > LEVEL))
return;
std::vector<jlog::token> fmt = jlog::log_format(this->context, message, this->colorcode);
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
}
void Logger::log_trace(const std::string &message, const std::source_location &location) {
if ((!enabled) || (severity > LEVEL))
return;
//std::vector<jlog::token> fmt = log_format(this->context, message, func, file, line,this->colorcode);
std::vector<jlog::token> fmt = jlog::log_format(this->context, message, location.function_name(), location.file_name(), location.line(),this->colorcode);
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
}
}
*/
int main() int main()
{ {
jlog::GlobalLogger::INFO.log("This dick"); jlog::INFO("This dick");
jlog::GlobalLogger::DEBUG.log("This dick"); jlog::DEBUG("This dick");
jlog::GlobalLogger::VERBOSE.log("This dick"); jlog::VERBOSE.Log("This dick");
jlog::GlobalLogger::WARNING.log("This dick"); jlog::WARNING.Log("This dick");
jlog::GlobalLogger::ERROR.log("This dick"); jlog::ERROR.Log("This dick");
jlog::GlobalLogger::FATAL.log("This dick"); jlog::FATAL.Log("This dick");
jlog::GlobalLogger::INFO.log_trace("This dick"); jlog::INFO("This dick");
jlog::GlobalLogger::DEBUG.log_trace("This dick"); jlog::DEBUG.LogTrace("This dick");
jlog::GlobalLogger::VERBOSE.log_trace("This dick"); jlog::VERBOSE.LogTrace("This dick");
jlog::GlobalLogger::WARNING.log_trace("This dick"); jlog::WARNING.LogTrace("This dick");
jlog::GlobalLogger::ERROR.log_trace("This dick"); jlog::ERROR.LogTrace("This dick");
jlog::GlobalLogger::FATAL.log_trace("This dick"); jlog::FATAL.LogTrace("This dick");
//struct { std::string ass; std::string balls; } dick{"cum","cock"};
//jlog::GlobalLogger::DEBUG.log_trace(dick.ass + dick.balls);
/*
CoolProject::Logger::INFO.log_trace("This BALLS");
CoolProject::Logger::INFO.log_trace("This dick");
CoolProject::Logger::DEBUG.log_trace("This dick");
CoolProject::Logger::VERBOSE.log_trace("This dick");
CoolProject::Logger::WARNING.log_trace("This dick");
CoolProject::Logger::ERROR.log_trace("This dick");
CoolProject::Logger::FATAL.log_trace("This dick");
*/
jlog::Logger cock{"COCK"}; jlog::Logger cock{"COCK"};
//jlog::Logger info{"I"}; //jlog::Logger info{"I"};
@@ -106,15 +33,10 @@ int main()
cock.AddChild(&error); cock.AddChild(&error);
cock.Log("dick ass"); cock.Log("dick ass");
//cock.log("I" "dick ass"); //cock.Log("I" "dick ass");
error.Log("dick ass"); error.Log("dick ass");
error.LogTrace("dick ass" " " + error.Parent()->Context()); error.LogTrace("dick ass" " " + error.Parent()->Context());
//cock.DelChild("E");
//error.LogTrace("dick ass" " " + error.Parent()->Context());
return 0; return 0;
} }

View File

@@ -29,10 +29,11 @@ namespace jlog
latest_log.close(); latest_log.close();
} }
Logger::Logger(const std::string& context) { Logger::Logger(const std::string& context, const mcolor::ansiColors::Colors& color) {
this->context = context; this->context = context;
this->parent = this; this->parent = this;
this->children = {}; this->children = {};
this->colorcode = color;
} }
void Logger::Log(const std::string &message) { void Logger::Log(const std::string &message) {
@@ -155,34 +156,8 @@ namespace jlog
return this->children; return this->children;
} }
} void Logger::operator()(const std::string &message) {
Log(message);
namespace jlog::GlobalLogger {
GlobalLogger::GlobalLogger(const std::string &context, const mcolor::ansiColors::Colors& colorcode) {
this->context = context;
this->colorcode = colorcode;
}
void GlobalLogger::log(const std::string &message) {
if (!enabled)
return;
std::vector<jlog::token> fmt = log_format(this->context, message, this->colorcode);
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
}
void GlobalLogger::log_trace(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);
jlog::log_to_console(jlog::toks2consoleMsg(fmt) + '\n');
jlog::log_to_file(this->logfile, jlog::toks2logfileMsg(fmt) + '\n');
} }
} }