Toast Engine
Loading...
Searching...
No Matches
log.hpp
Go to the documentation of this file.
1
7
8#pragma once
9#include "export.hpp"
10
11#include <cassert>
12#include <cstdint>
13#include <cstdlib>
14#include <format>
15#include <string_view>
16#include <tracy/Tracy.hpp>
17
19namespace logging::_detail {
20constexpr auto getOnlyName(std::string_view path) -> std::string_view {
21 size_t last_slash = path.find_last_of("\\/");
22 if (last_slash == std::string_view::npos) {
23 return path;
24 }
25 return path.substr(last_slash + 1);
26}
27
28void TOAST_API
29 log(uint8_t severity, std::string_view file_name, unsigned line_number, std::string_view sink, std::string_view message);
30}
31
32#define TOAST_FILE_NAME ::logging::_detail::getOnlyName(__FILE__)
33
34#ifdef DEBUG
35#define TOAST_LOG_IMPL(severity, sink, ...) \
36 do { \
37 std::string toast_log_impl_message_ = std::format(__VA_ARGS__); \
38 ::logging::_detail::log(severity, TOAST_FILE_NAME, __LINE__, sink, toast_log_impl_message_); \
39 constexpr uint32_t red = 0xFF0000; \
40 constexpr uint32_t green = 0x00FF00; \
41 constexpr uint32_t yellow = red | green; /* i love this so much -x */ \
42 switch (severity) { \
43 case 4: \
44 case 3: TracyMessageC(toast_log_impl_message_.c_str(), toast_log_impl_message_.size(), red); break; \
45 case 2: TracyMessageC(toast_log_impl_message_.c_str(), toast_log_impl_message_.size(), yellow); break; \
46 case 1: TracyMessageC(toast_log_impl_message_.c_str(), toast_log_impl_message_.size(), green); break; \
47 default: break; \
48 } \
49 } while (0)
50#else
51#define TOAST_LOG_IMPL(severity, sink, ...) \
52 do { \
53 ::logging::_detail::log(severity, TOAST_FILE_NAME, __LINE__, sink, std::format(__VA_ARGS__)); \
54 } while (0)
55#endif
56
71#define TOAST_TRACE(sink, ...) TOAST_LOG_IMPL(0, sink, __VA_ARGS__)
72
83#define TOAST_INFO(sink, ...) TOAST_LOG_IMPL(1, sink, __VA_ARGS__)
84
94#define TOAST_WARN(sink, ...) TOAST_LOG_IMPL(2, sink, __VA_ARGS__)
95
104#define TOAST_ERROR(sink, ...) TOAST_LOG_IMPL(3, sink, __VA_ARGS__)
105
113#define TOAST_CRITICAL(sink, ...) \
114 do { \
115 TOAST_LOG_IMPL(4, sink, __VA_ARGS__); \
116 std::abort(); \
117 } while (0)
118
119#ifdef _DEBUG
127#define TOAST_ASSERT(condition, sink, ...) \
128 do { \
129 if (!(condition)) { \
130 TOAST_LOG_IMPL(4, sink, __VA_ARGS__); \
131 assert(condition); \
132 } \
133 } while (0)
134#else
135#define TOAST_ASSERT(condition, sink, ...)
136#endif
137
138#define TOAST_NOT_IMPLEMENTED TOAST_ASSERT(false, "Engine", "Not implemented")