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