Atlas Game Manager
A game manager for f95 and dlsite written in c++
Loading...
Searching...
No Matches
logging.hpp
Go to the documentation of this file.
1//
2// Created by kj16609 on 1/27/23.
3//
4
5#pragma once
6#ifndef ATLAS_LOGGING_HPP
7#define ATLAS_LOGGING_HPP
8
9#include <QString>
10
11#include <filesystem>
12#include <source_location>
13
14//Formatters needs to exist before we include the other things since notifications.hpp uses some formatters in here for it's own logging
15// clang-format: off
16#include "formatters.hpp"
17// clang-format: on
18
19#include "core/Types.hpp"
22
23namespace atlas::logging
24{
25 void init();
26
27 void setFormat();
28
31
32 std::string formatSourceLocation( const std::source_location loc, const format_ns::string_view msg );
33
34 //Everything in this namespace is defined in `spdlogHelpers.cpp`
35 namespace internal
36 {
37 void logDebug( const std::string_view );
38 void logInfo( const std::string_view );
39 void logWarn( const std::string_view );
40 void logError( const std::string_view );
41 void logCritical( const std::string_view );
42 } // namespace internal
43
44 template < typename... Ts >
45 struct debug
46 {
48 const format_ns::format_string< Ts... > format_string,
49 Ts&&... ts,
50 const std::source_location& source_location = std::source_location::current() )
51 {
52 if constexpr ( sizeof...( Ts ) > 0 )
53 {
54 const std::string user_message { format_ns::format( format_string, std::forward< Ts >( ts )... ) };
55 const std::string full_message { formatSourceLocation( source_location, user_message ) };
56
57 internal::logDebug( full_message );
58 }
59 else
60 {
61 const std::string full_message { formatSourceLocation( source_location, format_string.get() ) };
62
63 internal::logDebug( full_message );
64 }
65 }
66 };
67
68 template < typename... Ts >
69 debug( format_ns::format_string< Ts... >, Ts&&... ) -> debug< Ts... >;
70
71 template < typename... Ts >
72 struct info
73 {
75 const format_ns::format_string< Ts... > format_string,
76 Ts&&... ts,
77 [[maybe_unused]] const std::source_location& source_location = std::source_location::current() )
78 {
79 const std::string user_message { format_ns::format( format_string, std::forward< Ts >( ts )... ) };
80 const std::string full_message { formatSourceLocation( source_location, user_message ) };
81
82 internal::logInfo( full_message );
83
86 }
87 };
88
89 template < typename... Ts >
90 info( format_ns::format_string< Ts... >, Ts&&... ) -> info< Ts... >;
91
92 template < typename... Ts >
93 struct warn
94 {
96 const format_ns::format_string< Ts... > format_string,
97 Ts&&... ts,
98 const std::source_location& source_location = std::source_location::current() )
99 {
100 if constexpr ( sizeof...( Ts ) > 0 )
101 {
102 const std::string user_message { format_ns::format( format_string, std::forward< Ts >( ts )... ) };
103 const std::string full_message { formatSourceLocation( source_location, user_message ) };
104
105 internal::logWarn( full_message );
108 format_ns::format( format_string, std::forward< Ts >( ts )... ),
109 full_message,
111 }
112 else
113 {
114 const std::string full_message { formatSourceLocation( source_location, format_string.get() ) };
115
116 internal::logWarn( full_message );
119 createMessage( format_string.get(), full_message, MessageLevel::ATLAS_WARNING );
120 }
121 }
122 };
123
124 template < typename... Ts >
125 warn( format_ns::format_string< Ts... >, Ts&&... ) -> warn< Ts... >;
126
127 template < typename... Ts >
128 struct error
129 {
131 const format_ns::format_string< Ts... > body,
132 Ts&&... ts,
133 const std::source_location& loc = std::source_location::current() )
134 {
135 if constexpr ( sizeof...( Ts ) > 0 )
136 {
137 const std::string user_message { format_ns::format( body, std::forward< Ts >( ts )... ) };
138 const std::string full_message { formatSourceLocation( loc, user_message ) };
139
140 internal::logError( full_message );
143 format_ns::format( body, std::forward< Ts >( ts )... ),
144 full_message,
146 }
147 else
148 {
149 const std::string full_message { formatSourceLocation( loc, body.get() ) };
150
151 internal::logError( full_message );
154 }
155 }
156 };
157
158 template < typename... Ts >
159 error( format_ns::format_string< Ts... >, Ts&&... a ) -> error< Ts... >;
160
161 template < typename... Ts >
162 struct errorLoc
163 {
164 errorLoc( const format_ns::format_string< Ts... > format_string, const std::source_location& loc, Ts&... ts )
165 {
166 if constexpr ( sizeof...( Ts ) > 0 )
167 {
168 const std::string user_message { format_ns::format( format_string, std::forward< Ts >( ts )... ) };
169 const std::string full_message { formatSourceLocation( loc, user_message ) };
170
171 internal::logError( full_message );
174 format_ns::format( format_string, std::forward< Ts >( ts )... ),
175 full_message,
177 }
178 else
179 {
180 const std::string full_message { formatSourceLocation( loc, format_string.get() ) };
181
182 internal::logError( full_message );
184 atlas::notifications::createMessage( format_string.get(), full_message, MessageLevel::ATLAS_ERROR );
185 }
186 }
187 };
188
189 template < typename... Ts >
190 errorLoc( format_ns::format_string< Ts... >, const std::source_location&, Ts&&... a ) -> errorLoc< Ts... >;
191
192 template < typename... Ts >
193 struct critical
194 {
196 const format_ns::format_string< Ts... > format_string,
197 Ts&&... ts,
198 const std::source_location& loc = std::source_location::current() )
199 {
200 if constexpr ( sizeof...( Ts ) > 0 )
201 {
202 const std::string user_message { format_ns::format( format_string, std::forward< Ts >( ts )... ) };
203 const std::string full_message { formatSourceLocation( loc, user_message ) };
204
205 internal::logCritical( full_message );
208 format_ns::format( format_string, std::forward< Ts >( ts )... ),
209 full_message,
211 }
212 else
213 {
214 const std::string full_message { formatSourceLocation( loc, format_string.get() ) };
215
216 internal::logCritical( full_message );
219 createMessage( format_string.get(), full_message, MessageLevel::ATLAS_CRITICAL );
220 }
221 }
222 };
223
224 template < typename... Ts >
225 critical( format_ns::format_string< Ts... >, Ts&&... ) -> critical< Ts... >;
226
227
228} // namespace atlas::logging
229
230#endif //ATLAS_LOGGING_HPP
@ ATLAS_CRITICAL
Definition MessageNotification.hpp:27
@ ATLAS_WARNING
Definition MessageNotification.hpp:25
@ ATLAS_INFO
Definition MessageNotification.hpp:24
@ ATLAS_ERROR
Definition MessageNotification.hpp:26
void logError(const std::string_view)
Passes message to spdlog.
Definition spdlogHelpers.cpp:51
void logInfo(const std::string_view)
Passes message to spdlog.
Definition spdlogHelpers.cpp:34
void logCritical(const std::string_view)
Passes message to spdlog.
Definition spdlogHelpers.cpp:60
void logWarn(const std::string_view)
Passes message to spdlog.
Definition spdlogHelpers.cpp:42
void logDebug(const std::string_view)
Passes message to spdlog.
Definition spdlogHelpers.cpp:28
Definition logging.cpp:37
warn(format_ns::format_string< Ts... >, Ts &&...) -> warn< Ts... >
void setFormat()
Definition logging.cpp:76
info(format_ns::format_string< Ts... >, Ts &&...) -> info< Ts... >
std::string formatSourceLocation(const std::source_location loc, const format_ns::string_view msg)
Definition logging.cpp:81
debug(format_ns::format_string< Ts... >, Ts &&...) -> debug< Ts... >
errorLoc(format_ns::format_string< Ts... >, const std::source_location &, Ts &&... a) -> errorLoc< Ts... >
critical(format_ns::format_string< Ts... >, Ts &&...) -> critical< Ts... >
void init()
Definition logging.cpp:38
void initGUIHooks()
Loads the GUI hooks required for some warnings to display to the user.
error(format_ns::format_string< Ts... >, Ts &&... a) -> error< Ts... >
void createMessage(QString user_message, QString full_message, const MessageLevel level)
Definition notifications.cpp:36
bool isNotificationsReady()
Definition notifications.cpp:81
Definition Database.cpp:17
Definition logging.hpp:194
critical(const format_ns::format_string< Ts... > format_string, Ts &&... ts, const std::source_location &loc=std::source_location::current())
Definition logging.hpp:195
Definition logging.hpp:46
debug(const format_ns::format_string< Ts... > format_string, Ts &&... ts, const std::source_location &source_location=std::source_location::current())
Definition logging.hpp:47
Definition logging.hpp:163
errorLoc(const format_ns::format_string< Ts... > format_string, const std::source_location &loc, Ts &... ts)
Definition logging.hpp:164
Definition logging.hpp:129
error(const format_ns::format_string< Ts... > body, Ts &&... ts, const std::source_location &loc=std::source_location::current())
Definition logging.hpp:130
Definition logging.hpp:73
info(const format_ns::format_string< Ts... > format_string, Ts &&... ts, const std::source_location &source_location=std::source_location::current())
Definition logging.hpp:74
Definition logging.hpp:94
warn(const format_ns::format_string< Ts... > format_string, Ts &&... ts, const std::source_location &source_location=std::source_location::current())
Definition logging.hpp:95