stc
Loading...
Searching...
No Matches
minilog.hpp
Go to the documentation of this file.
1
2#pragma once
3
4#include <chrono>
5#include <format>
6#include "Colour.hpp"
7
14namespace minilog {
15
16enum Level {
17 Debug = 50,
18 Info = 60,
19 Warning = 70,
20 Error = 80,
21 Critical = 90
22};
23
27
28inline Config& config() {
29 // This should be fine:
30 // * https://stackoverflow.com/a/185730
31 // * https://stackoverflow.com/a/189162
32 //
33 // This is also consistent with getStreamConfigIdx in Colour.hpp. I vaguely remember running into this problem
34 // because shouldPrintColour is static, while getStreamConfigIdx is inline.
35 // I really should read up on what inline actually is, because I'm pretty sure I've misunderstood *something*
36 static Config instance;
37
38 return instance;
39}
40
41template <Level level>
42consteval std::string_view levelToString() {
43 if constexpr (level == Level::Debug) {
44 return "debug";
45 } else if constexpr (level == Level::Info) {
46 return "info";
47 } else if constexpr (level == Level::Warning) {
48 return "warning";
49 } else if constexpr (level == Level::Error) {
50 return "error";
51 } else if constexpr (level == Level::Critical) {
52 return "critical";
53 }
54}
55
56inline constexpr std::ostream& operator<<(std::ostream& ss, Level level) {
57 if (level == Level::Debug) {
58 ss << stc::colour::fg<stc::colour::FourBitColour::BRIGHT_BLACK>;
59 } else if (level == Level::Info) {
60 ss << stc::colour::fg<stc::colour::FourBitColour::BLUE>;
61 } else if (level == Level::Warning) {
62 ss << stc::colour::fg<stc::colour::FourBitColour::BRIGHT_YELLOW>;
63 } else if (level == Level::Error) {
64 ss << stc::colour::fg<stc::colour::FourBitColour::BRIGHT_RED>;
65 } else if (level == Level::Critical) {
66 ss << stc::colour::fg<stc::colour::FourBitColour::RED>;
67 }
68 return ss;
69}
70
71template <Level level, class... Args>
72inline constexpr void log(const std::format_string<Args...>& fmt, Args&&... args) {
73 if (config().level > level) {
74 return;
75 }
76
77 // This is good enough:
78 // https://stackoverflow.com/a/26909227
79 //
80 // This isn't going to win any performance awards by any metric, but it's good enough for now.
81 // Not sure if I'll ever revisit performance.
82 std::cout
83 << level
84 << std::format(
85 "{:%T} | {:<8} | {}\n",
86 std::chrono::floor<std::chrono::milliseconds>(
87 std::chrono::system_clock::now()
88 ),
89 levelToString<level>(),
90 std::format<Args...>(fmt, std::forward<Args>(args)...)
91 )
93}
94
95
96template <class... Args>
97inline constexpr void debug(const std::format_string<Args...>& format, Args&&... args) {
98 log<Level::Debug, Args...>(format, std::forward<Args>(args)...);
99}
100
101template <class... Args>
102inline constexpr void info(const std::format_string<Args...>& format, Args&&... args) {
103 log<Level::Info, Args...>(format, std::forward<Args>(args)...);
104}
105
106template <class... Args>
107inline constexpr void warn(const std::format_string<Args...>& format, Args&&... args) {
108 log<Level::Warning, Args...>(format, std::forward<Args>(args)...);
109}
110
111template <class... Args>
112inline constexpr void error(const std::format_string<Args...>& format, Args&&... args) {
113 log<Level::Error, Args...>(format, std::forward<Args>(args)...);
114}
115
116template <class... Args>
117inline constexpr void critical(const std::format_string<Args...>& format, Args&&... args) {
118 log<Level::Critical, Args...>(format, std::forward<Args>(args)...);
119}
120
121}
Module containing a small logger.
Definition minilog.hpp:14
constexpr void critical(const std::format_string< Args... > &format, Args &&... args)
Definition minilog.hpp:117
Level
Definition minilog.hpp:16
@ Info
Definition minilog.hpp:18
@ Error
Definition minilog.hpp:20
@ Warning
Definition minilog.hpp:19
@ Critical
Definition minilog.hpp:21
@ Debug
Definition minilog.hpp:17
constexpr std::ostream & operator<<(std::ostream &ss, Level level)
Definition minilog.hpp:56
constexpr void error(const std::format_string< Args... > &format, Args &&... args)
Definition minilog.hpp:112
Config & config()
Definition minilog.hpp:28
constexpr void log(const std::format_string< Args... > &fmt, Args &&... args)
Definition minilog.hpp:72
consteval std::string_view levelToString()
Definition minilog.hpp:42
constexpr void warn(const std::format_string< Args... > &format, Args &&... args)
Definition minilog.hpp:107
constexpr void info(const std::format_string< Args... > &format, Args &&... args)
Definition minilog.hpp:102
constexpr void debug(const std::format_string< Args... > &format, Args &&... args)
Definition minilog.hpp:97
static constexpr std::basic_ostream< CharT > & reset(std::basic_ostream< CharT > &stream)
Definition Colour.hpp:216
Definition minilog.hpp:24
Level level
Definition minilog.hpp:25