Foundation
Loading...
Searching...
No Matches
Logging.hpp
Go to the documentation of this file.
1#pragma once
2#include <fmt/base.h>
3#include <fmt/format.h>
11// NOLINTBEGIN
12template<bool Ansi = true>
13constexpr const char* format_as(LogLevel level)
14{
15 if constexpr (Ansi)
16 {
17 switch (level)
18 {
19 case LogDebug: return "\033[34mD";
20 case LogInfo: return "\033[37mI";
21 case LogWarn: return "\033[33mW";
22 case LogError: return "\033[31mE";
23 }
24 }
25 else
26 {
27 switch (level)
28 {
29 case LogDebug: return "D";
30 case LogInfo: return "I";
31 case LogWarn: return "W";
32 case LogError: return "E";
33 }
34 }
35 return "?";
36}
37
38// NOLINTEND
39
40
41extern void Foundation_LogImpl(LogLevel level, const char* tag, std::string_view formatted);
42template<typename ...Args>
43void Foundation_Log(const char* tag, LogLevel level, fmt::format_string<Args...> format, Args&&... args)
44{
45 constexpr size_t kN = sizeof...(Args);
46 if constexpr (kN > 0)
47 {
48 Foundation_LogImpl(level, tag, fmt::format(format, std::forward<Args>(args)...));
49 } else
50 {
51 Foundation_LogImpl(level, tag, format.str.data());
52 }
53}
54
55#define LOG(TAG, LEVEL, FORMAT, ...) Foundation_Log(#TAG, LEVEL, FORMAT __VA_OPT__(,) __VA_ARGS__);
56
57#define CHECK(expr) if(!(expr)) { \
58 LOG(Core, LogError, "Check failed: " #expr); \
59 throw std::runtime_error( #expr ); \
60}
61
62#define CHECK_MSG(expr, format_str, ...) if(!(expr)) { \
63 LOG(Core, LogError, format_str __VA_OPT__(,) __VA_ARGS__); \
64 throw std::runtime_error( #expr ); \
65}
void Foundation_Log(const char *tag, LogLevel level, fmt::format_string< Args... > format, Args &&... args)
Definition Logging.hpp:43
constexpr const char * format_as(LogLevel level)
Definition Logging.hpp:13
LogLevel
Definition Logging.hpp:5
@ LogWarn
Definition Logging.hpp:8
@ LogDebug
Definition Logging.hpp:6
@ LogError
Definition Logging.hpp:9
@ LogInfo
Definition Logging.hpp:7
void Foundation_LogImpl(LogLevel level, const char *tag, std::string_view formatted)
Definition Logging.cpp:10