SA_Logger
Sapphire's Suite's C++ Logger.
Loading...
Searching...
No Matches
LogMacro.hpp
Go to the documentation of this file.
1// Copyright (c) 2023 Sapphire's Suite. All Rights Reserved.
2
3#pragma once
4
5#ifndef SAPPHIRE_LOGGER_LOG_MACRO_GUARD
6#define SAPPHIRE_LOGGER_LOG_MACRO_GUARD
7
8#include <iostream>
9
10#include <SA/Support/Debug.hpp>
11
12#include <SA/Logger/Log/Log.hpp>
13#include <SA/Logger/Misc/StringFormat.hpp>
14#include <SA/Logger/Preprocessors/FileName.hpp>
15#include <SA/Logger/Preprocessors/FunctionName.hpp>
16#include <SA/Logger/Preprocessors/ChannelName.hpp>
17#include <SA/Logger/Preprocessors/Unparenthese.hpp>
18
29namespace SA
30{
31//{ Callback
32
33 class LoggerBase;
34
36 namespace Debug
37 {
39 extern LoggerBase* logger;
40
42 extern void (*logCB)(SA::Log);
43
49
55
56
58
59 namespace Intl
60 {
61 void DefaultLogCallback(SA::Log _log);
62 }
63
65 }
66
68
69 #define __SA_LOGGER(_log)\
70 if (SA::Debug::logger)\
71 SA::Debug::logger->Log(_log);\
72 else if (SA::Debug::logCB)\
73 SA::Debug::logCB(_log);\
74 else\
75 std::cerr << "Try logging with invalid logger instance or callback! Initialize SA::Debug::logger or SA::Debug::logCB." << std::endl;
76
77 #define __SA_LOG_FRAME_NUM\
78 SA::Debug::logger ? SA::Debug::logger->GetFrameNum() : 0u
79
81
82//}
83
84
86
87 //#define __SA_TRY_FAST_WIDE(_str) std::is_same_v<std::remove_extent_t<std::remove_reference_t<decltype(_str)>>, const char> ? (SA_WIDE(_str)) : SA::ToWString(_str)
88
89 #define __SA_CREATE_LOG(_str, _lvl, _chan, _dets) SA::Log(\
90 __SA_FILE_NAME,\
91 __LINE__,\
92 __SA_FUNC_NAME,\
93 SA::StringFormat(__SA_UNPARENT(_str)),\
94 SA::LogLevel::_lvl,\
95 __SA_CHAN_NAME(_chan),\
96 SA::StringFormat(__SA_UNPARENT(_dets)),\
97 __SA_LOG_FRAME_NUM,\
98 std::string()\
99 )
100
101 // TODO: Add backtrace.
102
103 #define __SA_SELECT_LOG_MACRO(_1, _2, _3, _4, _5, _name, ...) _name
104
105 #define __SA_LOG4(_str, _lvl, _chan, _dets) __SA_LOGGER(__SA_CREATE_LOG(_str, _lvl, _chan, _dets));
106 #define __SA_LOG5(_str, _lvl, _chan, _dets, _postCmd) { __SA_LOG4(_str, _lvl, _chan, _dets) _postCmd; }
107 #define __SA_LOG3(_str, _lvl, _chan) __SA_LOG4(_str, _lvl, _chan, L"")
108 #define __SA_LOG2(_str, _lvl) __SA_LOG3(_str, _lvl, Default)
109 #define __SA_LOG1(_str) __SA_LOG2(_str, Normal)
110
111 #define __SA_COND_LOG(_pred, _lvl, ...) if(!(_pred)) SA_LOG(L"{ " SA_WSTR(_pred) " }\tevaluated to false!", _lvl, ##__VA_ARGS__)
112
114
115#if defined(DOXYGEN)
116
124 #define SA_LOG_END_OF_FRAME()
125
139 #define SA_LOG((_str, _args), _lvl, _chan, _dets)
140
153 #define SA_WARN(_pred, _chan, _dets, _postCmd)
154
167 #define SA_ERROR(_pred, _chan, _dets, _postCmd)
168
169#elif SA_DEBUG || SA_LOG_RELEASE_OPT
170
171 #define SA_LOG_END_OF_FRAME() { if(SA::Debug::logger) SA::Debug::logger->IncrementFrameNum(); }
172
173 #define SA_LOG(...) { __SA_SELECT_LOG_MACRO(__VA_ARGS__, __SA_LOG5, __SA_LOG4, __SA_LOG3, __SA_LOG2, __SA_LOG1)(__VA_ARGS__) }
174
175 #define SA_WARN(_pred, ...) { __SA_COND_LOG(_pred, Warning, ##__VA_ARGS__) }
176
177 #define SA_ERROR(_pred, ...) { __SA_COND_LOG(_pred, Error, ##__VA_ARGS__) }
178
179#else
180
181 #define SA_LOG_END_OF_FRAME() {}
182
183 #define SA_LOG(...) {}
184
185 #define SA_WARN(...) {}
186
187 #define SA_ERROR(...) {}
188
189#endif
190}
191
192
195#endif // GUARD
Log type implementation.
Log type implementation.
Definition Log.hpp:25
Logger base class implementation.
Definition LoggerBase.hpp:29
void InitDefaultLogger()
Initialize default logger for single-thread use. Console color and file output.
LoggerBase * logger
Logger instance.
void InitDefaultLoggerThread()
Initialize default logger for multi-thread use. Console color and file output.
void(* logCB)(SA::Log)
Simple logger callback.