@@ -12,12 +12,13 @@ constexpr static inline uint8_t _newline_buffer[]{ '\n' };
1212constexpr static inline Span<const uint8_t > _newline_span{ _newline_buffer };
1313MAKE_FANCY_ENUM (LogLevel, uint8_t , Critical = 5 , Error = 4 , Warning = 3 , Info = 2 , Debug = 1 , Trace = 0 );
1414MAKE_FANCY_ENUM (LoggingError, uint8_t , FormatError, OutputError, OpenStreamError, LoggerNotFoundError);
15- constexpr StringView DefaultLogFormat = " %c[%n|%l - %u][%t]%r %m" ;
15+ constexpr StringView DefaultLogFormat = " %c[%n|%l - %u][%t]%r %m" ;
1616constexpr StringView DefaultLogFileFormat = " [%n|%l - %u][%t] %m" ;
1717class LoggingFormat {
18- enum class LoggingFormatSpecifier {
18+ enum class LoggingFormatSpecifier : uint8_t {
1919 Message,
2020 LogLevel,
21+ LogLevelShort,
2122 Timestamp,
2223 ThreadId,
2324 DefaultColor,
@@ -32,21 +33,61 @@ class LoggingFormat {
3233 static Result<LoggingFormat, LoggingError> from_string (StringView format);
3334 String format_message (StringView message, LogLevel level, StringView logger_name) const ;
3435};
36+ #ifdef __INTELLISENSE__
37+ constexpr bool is_intellisense = true ;
38+ #else
39+ constexpr bool is_intellisense = false ;
40+ #endif
41+ template <typename ... Args>
42+ using LoggerStringParam = ConditionalT<is_intellisense, StringView, FormatString<sizeof ...(Args)>>;
43+
44+ template <typename T>
45+ concept LoggingPrintable = Printable<RemoveReferenceT<T>>;
3546class LoggingBackend {
3647 String m_name;
3748 LogLevel m_level;
3849 LoggingFormat m_format;
3950 friend class LoggingBackendTs ;
51+ friend class Logger ;
52+ public:
53+ using LogResult = DiscardResult<LoggingError>;
4054 protected:
4155 String format_message (StringView message, LogLevel level) const ;
42- protected:
4356 LoggingBackend (String name, LogLevel level, LoggingFormat format) :
4457 m_name{ name }, m_level{ level }, m_format{ format } {}
58+ virtual LogResult log (LogLevel level, StringView message) = 0;
4559 public:
46- using LogResult = DiscardResult<LoggingError>;
4760 constexpr bool should_log (LogLevel level) const { return level >= m_level; }
4861 const String& name () const { return m_name; }
49- virtual LogResult log (LogLevel level, StringView message) = 0;
62+ template <LoggingPrintable... Args>
63+ LogResult log (LogLevel level, LoggerStringParam<Args...> str, Args&&... args) {
64+ auto formatted_string = Printer::format (move (str), Forward<Args>(args)...);
65+ return log (level, formatted_string);
66+ }
67+ template <LoggingPrintable... Args>
68+ LogResult log_critical (LoggerStringParam<Args...> str, Args&&... args) {
69+ return log (LogLevel::Critical, move (str), Forward<Args>(args)...);
70+ }
71+ template <LoggingPrintable... Args>
72+ LogResult log_error (LoggerStringParam<Args...> str, Args&&... args) {
73+ return log (LogLevel::Error, move (str), Forward<Args>(args)...);
74+ }
75+ template <LoggingPrintable... Args>
76+ LogResult log_warning (LoggerStringParam<Args...> str, Args&&... args) {
77+ return log (LogLevel::Warning, move (str), Forward<Args>(args)...);
78+ }
79+ template <LoggingPrintable... Args>
80+ LogResult log_info (LoggerStringParam<Args...> str, Args&&... args) {
81+ return log (LogLevel::Info, move (str), Forward<Args>(args)...);
82+ }
83+ template <LoggingPrintable... Args>
84+ LogResult log_debug (LoggerStringParam<Args...> str, Args&&... args) {
85+ return log (LogLevel::Debug, move (str), Forward<Args>(args)...);
86+ }
87+ template <LoggingPrintable... Args>
88+ LogResult log_trace (LoggerStringParam<Args...> str, Args&&... args) {
89+ return log (LogLevel::Trace, move (str), Forward<Args>(args)...);
90+ }
5091 virtual bool supports_color () { return false ; }
5192 virtual ~LoggingBackend () = default ;
5293};
@@ -154,8 +195,9 @@ class FileLogger : public StreamLogger<FileStream> {
154195};
155196class BufferedFileLogger : public StreamLogger <BufferedFileStream> {
156197 String m_file_path;
157- BufferedFileLogger (String name, LogLevel level, LoggingFormat format, String file_path, BufferedFileStream&& stream) :
158- StreamLogger<BufferedFileStream>{ name, level, format, move (stream) }, m_file_path{ move (file_path) } {}
198+ BufferedFileLogger (
199+ String name, LogLevel level, LoggingFormat format, String file_path, BufferedFileStream&& stream
200+ ) : StreamLogger<BufferedFileStream>{ name, level, format, move (stream) }, m_file_path{ move (file_path) } {}
159201 public:
160202 static Result<SharedPtr<LoggingBackend>, LoggingError>
161203 create (String name, LogLevel level, String file_path, StringView format = DefaultLogFileFormat) {
@@ -179,12 +221,12 @@ class StringLogger : public StreamLogger<StringStream> {
179221 };
180222 }
181223 String output () const { return m_stream->str (); }
224+ auto lines () const { return StringViewStream{ *m_stream }.lines_view (); }
182225};
183226class FileLoggerTs : public StreamLoggerTs <FileStream> {
184227 String m_file_path;
185228 FileLoggerTs (String name, LogLevel level, LoggingFormat format, String file_path, FileStream&& stream) :
186- StreamLoggerTs<FileStream>{ name, level, format, move (stream) }, m_file_path{ move (file_path) } {
187- }
229+ StreamLoggerTs<FileStream>{ name, level, format, move (stream) }, m_file_path{ move (file_path) } {}
188230 public:
189231 static Result<SharedPtr<LoggingBackend>, LoggingError>
190232 create (String name, LogLevel level, String file_path, StringView format = DefaultLogFileFormat) {
@@ -247,30 +289,23 @@ class Logger {
247289 }
248290 public:
249291 static void register_logger (SharedPtr<LoggingBackend> backend) {
250- auto name_copy = backend->name ();
292+ String name_copy = backend->name ();
251293 store ().add_backend (move (name_copy), move (backend));
252294 }
253295 static LoggingStorage::ResultType get_named_logger (StringView name);
254296 static LoggingStorage::ResultType get_default_logger ();
255- #ifdef __INTELLISENSE__
256- #define LOGGER_STRING_PARAM_TYPE StringView
257- #else
258- #define LOGGER_STRING_PARAM_TYPE FormatString<sizeof ...(Args)>
259- #endif
260- template <typename ... Args>
261- requires (... && Printable<RemoveReferenceT<Args>>)
297+ template <LoggingPrintable... Args>
262298 static LoggingBackend::LogResult
263- log (StringView logger_name, LogLevel level, LOGGER_STRING_PARAM_TYPE str, Args&&... args) {
299+ log (StringView logger_name, LogLevel level, LoggerStringParam<Args...> str, Args&&... args) {
264300 auto formatted_string = Printer::format (move (str), Forward<Args>(args)...);
265301 if (auto it = store ().get (logger_name); it.is_ok ()) {
266302 auto backend = it.to_ok ();
267303 TRY (backend->log (level, formatted_string));
268304 }
269305 return {};
270306 }
271- template <typename ... Args>
272- requires (... && Printable<RemoveReferenceT<Args>>)
273- static LoggingBackend::LogResult log (LogLevel level, LOGGER_STRING_PARAM_TYPE str, Args&&... args) {
307+ template <LoggingPrintable... Args>
308+ static LoggingBackend::LogResult log (LogLevel level, LoggerStringParam<Args...> str, Args&&... args) {
274309 auto formatted_string = Printer::format (move (str), Forward<Args>(args)...);
275310 auto & sync_store = Logger::store ();
276311 return sync_store.m_store .with_lock (
@@ -280,5 +315,29 @@ class Logger {
280315 }
281316 );
282317 }
318+ template <LoggingPrintable... Args>
319+ static LoggingBackend::LogResult log_critical (LoggerStringParam<Args...> str, Args&&... args) {
320+ return log (LogLevel::Critical, move (str), Forward<Args>(args)...);
321+ }
322+ template <LoggingPrintable... Args>
323+ static LoggingBackend::LogResult log_error (LoggerStringParam<Args...> str, Args&&... args) {
324+ return log (LogLevel::Error, move (str), Forward<Args>(args)...);
325+ }
326+ template <LoggingPrintable... Args>
327+ static LoggingBackend::LogResult log_warning (LoggerStringParam<Args...> str, Args&&... args) {
328+ return log (LogLevel::Warning, move (str), Forward<Args>(args)...);
329+ }
330+ template <LoggingPrintable... Args>
331+ static LoggingBackend::LogResult log_info (LoggerStringParam<Args...> str, Args&&... args) {
332+ return log (LogLevel::Info, move (str), Forward<Args>(args)...);
333+ }
334+ template <LoggingPrintable... Args>
335+ static LoggingBackend::LogResult log_debug (LoggerStringParam<Args...> str, Args&&... args) {
336+ return log (LogLevel::Debug, move (str), Forward<Args>(args)...);
337+ }
338+ template <LoggingPrintable... Args>
339+ static LoggingBackend::LogResult log_trace (LoggerStringParam<Args...> str, Args&&... args) {
340+ return log (LogLevel::Trace, move (str), Forward<Args>(args)...);
341+ }
283342};
284343} // namespace ARLib
0 commit comments