Initial Commit
This commit is contained in:
106
include/ansi_escape_codes.hpp
Normal file
106
include/ansi_escape_codes.hpp
Normal file
@@ -0,0 +1,106 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <format>
|
||||
|
||||
#define ESC "\033["
|
||||
|
||||
namespace jlog::ansi_escape_codes
|
||||
{
|
||||
static const std::string CURSOR_HOME = "\033[H";
|
||||
|
||||
static const std::string ERASE_SCREEN_AFTER_CURSOR = "\033[0J";
|
||||
static const std::string ERASE_SCREEN_BEFORE_CURSOR = "\033[1J";
|
||||
static const std::string ERASE_SCREEN_ALL = "\033[2J";
|
||||
static const std::string ERASE_LINE_AFTER_CURSOR = "\033[0K";
|
||||
static const std::string ERASE_LINE_BEFORE_CURSOR = "\033[1K";
|
||||
static const std::string ERASE_LINE_ALL = "\033[2K";
|
||||
|
||||
static const std::string RESET = "\033[0m";
|
||||
static const std::string BOLD = "\033[1m";
|
||||
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.
|
||||
|
||||
static const std::string CURSOR_INVISIBLE = "\033[?25l";
|
||||
static const std::string CURSOR_VISIBLE = "\033[?25h";
|
||||
static const std::string RESTORE_SCREEN = "\033[?47l";
|
||||
static const std::string SAVE_SCREEN = "\033[?47h";
|
||||
|
||||
|
||||
static const std::string FG_BLACK = "\033[30m";
|
||||
static const std::string FG_RED = "\033[31m";
|
||||
static const std::string FG_GREEN = "\033[32m";
|
||||
static const std::string FG_YELLOW = "\033[33m";
|
||||
static const std::string FG_BLUE = "\033[34m";
|
||||
static const std::string FG_MAGENTA = "\033[35m";
|
||||
static const std::string FG_CYAN = "\033[36m";
|
||||
static const std::string FG_WHITE = "\033[37m";
|
||||
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 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_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";
|
||||
|
||||
std::string true_color(uint8_t r, uint8_t g, uint8_t b)
|
||||
{
|
||||
return std::format("\033[38;2;{};{};{}m", r, g, b);
|
||||
}
|
||||
|
||||
std::string cursor_to(int line, int col)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string cursor_up(int lines)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string cursor_down(int lines)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string cursor_left(int cols)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string cursor_right(int cols)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
std::string cursor_to_col(int col)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
109
include/jlog.hpp
Normal file
109
include/jlog.hpp
Normal file
@@ -0,0 +1,109 @@
|
||||
#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
|
||||
};
|
||||
|
||||
|
||||
|
||||
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;
|
||||
std::string content;
|
||||
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& 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); }
|
||||
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 init() {}
|
||||
|
||||
static void flush() {}
|
||||
|
||||
|
||||
}
|
Reference in New Issue
Block a user