BioCMAMC-ST
logger.hpp
1#ifndef __COMMON_LOGGER_HPP__
2#define __COMMON_LOGGER_HPP__
3
4#include <source_location>
5#include <string>
6#include <string_view>
7#include <type_traits>
8
9namespace IO
10{
11
12 namespace AnsiCode
13 {
14 constexpr auto blue = "\033[34m";
15 constexpr auto red = "\033[31m";
16 constexpr auto reset = "\033[0m";
17 constexpr auto green = "\033[32m";
18 } // namespace AnsiCode
19
20 namespace Unicode
21 {
22 constexpr auto red_circle = "\U0001F534";
23 } // namespace Unicode
24
30 class Logger
31 {
32 public:
33 Logger() = default;
34 Logger(const Logger&) = default;
35 Logger(Logger&&) = default;
36 Logger& operator=(const Logger&) = default;
37 Logger& operator=(Logger&&) = default;
38 virtual ~Logger() = default;
39
47 virtual void debug(std::string_view message) = 0;
48
58 virtual void print(std::string_view prefix, std::string_view message) = 0;
59
68 virtual void alert(std::string_view prefix, std::string_view message) = 0;
69
80 virtual void error(std::string_view message,
81 std::source_location location
82 = std::source_location::current())
83 = 0;
84
92 virtual void raw_log(std::string_view message) = 0;
93
99 virtual void toggle_debug() noexcept = 0;
100
106 virtual void toggle_print() noexcept = 0;
107
113 virtual void toggle_alert() noexcept = 0;
114
120 virtual void toggle_error() noexcept = 0;
121
128 virtual void toggle_all() noexcept = 0;
129 };
130
131 // template <typename... MsgType>
132 // std::string
133 // format(MsgType&&... msgs)
134 // {
135 // std::string result;
136 // (result += ... += std::forward<MsgType>(msgs));
137 // return result;
138 // }
139 //
140 template <typename MsgType>
141 requires(std::is_integral_v<std::remove_cvref_t<MsgType>>
142 || std::is_floating_point_v<std::remove_cvref_t<MsgType>>)
143 void
144 _format(std::string& result, MsgType&& msg)
145 {
146 result += std::to_string(std::forward<MsgType>(msg));
147 }
148
149 template <typename MsgType>
150 requires(!(std::is_integral_v<std::remove_cvref_t<MsgType>>
151 || std::is_floating_point_v<std::remove_cvref_t<MsgType>>))
152 void
153 _format(std::string& result, MsgType&& msg)
154 {
155
156 result += std::forward<MsgType>(msg);
157 }
158
159 template <typename... MsgType>
160 std::string
161 format(MsgType&&... msgs)
162 {
163 std::string result;
164 (_format(result, std::forward<MsgType>(msgs)), ...);
165 return result;
166 }
167
168} // namespace IO
169
170#endif
virtual void error(std::string_view message, std::source_location location=std::source_location::current())=0
Log an error message with optional location details.
Logger & operator=(const Logger &)=default
virtual void toggle_alert() noexcept=0
Toggle the alert logging behavior on or off.
virtual void toggle_debug() noexcept=0
Toggle the debug logging behavior on or off.
virtual void toggle_error() noexcept=0
Toggle the error logging behavior on or off.
virtual void raw_log(std::string_view message)=0
Log a raw message with no additional formatting.
virtual void debug(std::string_view message)=0
Log a debug message.
Logger(const Logger &)=default
virtual ~Logger()=default
virtual void alert(std::string_view prefix, std::string_view message)=0
Log an alert message with a prefix.
Logger()=default
Logger & operator=(Logger &&)=default
virtual void print(std::string_view prefix, std::string_view message)=0
Log a regular print message with a prefix.
virtual void toggle_print() noexcept=0
Toggle the print logging behavior on or off.
virtual void toggle_all() noexcept=0
Toggle all logging types on or off.
Logger(Logger &&)=default
Definition logger.hpp:13
constexpr auto red
Definition logger.hpp:15
constexpr auto green
Definition logger.hpp:17
constexpr auto reset
Definition logger.hpp:16
constexpr auto blue
Definition logger.hpp:14
Definition logger.hpp:21
constexpr auto red_circle
Definition logger.hpp:22
Definition impl_post_process.hpp:11
std::string format(MsgType &&... msgs)
Definition logger.hpp:161
void _format(std::string &result, MsgType &&msg)
Definition logger.hpp:144