30 Commits

Author SHA1 Message Date
aace84af52 Allow disabling of console output for logging. New macro CONSOLELOGGING set to true/false to enable/disable. 2024-07-18 10:48:27 -04:00
maxine
47e06dd8ed Merge pull request 'mcolortest merge' (#12) from mcolortest into main
Reviewed-on: #12
2024-07-18 10:05:45 -04:00
cad79a474a Make jlog log_to_console use mcolor windowsSaneify for Windows platform 2024-07-02 13:33:52 -04:00
67a83bbac4 Removed need for jlog to handle colors. Using mcolor library now. 2024-07-02 13:28:00 -04:00
e553c74ade Cleanup 2024-07-01 14:48:56 -04:00
9d36f07717 Beating Windows and MSVC further into submission. 2024-07-01 14:28:18 -04:00
47caaba587 Forcing abstraction to work with windows and beating windows into submission to work like a good boy 2024-07-01 14:05:12 -04:00
f2651b58df FIx bug causing std::max to be an error in j3ml QuaternionTests 2024-06-28 18:19:22 -04:00
83df783e7c Unfortunate regression to get Windows ColorCodes working again. Will clean up shortly. (Test On Linux Also) 2024-06-28 14:45:04 -04:00
99c1fe9f4c Color output currently broken, see notes. 2024-06-28 14:09:31 -04:00
e0355d2a32 Merge remote-tracking branch 'origin/main' 2024-06-28 13:57:42 -04:00
8e0d99ce75 forgot semiclon 2024-06-28 13:57:42 -04:00
2394f51240 Fix syntax error 2024-06-28 13:57:27 -04:00
e83dd27d31 Made logger more platform agnostic. Hopefully supporting Windows properly for the message formatter functions. 2024-06-28 13:54:11 -04:00
623b9efc26 Accreditations 2024-06-28 13:16:25 -04:00
f74b97ac11 const-ref tokens in loop since they aren't modified here 2024-06-28 13:09:26 -04:00
abb9eed60f use std::function signature in argument? 2024-06-28 13:06:43 -04:00
5675480499 Merge remote-tracking branch 'origin/main' 2024-06-28 12:59:15 -04:00
3e414abc8d 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. 2024-06-28 12:59:07 -04:00
69ef213d85 More documentation 2024-06-28 12:57:33 -04:00
22e75476f3 Remove several now-unused functions 2024-06-28 12:30:04 -04:00
e5d8ea5faa Removed unused include "format" 2024-06-28 09:43:31 -04:00
e870007004 Beginning documentation effort. 2024-06-27 15:09:10 -04:00
4138b45404 Implement basic capability to log to specified file. 2024-06-27 14:57:14 -04:00
f0ccdf00c0 consolidate as much as possible 2024-06-26 21:19:04 -04:00
Redacted
9f0ccef302 Refactor to abstract platform-specific colorcodes away from the higher-level API 2024-06-26 19:17:12 -04:00
c5d3f7dc82 Updated README.md 2024-06-19 11:26:53 -04:00
aaa7318672 Merge remote-tracking branch 'origin/main' 2024-06-19 11:25:01 -04:00
95f2c761c7 Rewritten most functionality to be more generic and flexible. Cleaned up code as well. Implemented formatter system for easier wrapping. Made it easier for custom loggers to be built from jlog generics and primitives. Removed unused ultra short and short loggers to remove clutter. 2024-06-19 11:23:57 -04:00
Redacted
16d1eaa1ee Update README.md 2024-06-18 21:49:22 -04:00
8 changed files with 465 additions and 924 deletions

View File

@@ -14,16 +14,11 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(cmake/CPM.cmake)
file(GLOB_RECURSE jlog_HEADERS "include/jlog/*.h" "include/jlog/*.hpp")
if(UNIX AND NOT APPLE)
file(GLOB_RECURSE jlog_SRC "src/jlog/linux/*.c" "src/jlog/linux/*.cpp")
endif()
file(GLOB_RECURSE jlog_SRC "src/jlog/*.cpp")
if(WIN32)
file(GLOB_RECURSE jlog_SRC "src/jlog/windows/*.c" "src/jlog/windows/*.cpp")
endif()
include_directories("include")
CPMAddPackage(
@@ -31,7 +26,10 @@ CPMAddPackage(
URL https://git.redacted.cc/josh/Event/archive/Release-6.zip
)
include_directories(${Event_SOURCE_DIR}/include)
CPMAddPackage(
NAME mcolor
URL https://git.redacted.cc/maxine/mcolor/archive/Prerelease-1.zip
)
if (UNIX)
add_library(jlog SHARED ${jlog_SRC})
@@ -39,8 +37,11 @@ endif()
if (WIN32)
add_library(jlog STATIC ${jlog_SRC})
target_compile_options(jlog PRIVATE /wd4005)
endif()
target_include_directories(jlog PUBLIC ${Event_SOURCE_DIR}/include ${mcolor_SOURCE_DIR}/include)
set_target_properties(jlog PROPERTIES LINKER_LANGUAGE CXX)
install(TARGETS ${PROJECT_NAME} DESTINATION lib/${PROJECT_NAME})
@@ -48,7 +49,7 @@ install(FILES ${jlog_HEADERS} DESTINATION include/${PROJECT_NAME})
#add_subdirectory(tests)
target_link_libraries(jlog PRIVATE Event)
target_link_libraries(jlog PRIVATE Event mcolor)
add_executable(LoggerDemo main.cpp)
target_link_libraries(LoggerDemo PUBLIC ${PROJECT_NAME})
target_link_libraries(LoggerDemo PUBLIC jlog)

View File

@@ -11,6 +11,7 @@ jlog is a C++ library for logging to file, console, and event callbacks.
* Color Output
* Logs to file AND console!
* Idiomatic Event callback for hooking into your game engine gui!
* GCC and MSVC support
## Installation
@@ -19,7 +20,7 @@ Include this repository as a CPM dependency, and link against the library.
```cmake
CPMAddPackage(
NAME jlog
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-2.zip
URL https://git.redacted.cc/josh/jlog/archive/Prerelease-7.zip
)
# ...
include_directories(${jlog_SOURCE_DIR}/include)
@@ -29,6 +30,7 @@ target_link_libraries(YourProgram ... jlog)
```
# Usage
Using jlog is straightforward:
```cpp
@@ -58,12 +60,10 @@ int main() {
* Custom Contexts: Allow users to specify custom logging contexts for better organization.
* Disable & sort by context (other categories?)
* Custom Formats: Add support for custom log message formats.
* Documentation
* Thread Safety
* Memory Safety
* Stream Support
* Identify File, Line, Function name via macros (I hate macros!!!)
## License

View File

@@ -1,139 +0,0 @@
#pragma once
#include <string>
#include <cstdint>
#include <format>
#ifdef _WIN32
#include <windows.h>
#endif
#ifdef _WIN32
namespace jlog::ansi_escape_codes
{
const WORD FG_BLACK = 0;
const WORD FG_RED = FOREGROUND_RED;
const WORD FG_GREEN = FOREGROUND_GREEN;
const WORD FG_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN;
const WORD FG_BLUE = FOREGROUND_BLUE;
const WORD FG_MAGENTA = FOREGROUND_RED | FOREGROUND_BLUE;
const WORD FG_CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD FG_WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
const WORD FG_DEFAULT = FG_WHITE;
const WORD FG_BRIGHT_BLACK = 0 | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_RED = FOREGROUND_RED | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_GREEN = FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_YELLOW = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_BLUE = FOREGROUND_BLUE | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_MAGENTA = FOREGROUND_RED | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_CYAN = FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
const WORD FG_BRIGHT_WHITE = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_INTENSITY;
inline void SetConsoleTextColor(WORD color) {
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), color);
}
}
#else
namespace jlog::ansi_escape_codes
{
inline static const std::string CURSOR_HOME = "\033[H";
inline static const std::string ERASE_SCREEN_AFTER_CURSOR = "\033[0J";
inline static const std::string ERASE_SCREEN_BEFORE_CURSOR = "\033[1J";
inline static const std::string ERASE_SCREEN_ALL = "\033[2J";
inline static const std::string ERASE_LINE_AFTER_CURSOR = "\033[0K";
inline static const std::string ERASE_LINE_BEFORE_CURSOR = "\033[1K";
inline static const std::string ERASE_LINE_ALL = "\033[2K";
inline static const std::string RESET = "\033[0m";
inline static const std::string BOLD = "\033[1m";
inline static const std::string DIM = "\033[2m";
/// These are some examples of private modes, which are not defined by the spec, but are implemented in most terminals.
/// @see xterm control sequences for a more in-depth list.
/// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html
/// @note While these modes may be supported by most terminals, some may not work in multiplexers like tmux.
inline static const std::string CURSOR_INVISIBLE = "\033[?25l";
inline static const std::string CURSOR_VISIBLE = "\033[?25h";
inline static const std::string RESTORE_SCREEN = "\033[?47l";
inline static const std::string SAVE_SCREEN = "\033[?47h";
inline static const std::string FG_BLACK = "\033[30m";
inline static const std::string FG_RED = "\033[31m";
inline static const std::string FG_GREEN = "\033[32m";
inline static const std::string FG_YELLOW = "\033[33m";
inline static const std::string FG_BLUE = "\033[34m";
inline static const std::string FG_MAGENTA = "\033[35m";
inline static const std::string FG_CYAN = "\033[36m";
inline static const std::string FG_WHITE = "\033[37m";
inline static const std::string FG_DEFAULT = "\033[39m";
inline static const std::string FG_BRIGHT_BLACK = "\033[90m";
inline static const std::string FG_BRIGHT_RED = "\033[91m";
inline static const std::string FG_BRIGHT_GREEN = "\033[92m";
inline static const std::string FG_BRIGHT_YELLOW = "\033[93m";
inline static const std::string FG_BRIGHT_BLUE = "\033[94m";
inline static const std::string FG_BRIGHT_MAGENTA = "\033[95m";
inline static const std::string FG_BRIGHT_CYAN = "\033[96m";
inline static const std::string FG_BRIGHT_WHITE = "\033[97m";
inline static const std::string BG_BLACK = "\033[40m";
inline static const std::string BG_RED = "\033[41m";
inline static const std::string BG_GREEN = "\033[42m";
inline static const std::string BG_YELLOW = "\033[43m";
inline static const std::string BG_BLUE = "\033[44m";
inline static const std::string BG_MAGENTA = "\033[45m";
inline static const std::string BG_CYAN = "\033[46m";
inline static const std::string BG_WHITE = "\033[47m";
inline static const std::string BG_DEFAULT = "\033[49m";
inline static const std::string BG_BRIGHT_BLACK = "\033[100m";
inline static const std::string BG_BRIGHT_RED = "\033[101m";
inline static const std::string BG_BRIGHT_GREEN = "\033[102m";
inline static const std::string BG_BRIGHT_YELLOW = "\033[103m";
inline static const std::string BG_BRIGHT_BLUE = "\033[104m";
inline static const std::string BG_BRIGHT_MAGENTA = "\033[105m";
inline static const std::string BG_BRIGHT_CYAN = "\033[106m";
inline static const std::string BG_BRIGHT_WHITE = "\033[107m";
inline std::string true_color(uint8_t r, uint8_t g, uint8_t b)
{
return std::format("\033[38;2;{};{};{}m", r, g, b);
}
inline std::string cursor_to(int line, int col)
{
return "";
}
inline std::string cursor_up(int lines)
{
return "";
}
inline std::string cursor_down(int lines)
{
return "";
}
inline std::string cursor_left(int cols)
{
return "";
}
inline std::string cursor_right(int cols)
{
return "";
}
inline std::string cursor_to_col(int col)
{
return "";
}
}
#endif

View File

@@ -3,17 +3,13 @@
/// Developed & Maintained by Josh O'Leary
/// This work is dedicated to the public domain.
/// Special thanks: Maxine Hayes, William J Tomasine II
#pragma once
#include <format>
#include <string>
#include <iostream>
#include <Event.h>
#include <jlog/ansi_escape_codes.hpp>
#ifdef _WIN32
#include <windows.h>
#endif
#include <mcolor.h>
#ifdef __linux__
#define FUNCTION __PRETTY_FUNCTION__
@@ -25,28 +21,21 @@
namespace jlog
{
/*enum class severity : uint8_t
{
none,
verbose,
debug,
warning,
error,
fatal
};
*/
/// Severity levels for logging. Higher numbers filter less messages out.
/// @see LOG_LEVEL macro
enum class severity : uint8_t
{
none,
warning,
error,
fatal,
verbose,
debug,
none, // Show no output
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).
};
inline severity loglevel = severity::debug; // Default log level always debug
inline bool console_logging = true;
struct LogEntry
{
@@ -55,100 +44,200 @@ namespace jlog
std::string timestamp;
};
// TODO: Fully implement logging callback.
static Event<LogEntry> on_log = Event<LogEntry>();
inline std::vector<LogEntry> log_history;
/// 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
{
#ifdef _WIN32
WORD colorCode = ansi_escape_codes::FG_DEFAULT;
#else
std::string colorCode = ansi_escape_codes::RESET;
#endif
mcolor::ansiColors::Colors colorCode = mcolor::ansiColors::Colors::FG_DEFAULT;
std::string content = "";
std::string delimiter = "[]";
};
void set_default_logfile(const std::string& filename);
/// Returns a string timestamp in the format of 'YYYY-MM-DD hh:mm:ss.ms'
std::string get_timestamp();
/// 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 default destination logfile
/// @note Does not append a newline to the message.
/// @see set_default_logfile()
void log_to_file(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);
/// Writes an input string directly to the specified output stream
/// @note Does not append a newline to the message.
void log_to_stream(std::ostream stream, const std::string& message);
void log(std::vector<token> tokens);
/// 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::function<std::string(token)> formatter);
std::string consoleMsgFormatter(token t);
std::string logfileMsgFormatter(token t);
std::string toks2consoleMsg(std::vector<token> tokens);
std::string toks2logfileMsg(std::vector<token> tokens);
void info(const std::string& message);
void sinfo(const std::string& message);
void usinfo(const std::string& message);
/// 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(const std::vector<token>& tokens, const std::string& filename);
void log(const std::vector<token>& tokens);
void ltlog(const std::vector<token>& tokens); // Just for debug purposes
void verbose(const std::string& message);
void sverbose(const std::string& message);
void usverbose(const std::string& message);
#pragma region Generic Formatters
void debug(const std::string& message);
void sdebug(const std::string& message);
void usdebug(const std::string& message);
/// Returns a pseudo-"stacktrace" formatted sequence of tokens.
/// @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 line The source-code line to trace back to. Should be provided by a __LINE__ macro variant.
std::vector<token> trace_format(
const std::string& func,
const std::string& file,
int line);
void warning(const std::string& message);
void swarning(const std::string& message);
void uswarning(const std::string& message);
/// Returns a formatted sequence of tokens given a severity and message.
/// @param severity_name The severity tag to prefix to the message. Could theoretically also be a context.
/// @param message The actual message to include
/// @param severity_cc The colorcode to assign to the severity.
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);
void error(const std::string& message);
void serror(const std::string& message);
void userror(const std::string& message);
/// 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.
/// @param message The actual message to include
/// @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 line The source-code line to trace back to. Should be provided by a __LINE__ macro variant.
/// @param severity_cc The colorcode to assign to the severity.
std::vector<token> log_detailed_format(
const std::string& severity_name,
const std::string& message,
const std::string& func,
const std::string& file,
int line,
const mcolor::ansiColors::Colors& severity_cc = mcolor::ansiColors::Colors::FG_WHITE);
void fatal(const std::string& message);
void sfatal(const std::string& message);
void usfatal(const std::string& message);
/// Returns a token sequence pre-formatted for the INFO log level.
/// @param message The message to send out.
std::vector<token> info_format(const std::string& message);
void info_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void sinfo_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void usinfo_spec(const std::string& message, const std::string& func, const std::string& file, int line);
/// Returns a token sequence pre-formatted for the INFO log 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.
/// @param line The source-code line to trace back to. Should be provided by a __LINE__ macro variant.
std::vector<token> info_detailed_format(
const std::string &message,
const std::string &func,
const std::string &file,
int line);
void verbose_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void sverbose_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void usverbose_spec(const std::string& message, const std::string& func, const std::string& file, int line);
/// Returns a token sequence pre-formatted for the WARNING log level.
/// @param message The message to send out.
std::vector<token> warning_format(const std::string& message);
void debug_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void sdebug_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void usdebug_spec(const std::string& message, const std::string& func, const std::string& file, int line);
/// Returns a token sequence pre-formatted for the WARNING log 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.
/// @param line The source-code line to trace back to. Should be provided by a __LINE__ macro variant.
std::vector<token> warning_detailed_format(
const std::string &message,
const std::string &func,
const std::string &file,
int line);
void warning_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void swarning_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void uswarning_spec(const std::string& message, const std::string& func, const std::string& file, int line);
/// Returns a token sequence pre-formatted for the ERROR log level.
/// @param message The message to send out.
std::vector<token> error_format(const std::string& message);
void error_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void serror_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void userror_spec(const std::string& message, const std::string& func, const std::string& file, int line);
/// Returns a token sequence pre-formatted for the ERROR log 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.
/// @param line The source-code line to trace back to. Should be provided by a __LINE__ macro variant.
std::vector<token> error_detailed_format(
const std::string &message,
const std::string &func,
const std::string &file,
int line);
void fatal_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void sfatal_spec(const std::string& message, const std::string& func, const std::string& file, int line);
void usfatal_spec(const std::string& message, const std::string& func, const std::string& file, int line);
/// Returns a token sequence pre-formatted for the FATAL log 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.
/// @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.
/// @param line The source-code line to trace back to. Should be provided by a __LINE__ macro variant.
std::vector<token> fatal_detailed_format(
const std::string &message,
const std::string &func,
const std::string &file,
int line);
/// Returns a token sequence pre-formatted for the VERBOSE log 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.
/// @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.
/// @param line The source-code line to trace back to. Should be provided by a __LINE__ macro variant.
std::vector<token> verbose_detailed_format(
const std::string &message,
const std::string &func,
const std::string &file,
int line);
/// Returns a token sequence pre-formatted for the DEBUG log 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.
/// @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.
/// @param line The source-code line to trace back to. Should be provided by a __LINE__ macro variant.
std::vector<token> debug_detailed_format(
const std::string &message,
const std::string &func,
const std::string &file,
int line);
#pragma endregion
}
#define INFO(i) if (jlog::loglevel >= jlog::severity::none) { jlog::info_spec(i, FUNCTION, __FILE__, __LINE__); };
#define SINFO(i) if (jlog::loglevel >= jlog::severity::none) { jlog::sinfo_spec(i, FUNCTION, __FILE__, __LINE__); };
#define USINFO(i) if (jlog::loglevel >= jlog::severity::none) { jlog::usinfo_spec(i, FUNCTION, __FILE__, __LINE__); };
#define INFO(i) if (jlog::loglevel >= jlog::severity::none) { jlog::log(jlog::info_detailed_format(i, FUNCTION, __FILE__, __LINE__)); }
#define VERBOSE(i) if (jlog::loglevel >= jlog::severity::verbose) { jlog::verbose_spec(i, FUNCTION, __FILE__, __LINE__); };
#define SVERBOSE(i) if (jlog::loglevel >= jlog::severity::verbose) { jlog::sverbose_spec(i, FUNCTION, __FILE__, __LINE__); };
#define USVERBOSE(i) if (jlog::loglevel >= jlog::severity::verbose) { jlog::usverbose_spec(i, FUNCTION, __FILE__, __LINE__); };
#define VERBOSE(i) if (jlog::loglevel >= jlog::severity::verbose) { jlog::log(jlog::verbose_detailed_format(i, FUNCTION, __FILE__, __LINE__)); }
#define DEBUG(i) if (jlog::loglevel >= jlog::severity::debug) { jlog::debug_spec(i, FUNCTION, __FILE__, __LINE__); };
#define SDEBUG(i) if (jlog::loglevel >= jlog::severity::debug) { jlog::sdebug_spec(i, FUNCTION, __FILE__, __LINE__); };
#define USDEBUG(i) if (jlog::loglevel >= jlog::severity::debug) { jlog::usdebug_spec(i, FUNCTION, __FILE__, __LINE__); };
#define DEBUG(i) if (jlog::loglevel >= jlog::severity::debug) { jlog::log(jlog::debug_detailed_format(i, FUNCTION, __FILE__, __LINE__)); }
#define WARNING(i) if (jlog::loglevel >= jlog::severity::warning) { jlog::warning_spec(i, FUNCTION, __FILE__, __LINE__); };
#define SWARNING(i) if (jlog::loglevel >= jlog::severity::warning) { jlog::swarning_spec(i, FUNCTION, __FILE__, __LINE__); };
#define USWARNING(i) if (jlog::loglevel >= jlog::severity::warning) { jlog::uswarning_spec(i, FUNCTION, __FILE__, __LINE__); };
#define WARNING(i) if (jlog::loglevel >= jlog::severity::warning) { jlog::log(jlog::warning_detailed_format(i, FUNCTION, __FILE__, __LINE__)); }
#define ERROR(i) if (jlog::loglevel >= jlog::severity::error) { jlog::error_spec(i, FUNCTION, __FILE__, __LINE__); };
#define SERROR(i) if (jlog::loglevel >= jlog::severity::error) { jlog::serror_spec(i, FUNCTION, __FILE__, __LINE__); };
#define USERROR(i) if (jlog::loglevel >= jlog::severity::error) { jlog::userror_spec(i, FUNCTION, __FILE__, __LINE__); };
#define ERROR(i) if (jlog::loglevel >= jlog::severity::error) { jlog::log(jlog::error_detailed_format(i, FUNCTION, __FILE__, __LINE__)); }
#define FATAL(i) if (jlog::loglevel >= jlog::severity::fatal) { jlog::fatal_spec(i, FUNCTION, __FILE__, __LINE__); };
#define SFATAL(i) if (jlog::loglevel >= jlog::severity::fatal) { jlog::sfatal_spec(i, FUNCTION, __FILE__, __LINE__); };
#define USFATAL(i) if (jlog::loglevel >= jlog::severity::fatal) { jlog::usfatal_spec(i, FUNCTION, __FILE__, __LINE__); };
#define FATAL(i) if (jlog::loglevel >= jlog::severity::fatal) { jlog::log(jlog::fatal_detailed_format(i, FUNCTION, __FILE__, __LINE__)); }
//LINFO
#define LOGLEVEL(i) jlog::loglevel = i;
#define CONSOLELOGGING(i) jlog::console_logging = i
#define LOGTEST(i) if (jlog::loglevel >= jlog::severity::none) { jlog::ltlog(jlog::info_detailed_format(i, FUNCTION, __FILE__, __LINE__)); }
#define LOGLEVEL(i) jlog::loglevel = i;

View File

@@ -1,8 +1,29 @@
// Josh's Logger
// Minimal, robust, Modern (C++20) Logging Framework
// Created by Joshua O'Leary @ Redacted Software, June 2024
// Contact: josh@redacted.cc
// Contributors: william@redacted.cc maxi@redacted.cc
// This work is dedicated to the public domain.
#include <jlog/jlog.hpp>
// Writing custom wrappers for jlog is super easy!
/*void coollog(std::vector<jlog::token> tokens) {
std::vector<jlog::token> wtokens;
auto group = jlog::token{.content = "COOLLOGGER"};
wtokens.push_back(group);
wtokens.insert(wtokens.end(), tokens.begin(), tokens.end());
jlog::log(wtokens);
}*/
// We can either define custom logging macros or redefine jlog's builtin macros.
//#define COOLINFO(i) coollog(jlog::info_format(i));
//#define COOLINFOTRACE(i) coollog(jlog::info_detailed_format(i, FUNCTION, __FILE__, __LINE__));
int main()
{
LOGLEVEL(jlog::severity::debug); // <- see jlog::severity for full list of log levels
CONSOLELOGGING(true); // <- Set to true or false to enable/disable logging to console. Useful for release builds of a program.
INFO("This is barely useful information.");
DEBUG("Debugging Information");
@@ -11,22 +32,13 @@ int main()
ERROR("Oops, something went wrong.");
FATAL("Unrecoverable Error!!!");
SINFO("This is even less useful information.");
SDEBUG("Shorter Debugging Information");
SVERBOSE("Yadda Yadda");
SWARNING("Minute miscalculation!");
SERROR("Oops, something went wrong, but the programmer used the short error logger so you're fucked!");
SFATAL("Unrecoverable Error, but the programmer used the short fatal logger so you're even more fucked!!!");
//COOLINFO("This is really cool!!!");
//COOLINFOTRACE("THIS IS EVEN COOLER!!!");
USINFO("This is EVEN less useful information.");
USDEBUG("Ultra compact debugging information.");
USVERBOSE("Isn't this an oxymoron?");
USWARNING("Captain Qwark grade miscalculation!");
USERROR("You're fucked!");
USFATAL("You're super fucked!!!");
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;
///
}
//Windows :(

253
src/jlog/jlog.cpp Normal file
View File

@@ -0,0 +1,253 @@
#include <jlog/jlog.hpp>
#include <fstream>
#include <iostream>
#include <chrono>
#ifdef WIN32
#define NOMINMAX
#include <windows.h>
#endif
namespace jlog
{
static std::string default_logfile = "latest.log";
void set_default_logfile(const std::string& filename)
{
default_logfile = filename;
}
std::string get_timestamp()
{
using namespace std::chrono;
auto const timestamp = current_zone()->to_local(system_clock::now());
auto dp = floor<days>(timestamp);
year_month_day ymd{floor<days>(timestamp)};
hh_mm_ss time{floor<milliseconds>(timestamp - dp)};
auto y = ymd.year();
auto m = ymd.month();
auto d = ymd.day();
auto h = time.hours();
auto M = time.minutes();
auto s = time.seconds();
auto ms = time.subseconds();
return std::format("{}-{}-{} {}:{}:{}.{}", y, m, d, h.count(), M.count(), s.count(), ms.count());
}
void log_to_console(const std::string& message)
{
#ifdef WIN32
mcolor::windowsSaneify();
#endif
std::cout << message;
}
void log_to_file(const std::string& message)
{
std::ofstream latest_log(default_logfile, std::ios_base::app);
latest_log << message;
latest_log.close();
}
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();
}
void log_to_stream(std::ostream stream, const std::string& message)
{
stream << message;
}
std::string toks2msg(std::vector<token> tokens, std::function<std::string(token)> formatter)
{
std::string msg;
for (const 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);
}
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));
}
return std::format("{}{}{} ", mcolor::toEscapeCode(t.colorCode), t.content, mcolor::toEscapeCode(mcolor::ansiColors::Colors::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(const std::vector<token>& tokens, const std::string& filename)
{
if (console_logging) {
log_to_console(toks2consoleMsg(tokens));
log_to_console("\n");
}
log_to_file(filename, toks2logfileMsg(tokens));
log_to_file(filename, "\n");
}
void log(const std::vector<token>& tokens)
{
log(tokens, default_logfile);
}
// Mainly for debug purposes
void ltlog(const std::vector<token>& tokens)
{
std::vector<token> wtokens;
//auto head = token{.colorCode = ansi_escape_codes::true_color(color_codes::BG_RED.rgb.r, color_codes::BG_RED.rgb.g, color_codes::BG_RED.rgb.b), .content = "GAS"};
//wtokens.push_back(head);
wtokens.insert(wtokens.end(), tokens.begin(), tokens.end());
log(wtokens);
log_to_file("logtest.log", toks2logfileMsg(wtokens) + "\n");
}
std::vector<token> trace_format(
const std::string& func,
const std::string& file,
int line)
{
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
return {trace, filedata};
}
std::vector<token> info_format(const std::string& message)
{
return log_format("INFO", message);
}
std::vector<token> warning_format(const std::string& message)
{
return log_format("INFO", message, mcolor::ansiColors::Colors::FG_YELLOW);
}
std::vector<token> error_format(const std::string& message)
{
return log_format("ERROR", message, mcolor::ansiColors::Colors::FG_RED);
}
std::vector<token> fatal_format(const std::string& message)
{
return log_format("FATAL", message, mcolor::ansiColors::Colors::FG_BRIGHT_RED);
}
std::vector<token> verbose_format(const std::string& message)
{
return log_format("VERBOSE", message, mcolor::ansiColors::Colors::FG_CYAN);
}
std::vector<token> debug_format(const std::string& message)
{
return log_format("DEBUG", message, mcolor::ansiColors::Colors::FG_GREEN);
}
std::vector<token> debug_detailed_format(
const std::string& message,
const std::string& func,
const std::string& file,
int line)
{
return log_detailed_format("DEBUG", message, func, file, line, mcolor::ansiColors::Colors::FG_GREEN);
}
std::vector<token> info_detailed_format(
const std::string& message,
const std::string& func,
const std::string& file,
int line)
{
return log_detailed_format("INFO", message, func, file, line);
}
std::vector<token> warning_detailed_format(
const std::string& message,
const std::string& func,
const std::string& file,
int line)
{
return log_detailed_format("WARNING", message, func, file, line, mcolor::ansiColors::Colors::FG_YELLOW);
}
std::vector<token> error_detailed_format(
const std::string& message,
const std::string& func,
const std::string& file,
int line)
{
return log_detailed_format("ERROR", message, func, file, line, mcolor::ansiColors::Colors::FG_RED);
}
std::vector<token> verbose_detailed_format(
const std::string& message,
const std::string& func,
const std::string& file,
int line)
{
return log_detailed_format("VERBOSE", message, func, file, line, mcolor::ansiColors::Colors::FG_CYAN);
}
std::vector<token> fatal_detailed_format(
const std::string& message,
const std::string& func,
const std::string& file,
int line)
{
return log_detailed_format("FATAL", message, func, file, line, mcolor::ansiColors::Colors::FG_BRIGHT_RED);
}
std::vector<token> log_format(
const std::string& severity_name,
const std::string& message,
const mcolor::ansiColors::Colors& severity_cc)
{
auto severity = token{.colorCode = severity_cc, .content = severity_name};
auto content = token{.content = message, .delimiter = ""};
return {severity, content};
}
std::vector<token> log_detailed_format(
const std::string& severity_name,
const std::string& message,
const std::string& func,
const std::string& file,
int line,
const mcolor::ansiColors::Colors& severity_cc)
{
std::vector<token> tokens;
auto timestamp = token{.content = jlog::get_timestamp()};
auto tf_tokens = jlog::trace_format(func, file, line);
auto lf_tokens = jlog::log_format(severity_name, message, severity_cc);
tokens.push_back(timestamp);
tokens.insert(tokens.end(), tf_tokens.begin(), tf_tokens.end());
tokens.insert(tokens.end(), lf_tokens.begin(), lf_tokens.end());
return tokens;
}
}

View File

@@ -1,363 +0,0 @@
#include <source_location>
#include <jlog/ansi_escape_codes.hpp>
#include <jlog/jlog.hpp>
#include <string>
#include <fstream>
namespace jlog {
std::string get_timestamp()
{
using namespace std::chrono;
auto const timestamp = current_zone()->to_local(system_clock::now());
auto dp = floor<days>(timestamp);
year_month_day ymd{floor<days>(timestamp)};
hh_mm_ss time{floor<milliseconds>(timestamp-dp)};
auto y = ymd.year();
auto m = ymd.month();
auto d = ymd.day();
auto h = time.hours();
auto M = time.minutes();
auto s = time.seconds();
auto ms = time.subseconds();
return std::format("{}-{}-{} {}:{}:{}.{}", y, m, d, h.count(), M.count(), s.count(), ms.count());
}
void log_to_console(const std::string &message)
{
std::cout << message;
}
void log_to_file(const std::string &message)
{
std::ofstream latest_log("latest.log", std::ios_base::app);
latest_log << message;
latest_log.close();
}
void log(std::vector<token> tokens) {
for (token t : tokens) {
if (t.delimiter != "") {
log_to_console(std::format("{}{}{}{}{} ", t.colorCode, 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, t.content, ansi_escape_codes::RESET));
log_to_file(t.content + " ");
}
}
log_to_console("\n");
log_to_file("\n");
}
void direct(const std::string &message) { log({{.content = message, .delimiter=""}});}
void info(const std::string &message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({timestamp, severity, content});
}
void sinfo(const std::string &message) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({severity, content});
}
void usinfo(const std::string &message) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({severity, content});
}
void verbose(const std::string &message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({timestamp, severity, content});
//log(ansi_escape_codes::FG_CYAN, "VERBOSE", message);
}
void sverbose(const std::string &message) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({severity, content});
//log(ansi_escape_codes::FG_CYAN, "VERBOSE", message);
}
void usverbose(const std::string &message) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({severity, content});
//log(ansi_escape_codes::FG_CYAN, "VERBOSE", message);
}
void debug(const std::string &message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({timestamp, severity, content});
}
void sdebug(const std::string &message) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({severity, content});
}
void usdebug(const std::string &message) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({severity, content});
}
void warning(const std::string &message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({timestamp, severity, content});
//log(ansi_escape_codes::FG_YELLOW, "WARNING", message);
}
void uswarning(const std::string &message) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({severity, content});
//log(ansi_escape_codes::FG_YELLOW, "WARNING", message);
}
void error(const std::string &message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({timestamp, severity, content});
//log(ansi_escape_codes::FG_RED, "ERROR", message);
}
void userror(const std::string &message) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = };
log({severity, content});
//log(ansi_escape_codes::FG_RED, "ERROR", message);
}
void fatal(const std::string &message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void usfatal(const std::string &message) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void info_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sinfo_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
//log({severity, content});
}
void usinfo_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = func};
//auto filedata = token{.content = std::format("{}:{}", file, line)};
//log({trace, filedata, severity, content});
log({severity, content});
}
void verbose_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sverbose_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usverbose_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = func};
//auto filedata = token{.content = std::format("{}:{}", file, line)};
log({severity, content});
}
void debug_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sdebug_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usdebug_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = func};
//auto filedata = token{.content = std::format("{}:{}", file, line)};
log({severity, content});
}
void warning_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void swarning_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void uswarning_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = func};
//auto filedata = token{.content = std::format("{}:{}", file, line)};
log({severity, content});
}
void error_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void serror_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void userror_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
//auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = func};
//auto filedata = token{.content = std::format("{}:{}", file, line)};
log({severity, content});
}
void fatal_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sfatal_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usfatal_spec(const std::string &message, const std::string &func, const std::string &file,
int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
//auto trace = token{.content = func};
//auto filedata = token{.content = std::format("{}:{}", file, line)};
log({severity, content});
}
}

View File

@@ -1,312 +0,0 @@
#include <source_location>
#include <jlog/ansi_escape_codes.hpp>
#include <jlog/jlog.hpp>
#include <string>
#include <chrono>
#include <fstream>
#include <iostream>
#include <windows.h>
namespace jlog {
std::string get_timestamp() {
using namespace std::chrono;
auto const timestamp = current_zone()->to_local(system_clock::now());
auto dp = floor<days>(timestamp);
year_month_day ymd{floor<days>(timestamp)};
hh_mm_ss time{floor<milliseconds>(timestamp - dp)};
auto y = ymd.year();
auto m = ymd.month();
auto d = ymd.day();
auto h = time.hours();
auto M = time.minutes();
auto s = time.seconds();
auto ms = time.subseconds();
return std::format("{}-{}-{} {}:{}:{}.{}", y, m, d, h.count(), M.count(), s.count(), ms.count());
}
void log_to_console(const std::string& message) {
std::cout << message;
}
void log_to_file(const std::string& message) {
std::ofstream latest_log("latest.log", std::ios_base::app);
latest_log << message;
latest_log.close();
}
void log(std::vector<token> tokens) {
for (const token& t : tokens) {
if (!t.delimiter.empty()) {
ansi_escape_codes::SetConsoleTextColor(t.colorCode);
log_to_console(std::string(1, t.delimiter[0]) + t.content + std::string(1, t.delimiter[1]) + " ");
log_to_file(std::string(1, t.delimiter[0]) + t.content + std::string(1, t.delimiter[1]) + " ");
} else {
ansi_escape_codes::SetConsoleTextColor(t.colorCode);
log_to_console(t.content + " ");
log_to_file(t.content + " ");
}
}
ansi_escape_codes::SetConsoleTextColor(ansi_escape_codes::FG_DEFAULT);
log_to_console("\n");
log_to_file("\n");
}
void direct(const std::string& message) {
log({{.content = message, .delimiter = ""}});
}
void info(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void sinfo(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void usinfo(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void verbose(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void sverbose(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void usverbose(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void debug(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void sdebug(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void usdebug(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void warning(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void swarning(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void uswarning(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void error(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void serror(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void userror(const std::string& message) {
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void fatal(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({timestamp, severity, content});
}
void sfatal(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void usfatal(const std::string& message) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void info_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sinfo_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usinfo_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_WHITE, .content = "INFO"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void verbose_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sverbose_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usverbose_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_CYAN, .content = "VERBOSE"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void debug_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sdebug_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usdebug_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_GREEN, .content = "DEBUG"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void warning_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void swarning_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void uswarning_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_YELLOW, .content = "WARNING"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void error_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void serror_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void userror_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_RED, .content = "ERROR"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
void fatal_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({timestamp, trace, filedata, severity, content});
}
void sfatal_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto timestamp = token{.content = get_timestamp()};
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
auto trace = token{.content = func};
auto filedata = token{.content = std::format("{}:{}", file, line)};
log({trace, filedata, severity, content});
}
void usfatal_spec(const std::string& message, const std::string& func, const std::string& file, int line) {
auto severity = token{.colorCode = ansi_escape_codes::FG_BRIGHT_RED, .content = "FATAL"};
auto content = token{.content = message, .delimiter = ""};
log({severity, content});
}
}