Partial work

This commit is contained in:
2024-06-13 00:49:04 -04:00
parent ab6b92f19d
commit 85203b29e4
8 changed files with 189 additions and 157 deletions

View File

@@ -15,12 +15,11 @@ set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/cmake")
include(cmake/CPM.cmake)
file(GLOB_RECURSE jlog_HEADERS "include/*.h" "include/*.hpp")
file(GLOB_RECURSE jlog_SRC "src/*.c" "src/*.cpp")
file(GLOB_RECURSE jlog_HEADERS "include/jlog/*.h" "include/jlog/*.hpp")
file(GLOB_RECURSE jlog_SRC "src/jlog/*.c" "src/jlog/*.cpp")
include_directories("include")
CPMAddPackage(
NAME Event
URL https://git.redacted.cc/josh/Event/archive/Release-6.zip

View File

@@ -2,16 +2,17 @@
jlog is a C++ library for logging to file, console, and event callbacks.
Features
![Static Badge](https://img.shields.io/badge/Lit-Based-%20)
## Features
* Modern (C++20)
* Static Library
* Color Output
* Logs to file AND console!
* Idiomatic Event callback for hooking into your game engine gui!
Installation
## Installation
Include this repository as a CPM dependency, and link against the library.
(TODO: Show the relevant CMake script lines.)
@@ -44,7 +45,7 @@ int main() {
```
TODO
## TODO
* Custom Contexts: Allow users to specify custom logging contexts for better organization.
* Disable & sort by context (other categories?)
@@ -55,10 +56,10 @@ TODO
* Stream Support
* Identify File, Line, Function name via macros (I hate macros!!!)
License
## License
This software is written and developed by Josh O'Leary @ Redacted Software 2024. This work is dedicated to the public domain.
Contributing
## Contributing
Send'em pull requests.

View File

@@ -1,50 +0,0 @@
#pragma once
#include <format>
#include <string>
#include <iostream>
#include <chrono>
#include <Event.h>
#include <fstream>
namespace jlog
{
enum class severity
{
none,
verbose,
debug,
warning,
error,
fatal
};
struct LogEntry
{
severity level;
std::string content;
std::string timestamp;
};
static Event<LogEntry> on_log;
static std::vector<LogEntry> log_history;
static void log(const std::string& message);
static void log(const std::string& contextColorCode, const std::string& context, const std::string& message);
static void direct(const std::string& message);;
static void info(const std::string& message);
static void verbose(const std::string& message);
static void debug (const std::string& message);
static void warning(const std::string& message);
static void error (const std::string& message);
static void fatal (const std::string& message);
}

View File

@@ -43,31 +43,34 @@ namespace jlog::ansi_escape_codes
static const std::string FG_DEFAULT = "\033[39m";
static const std::string FG_BRIGHT_BLACK = "90";
static const std::string FG_BRIGHT_RED = "91";
static const std::string FG_BRIGHT_GREEN = "92";
static const std::string FG_BRIGHT_YELLOW = "93";
static const std::string FG_BRIGHT_BLUE = "94";
static const std::string FG_BRIGHT_MAGENTA = "95";
static const std::string FG_BRIGHT_CYAN = "96";
static const std::string FG_BRIGHT_WHITE = "97";
static const std::string FG_BRIGHT_BLACK = "\033[90m";
static const std::string FG_BRIGHT_RED = "\033[91m";
static const std::string FG_BRIGHT_GREEN = "\033[92m";
static const std::string FG_BRIGHT_YELLOW = "\033[93m";
static const std::string FG_BRIGHT_BLUE = "\033[94m";
static const std::string FG_BRIGHT_MAGENTA = "\033[95m";
static const std::string FG_BRIGHT_CYAN = "\033[96m";
static const std::string FG_BRIGHT_WHITE = "\033[97m";
static const std::string BG_BLACK = "40";
static const std::string BG_RED = "41";
static const std::string BG_GREEN = "42";
static const std::string BG_YELLOW = "43";
static const std::string BG_BLUE = "44";
static const std::string BG_MAGENTA = "45";
static const std::string BG_CYAN = "46";
static const std::string BG_WHITE = "47";
static const std::string BG_DEFAULT = "49";
static const std::string BG_BLACK = "\033[40m";
static const std::string BG_RED = "\033[41m";
static const std::string BG_GREEN = "\033[42m";
static const std::string BG_YELLOW = "\033[43m";
static const std::string BG_BLUE = "\033[44m";
static const std::string BG_MAGENTA = "\033[45m";
static const std::string BG_CYAN = "\033[46m";
static const std::string BG_WHITE = "\033[47m";
static const std::string BG_DEFAULT = "\033[49m";
static const std::string BG_BRIGHT_BLACK = "100";
static const std::string BG_BRIGHT_RED = "101";
static const std::string BG_BRIGHT_GREEN = "102";
static const std::string BG_BRIGHT_YELLOW = "103";
static const std::string BG_BRIGHT_BLUE = "104";
static const std::string BG_BRIGHT_BLACK = "\033[100m";
static const std::string BG_BRIGHT_RED = "\033[101m";
static const std::string BG_BRIGHT_GREEN = "\033[102m";
static const std::string BG_BRIGHT_YELLOW = "\033[103m";
static const std::string BG_BRIGHT_BLUE = "\033[104m";
static const std::string BG_BRIGHT_MAGENTA = "\033[105m";
static const std::string BG_BRIGHT_CYAN = "\033[106m";
static const std::string BG_BRIGHT_WHITE = "\033[107m";
std::string true_color(uint8_t r, uint8_t g, uint8_t b)
{

64
include/jlog/jlog.hpp Normal file
View File

@@ -0,0 +1,64 @@
/// Josh's Logger
/// A Redacted Software Product
/// Developed & Maintained by Josh O'Leary
/// This work is dedicated to the public domain.
#pragma once
#include <format>
#include <string>
#include <iostream>
#include <chrono>
#include <Event.h>
#include <fstream>
namespace jlog
{
enum class severity
{
none,
verbose,
debug,
warning,
error,
fatal
};
struct LogEntry
{
severity level;
std::string content;
std::string timestamp;
};
static Event<LogEntry> on_log = Event<LogEntry>();
std::vector<LogEntry> log_history;
struct token
{
std::string colorCode = "";
std::string content = "";
std::string delimiter = "[]";
};
void log(std::vector<token> tokens);
void log (const std::string& message);
void log (const std::string& contextColorCode, const std::string& context, const std::string& message);
void direct (const std::string& message);
void info (const std::string& message);
void verbose(const std::string& message);
void debug (const std::string& message);
void warning(const std::string& message);
void error (const std::string& message);
void fatal (const std::string& message);
}

View File

@@ -1,15 +1,15 @@
#include "include/ansi_escape_codes.hpp"
#include "include/jlog.hpp"
#include <jlog/ansi_escape_codes.hpp>
#include <jlog/jlog.hpp>
int main()
{
jlog::init();
jlog::log("Test Program");
jlog::debug("Debugging Message 123");
jlog::error("Big problem!");
jlog::verbose("Irrelevant message...");
jlog::warning("Our software suite is only supported on C++20! Get with it grampa.");
jlog::fatal("FUCK BRO");
jlog::flush();
return 0;
///
}

View File

@@ -1,70 +0,0 @@
//
// Created by dawsh on 6/11/24.
//
#include <ansi_escape_codes.hpp>
#include <jlog.hpp>
#include <string>
namespace jlog {
static std::string get_timestamp();
static void log_to_console(const std::string& message);
static void log_to_file(const std::string& message);
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 << std::endl;
}
void log_to_file(const std::string &message) {
std::ofstream latest_log("latest.log");
latest_log << message << std::endl;
}
void log(const std::string& message) {
auto output = std::format("[{}] {}", get_timestamp(), message);
log_to_console(output);
log_to_file(output);
on_log({.level = severity::none, .content = message, .timestamp = get_timestamp()});
}
void log(const std::string &contextColorCode, const std::string &context, const std::string &message) {
log_to_console(std::format("[{}] {}[{}]{} {}", get_timestamp(), contextColorCode, context, ansi_escape_codes::RESET, message));
log_to_file(std::format("[{}] [{}] {}", get_timestamp(), context, message));
on_log({.level = severity::none, .content = message, .timestamp = get_timestamp()});
}
void direct(const std::string &message) { log(message);}
void info(const std::string &message) {log(ansi_escape_codes::FG_WHITE, "INFO", message); }
void verbose(const std::string &message) { log(ansi_escape_codes::FG_CYAN, "VERBOSE", message); }
void debug(const std::string &message) { log(ansi_escape_codes::FG_GREEN, "DEBUG", message); }
void warning(const std::string &message) { log(ansi_escape_codes::FG_YELLOW, "WARNING", message); }
void error(const std::string &message) { log(ansi_escape_codes::FG_BRIGHT_RED, "ERROR", message); }
void fatal(const std::string &message) { log(ansi_escape_codes::FG_RED, "FATAL", message); }
}

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

@@ -0,0 +1,85 @@
#include <jlog/ansi_escape_codes.hpp>
#include <jlog/jlog.hpp>
#include <string>
namespace jlog {
static std::string get_timestamp();
static void log_to_console(const std::string& message);
static void log_to_file(const std::string& message);
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 << std::endl;
}
void log_to_file(const std::string &message)
{
std::ofstream latest_log("latest.log", std::ios_base::app);
latest_log << message << std::endl;
latest_log.close();
}
void log(std::vector<token> tokens) {
for (token t : tokens) {
if (t.delimiter != "")
log(std::format("{}{}{}{}{}", t.colorCode, t.delimiter[0], t.content, t.delimiter[1], ansi_escape_codes::RESET));
else
log(std::format("{}{}{}{}", t.colorCode, t.content, ansi_escape_codes::RESET));
}
}
void log(const std::string& message)
{
auto output = std::format("[{}] [{}:{} {}] {}", get_timestamp(), __FILE__, __LINE__, __PRETTY_FUNCTION__, message);
log_to_console(output);
log_to_file(output);
on_log({.level = severity::none, .content = message, .timestamp = get_timestamp()});
}
void log(const std::string &contextColorCode, const std::string &context, const std::string &message)
{
log_to_console(std::format("[{}] {}[{}]{} {}", get_timestamp(), contextColorCode, context, ansi_escape_codes::RESET, message));
log_to_file(std::format("[{}] [{}] {}", get_timestamp(), context, message));
on_log({.level = severity::none, .content = message, .timestamp = get_timestamp()});
}
void direct(const std::string &message) { log(message);}
void info(const std::string &message) {log(ansi_escape_codes::FG_WHITE, "INFO", message); }
void verbose(const std::string &message) { log(ansi_escape_codes::FG_CYAN, "VERBOSE", message); }
void debug(const std::string &message) { log(ansi_escape_codes::FG_GREEN, "DEBUG", message); }
void warning(const std::string &message) { log(ansi_escape_codes::FG_YELLOW, "WARNING", message); }
void error(const std::string &message) { log(ansi_escape_codes::FG_RED, "ERROR", message); }
void fatal(const std::string &message) {
auto timestamp = token{.content = get_timestamp()};
auto trace = token{.content = };
log(std::vector<token>(timestamp, {}));
}
}