This commit is contained in:
2024-06-11 20:16:26 -04:00
parent e719b700e4
commit 57b84b0672
2 changed files with 76 additions and 68 deletions

View File

@@ -19,45 +19,6 @@ namespace jlog
fatal
};
static 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());
}
static bool lib_initialized = false;
static std::vector<std::shared_ptr<std::ostream>> outputs;
static void add_output(std::ostream* output)
{
if (!lib_initialized)
throw std::runtime_error("jlog::init() ya dumbfuck!!!");
outputs.push_back(std::shared_ptr<std::ostream>(output));
}
static void add_output(std::shared_ptr<std::ofstream> output) {
outputs.push_back(std::move(output));
}
struct LogEntry
{
severity level;
@@ -65,45 +26,25 @@ namespace jlog
std::string timestamp;
};
static void log_to_console(const std::string& message) {
std::cout << message << std::endl;
}
static void log_to_file(const std::string& message) {
std::ofstream latest_log("latest.log");
latest_log << message << std::endl;
}
static Event<LogEntry> on_log;
static std::vector<LogEntry> log_history;
static 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()});
}
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 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()});
}
static void verbose(const std::string& message);
static void debug (const std::string& message);
void direct(const std::string& message) { log(message);};
void info(const std::string& message) {log(ansi_escape_codes::FG_WHITE, "INFO", message); }
static void verbose(const std::string& message) { log(ansi_escape_codes::FG_CYAN, "VERBOSE", message); }
static void debug (const std::string& message) { log(ansi_escape_codes::FG_GREEN, "DEBUG", message); }
static void warning(const std::string& message) { log(ansi_escape_codes::FG_YELLOW, "WARNING", message); }
static void error (const std::string& message) { log(ansi_escape_codes::FG_BRIGHT_RED, "ERROR", message); }
static void fatal (const std::string& message) { log(ansi_escape_codes::FG_RED, "FATAL", message); }
static void warning(const std::string& message);
static void error (const std::string& message);
static void init() {}
static void flush() {}
static void fatal (const std::string& message);
}

View File

@@ -1,3 +1,70 @@
//
// 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); }
}