some more updates and stuff
This commit is contained in:
parent
96c6e79780
commit
203d56e06b
17 changed files with 231 additions and 255 deletions
|
@ -1,7 +1,9 @@
|
|||
#pragma once
|
||||
|
||||
// probably stupid but it fixes the issue with windows.h defining ERROR
|
||||
#ifdef _WIN32
|
||||
#undef ERROR
|
||||
#endif
|
||||
|
||||
#include <chrono>
|
||||
#include <filesystem>
|
||||
#include <format>
|
||||
|
@ -11,18 +13,15 @@
|
|||
|
||||
#include "types.h"
|
||||
|
||||
#define fn auto // Rust-style function shorthand
|
||||
#define fn auto
|
||||
|
||||
// Terminal color implementation to replace fmt::color
|
||||
namespace term {
|
||||
// Text styles
|
||||
enum class Emphasis : u8 { none = 0, bold = 1, italic = 2 };
|
||||
|
||||
constexpr fn operator|(Emphasis emphA, Emphasis emphB)->Emphasis {
|
||||
return static_cast<Emphasis>(static_cast<int>(emphA) | static_cast<int>(emphB));
|
||||
}
|
||||
|
||||
// Terminal colors
|
||||
enum class Color : u8 {
|
||||
black = 30,
|
||||
red = 31,
|
||||
|
@ -42,70 +41,57 @@ namespace term {
|
|||
bright_white = 97
|
||||
};
|
||||
|
||||
// Style wrapper for foreground color
|
||||
struct FgColor {
|
||||
Color col;
|
||||
|
||||
constexpr explicit FgColor(Color color) : col(color) {}
|
||||
|
||||
[[nodiscard]] fn ansiCode() const -> std::string { return std::format("\033[{}m", static_cast<int>(col)); }
|
||||
[[nodiscard]] fn ansiCode() const -> String { return std::format("\033[{}m", static_cast<int>(col)); }
|
||||
};
|
||||
|
||||
// Create a foreground color modifier
|
||||
constexpr fn Fg(Color color) -> FgColor { return FgColor(color); }
|
||||
|
||||
// Combined style (emphasis + color)
|
||||
struct Style {
|
||||
Emphasis emph = Emphasis::none;
|
||||
FgColor fg_col = FgColor(static_cast<Color>(-1)); // Invalid color
|
||||
|
||||
[[nodiscard]] fn ansiCode() const -> std::string {
|
||||
std::string result;
|
||||
[[nodiscard]] fn ansiCode() const -> String {
|
||||
String result;
|
||||
|
||||
if (emph != Emphasis::none) {
|
||||
if ((static_cast<int>(emph) & static_cast<int>(Emphasis::bold)) != 0) {
|
||||
if ((static_cast<int>(emph) & static_cast<int>(Emphasis::bold)) != 0)
|
||||
result += "\033[1m";
|
||||
}
|
||||
if ((static_cast<int>(emph) & static_cast<int>(Emphasis::italic)) != 0) {
|
||||
if ((static_cast<int>(emph) & static_cast<int>(Emphasis::italic)) != 0)
|
||||
result += "\033[3m";
|
||||
}
|
||||
}
|
||||
|
||||
if (static_cast<int>(fg_col.col) != -1) {
|
||||
if (static_cast<int>(fg_col.col) != -1)
|
||||
result += fg_col.ansiCode();
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
// Combine emphasis and color
|
||||
constexpr fn operator|(Emphasis emph, FgColor fgColor)->Style { return { .emph = emph, .fg_col = fgColor }; }
|
||||
constexpr fn operator|(FgColor fgColor, Emphasis emph)->Style { return { .emph = emph, .fg_col = fgColor }; }
|
||||
|
||||
constexpr fn operator|(FgColor fgColor, Emphasis emph)->Style { return emph | fgColor; }
|
||||
|
||||
// Reset all styles
|
||||
constexpr const char* reset = "\033[0m";
|
||||
|
||||
// Print with style
|
||||
template <typename... Args>
|
||||
fn Print(const Style& style, std::format_string<Args...> fmt, Args&&... args) -> void {
|
||||
std::print("{}{}{}", style.ansiCode(), std::format(fmt, std::forward<Args>(args)...), reset);
|
||||
}
|
||||
|
||||
// Print with foreground color only
|
||||
template <typename... Args>
|
||||
fn Print(const FgColor& fgColor, std::format_string<Args...> fmt, Args&&... args) -> void {
|
||||
std::print("{}{}{}", fgColor.ansiCode(), std::format(fmt, std::forward<Args>(args)...), reset);
|
||||
}
|
||||
|
||||
// Print with emphasis only
|
||||
template <typename... Args>
|
||||
fn Print(Emphasis emph, std::format_string<Args...> fmt, Args&&... args) -> void {
|
||||
Print({ .emph = emph }, fmt, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// Print without styling (plain text)
|
||||
template <typename... Args>
|
||||
fn Print(std::format_string<Args...> fmt, Args&&... args) -> void {
|
||||
std::print(fmt, std::forward<Args>(args)...);
|
||||
|
@ -114,15 +100,20 @@ namespace term {
|
|||
|
||||
namespace log_colors {
|
||||
using term::Color;
|
||||
constexpr auto debug = Color::cyan, info = Color::green, warn = Color::yellow, error = Color::red,
|
||||
timestamp = Color::bright_white, file_info = Color::bright_white;
|
||||
|
||||
constexpr Color debug = Color::cyan, info = Color::green, warn = Color::yellow, error = Color::red,
|
||||
timestamp = Color::bright_white, file_info = Color::bright_white;
|
||||
}
|
||||
|
||||
enum class LogLevel : u8 { DEBUG, INFO, WARN, ERROR };
|
||||
|
||||
template <typename... Args>
|
||||
void LogImpl(const LogLevel level, const std::source_location& loc, std::format_string<Args...> fmt, Args&&... args) {
|
||||
const auto now = std::chrono::floor<std::chrono::seconds>(std::chrono::system_clock::now());
|
||||
fn LogImpl(const LogLevel level, const std::source_location& loc, std::format_string<Args...> fmt, Args&&... args)
|
||||
-> void {
|
||||
using namespace std::chrono;
|
||||
|
||||
const time_point<system_clock, duration<long long, std::ratio<1, 1>>> now =
|
||||
std::chrono::floor<std::chrono::seconds>(std::chrono::system_clock::now());
|
||||
|
||||
const auto [color, levelStr] = [&] {
|
||||
switch (level) {
|
||||
|
@ -141,19 +132,21 @@ void LogImpl(const LogLevel level, const std::source_location& loc, std::format_
|
|||
#pragma clang diagnostic pop
|
||||
}
|
||||
}();
|
||||
const string filename = std::filesystem::path(loc.file_name()).lexically_normal().string();
|
||||
|
||||
// Timestamp and level - using std::chrono with std::format
|
||||
term::Print(term::Fg(log_colors::timestamp), "[{:%H:%M:%S}] ", now);
|
||||
term::Print(term::Emphasis::bold | term::Fg(color), "{} ", levelStr);
|
||||
// Message
|
||||
term::Print(fmt, std::forward<Args>(args)...);
|
||||
// File info (debug builds only)
|
||||
const String filename = std::filesystem::path(loc.file_name()).lexically_normal().string();
|
||||
|
||||
using namespace term;
|
||||
|
||||
Print(Fg(log_colors::timestamp), "[{:%H:%M:%S}] ", now);
|
||||
Print(Emphasis::bold | Fg(color), "{} ", levelStr);
|
||||
Print(fmt, std::forward<Args>(args)...);
|
||||
|
||||
#ifndef NDEBUG
|
||||
term::Print(term::Fg(log_colors::file_info), "\n{:>14} ", "╰──");
|
||||
term::Print(term::Emphasis::italic | term::Fg(log_colors::file_info), "{}:{}", filename, loc.line());
|
||||
Print(Fg(log_colors::file_info), "\n{:>14} ", "╰──");
|
||||
Print(Emphasis::italic | Fg(log_colors::file_info), "{}:{}", filename, loc.line());
|
||||
#endif
|
||||
term::Print("\n");
|
||||
|
||||
Print("\n");
|
||||
}
|
||||
|
||||
#pragma clang diagnostic push
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue