Implemented ability to specify custom logfile globally and per-function. Wrote helper functions to make writing custom loggers easier. Still need to work on the windows side of things, but that should be easy.
This commit is contained in:
@@ -72,13 +72,24 @@ namespace jlog
|
||||
/// @note Does not append a newline to the message.
|
||||
void log_to_stream(std::ostream stream, const std::string& message);
|
||||
|
||||
/// These are helper functions designed to be wrapped around for easier custom logger building.
|
||||
/// @note This file is implemented differently per-platform to handle differences in console color handling.
|
||||
/// @see windows/jlog.cpp linux/jlog.cpp
|
||||
std::string toks2msg(std::vector<token> tokens, std::string (*formatter)(token));
|
||||
std::string consoleMsgFormatter(token t);
|
||||
std::string logfileMsgFormatter(token t);
|
||||
std::string toks2consoleMsg(std::vector<token> tokens);
|
||||
std::string toks2logfileMsg(std::vector<token> tokens);
|
||||
|
||||
/// Parses a sequence of tokens into a printable message, and logs to the following destinations:
|
||||
/// standard output (console)
|
||||
/// output file (default)
|
||||
/// logging callback (Event)
|
||||
/// @note This file is implemented differently per-platform to handle differences in console color handling.
|
||||
/// @see windows/jlog.cpp linux/jlog.cpp
|
||||
void log(std::vector<token> tokens, const std::string& filename);
|
||||
void log(std::vector<token> tokens);
|
||||
void ltlog(std::vector<token> tokens); // Just for debug purposes
|
||||
|
||||
// Generic formatters for building loggers.
|
||||
|
||||
@@ -164,3 +175,5 @@ namespace jlog
|
||||
|
||||
#define LOGLEVEL(i) jlog::loglevel = i;
|
||||
|
||||
#define LOGTEST(i) if (jlog::loglevel >= jlog::severity::none) { jlog::ltlog(jlog::info_detailed_format(i, FUNCTION, __FILE__, __LINE__)); }
|
||||
|
||||
|
3
main.cpp
3
main.cpp
@@ -34,6 +34,9 @@ int main()
|
||||
//COOLINFO("This is really cool!!!");
|
||||
//COOLINFOTRACE("THIS IS EVEN COOLER!!!");
|
||||
|
||||
LOGTEST("This is a really cool test man :3");
|
||||
LOGTEST("Go check cmake-build-debug/logtest.log and cmake-build-debug/latest.log");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
@@ -1,17 +1,31 @@
|
||||
#include <jlog/jlog.hpp>
|
||||
|
||||
namespace jlog {
|
||||
void log(std::vector<token> tokens) {
|
||||
for (token t: tokens) {
|
||||
if (!t.delimiter.empty())
|
||||
log_to_console(std::format("{}{}{}{}{} ", t.colorCode.ansi_code, t.delimiter[0], t.content, t.delimiter[1], ansi_escape_codes::RESET)),
|
||||
log_to_file(std::format("{}{}{} ", t.delimiter[0], t.content, t.delimiter[1]));
|
||||
|
||||
else
|
||||
log_to_console(std::format("{}{}{} ", t.colorCode.ansi_code, t.content, ansi_escape_codes::RESET)),
|
||||
log_to_file(t.content + " ");
|
||||
std::string consoleMsgFormatter(token t)
|
||||
{
|
||||
if (!t.delimiter.empty())
|
||||
{
|
||||
return std::format("{}{}{}{}{} ", t.colorCode.ansi_code, t.delimiter[0], t.content, t.delimiter[1], ansi_escape_codes::RESET);
|
||||
}
|
||||
|
||||
return std::format("{}{}{} ", t.colorCode.ansi_code, t.content, ansi_escape_codes::RESET);
|
||||
}
|
||||
|
||||
std::string logfileMsgFormatter(token t)
|
||||
{
|
||||
if (!t.delimiter.empty())
|
||||
{
|
||||
return std::format("{}{}{} ", t.delimiter[0], t.content, t.delimiter[1]);
|
||||
}
|
||||
|
||||
return t.content + " ";
|
||||
}
|
||||
|
||||
void log(std::vector<token> tokens, const std::string& filename)
|
||||
{
|
||||
log_to_console(toks2consoleMsg(tokens));
|
||||
log_to_console("\n");
|
||||
log_to_file("\n");
|
||||
log_to_file(filename, toks2logfileMsg(tokens));
|
||||
log_to_file(filename, "\n");
|
||||
}
|
||||
}
|
@@ -55,6 +55,38 @@ namespace jlog
|
||||
stream << message;
|
||||
}
|
||||
|
||||
std::string toks2msg(std::vector<token> tokens, std::string (*formatter)(token))
|
||||
{
|
||||
std::string msg;
|
||||
for (token t: tokens)
|
||||
{
|
||||
msg += formatter(t);
|
||||
}
|
||||
return msg;
|
||||
}
|
||||
|
||||
std::string toks2consoleMsg(std::vector<token> tokens)
|
||||
{
|
||||
return toks2msg(tokens, consoleMsgFormatter);
|
||||
}
|
||||
|
||||
std::string toks2logfileMsg(std::vector<token> tokens)
|
||||
{
|
||||
return toks2msg(tokens, logfileMsgFormatter);
|
||||
}
|
||||
|
||||
void log(std::vector<token> tokens)
|
||||
{
|
||||
log(tokens, default_logfile);
|
||||
}
|
||||
|
||||
// Mainly for debug purposes
|
||||
void ltlog(std::vector<token> tokens)
|
||||
{
|
||||
log(tokens);
|
||||
log_to_file("logtest.log", toks2logfileMsg(tokens) + "\n");
|
||||
}
|
||||
|
||||
std::vector<token> trace_format(
|
||||
const std::string& func,
|
||||
const std::string& file,
|
||||
|
Reference in New Issue
Block a user