nomlib
Loading...
Searching...
No Matches
SDL2Logger.hpp
1/******************************************************************************
2
3 nomlib - C++11 cross-platform game engine
4
5Copyright (c) 2013, 2014 Jeffrey Carpenter <i8degrees@gmail.com>
6All rights reserved.
7
8Redistribution and use in source and binary forms, with or without
9modification, are permitted provided that the following conditions are met:
10
111. Redistributions of source code must retain the above copyright notice, this
12 list of conditions and the following disclaimer.
132. Redistributions in binary form must reproduce the above copyright notice,
14 this list of conditions and the following disclaimer in the documentation
15 and/or other materials provided with the distribution.
16
17THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
18ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
21ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
26SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
28******************************************************************************/
29#ifndef NOMLIB_CORE_SDL2_LOGGER_HPP
30#define NOMLIB_CORE_SDL2_LOGGER_HPP
31
32#include <sstream>
33#include <string>
34
35#include <SDL.h>
36
53enum
54{
60 NOM = SDL_LOG_CATEGORY_CUSTOM,
61
63 NOM_LOG_CATEGORY_APPLICATION,
64
66 NOM_LOG_CATEGORY_ERROR,
67
69 NOM_LOG_CATEGORY_ASSERT,
70
72 NOM_LOG_CATEGORY_SYSTEM,
73
75 NOM_LOG_CATEGORY_AUDIO,
76
78 NOM_LOG_CATEGORY_VIDEO,
79
81 NOM_LOG_CATEGORY_RENDER,
82
84 NOM_LOG_CATEGORY_ACTION,
85
87 NOM_LOG_CATEGORY_ACTION_PLAYER,
88
90 NOM_LOG_CATEGORY_ACTION_QUEUE,
91
93 NOM_LOG_CATEGORY_EVENT,
94
96 NOM_LOG_CATEGORY_TEST,
97
99 NOM_LOG_CATEGORY_GUI,
100
102 NOM_LOG_CATEGORY_MEMORY,
103
105 NOM_LOG_CATEGORY_MEMORY_TOTALS,
106
108 NOM_LOG_CATEGORY_STATES,
109
110 NOM_LOG_CATEGORY_TRACE,
111 NOM_LOG_CATEGORY_TRACE_AUDIO,
112
114 NOM_LOG_CATEGORY_TRACE_FONTS,
115
116 NOM_LOG_CATEGORY_TRACE_VIDEO,
117 NOM_LOG_CATEGORY_TRACE_RENDER,
118 NOM_LOG_CATEGORY_TRACE_GUI,
119 NOM_LOG_CATEGORY_TRACE_EVENT,
120
122 NOM_LOG_CATEGORY_TRACE_STATES,
123
124 NOM_LOG_CATEGORY_TRACE_SYSTEM,
125
127 NOM_LOG_CATEGORY_TRACE_UNIT_TEST,
128
130 NOM_LOG_CATEGORY_TRACE_ACTION,
131
144 NOM_LOG_CATEGORY_CUSTOM
145};
146
147namespace nom {
148
155enum LogPriority
156{
157 NOM_LOG_PRIORITY_VERBOSE = SDL_LOG_PRIORITY_VERBOSE,
158 NOM_LOG_PRIORITY_DEBUG = SDL_LOG_PRIORITY_DEBUG,
159 NOM_LOG_PRIORITY_INFO = SDL_LOG_PRIORITY_INFO,
160 NOM_LOG_PRIORITY_WARN = SDL_LOG_PRIORITY_WARN,
161 NOM_LOG_PRIORITY_ERROR = SDL_LOG_PRIORITY_ERROR,
162 NOM_LOG_PRIORITY_CRITICAL = SDL_LOG_PRIORITY_CRITICAL,
163 NOM_NUM_LOG_PRIORITIES = SDL_NUM_LOG_PRIORITIES
164};
165
166namespace priv {
167
172void log_message( void* ptr, int cat, SDL_LogPriority prio, const char* msg );
173
174} // namespace priv
175
182template<typename Type>
183void write_debug_output( std::ostream& out, const Type& f )
184{
185 out << f;
186}
187
190{
191 public:
192 typedef SDL2Logger self_type;
193
195 static const std::string priority_prefixes_[NOM_NUM_LOG_PRIORITIES];
196
199 static bool initialized( void );
200
204 static void initialize();
205
209
218 SDL2Logger( int cat, nom::LogPriority prio );
219
228
232 template <typename Type, typename ... Args>
233 void write( const Type& f, const Args& ... rest )
234 {
235 nom::write_debug_output( this->output_stream(), f );
236
237 this->write( " " );
238
239 this->write( rest... );
240 }
241
248 template <typename Type>
249 void write( const Type& f )
250 {
251 nom::write_debug_output( this->output_stream(), f );
252 }
253
254 void write(uint8_t f)
255 {
256 nom::write_debug_output(this->output_stream(), (int)f);
257 }
258
265 void write();
266
268 int category() const;
269
271 LogPriority priority() const;
272
276 std::ostringstream& output_stream();
277
281 static LogPriority priority( SDL_LogPriority prio );
282
285 static SDL_LogPriority SDL_priority( LogPriority prio );
286
290 static void set_logging_priorities( LogPriority prio );
291
295 static void set_logging_priority( int cat, LogPriority prio );
296
297 private:
304 SDL2Logger( const self_type& rhs ) = delete;
305
312 self_type& operator =( const self_type& rhs ) = delete;
313
316
318 nom::LogPriority priority_;
319
321 std::ostringstream os_;
322
327 static bool initialized_;
328};
329
330} // namespace nom
331
332#endif // include guard defined
333
static void set_logging_priorities(LogPriority prio)
Set the logging priority for all logging categories.
void write()
Write a log message to the output stream.
int category_
The logging category of the log message.
static void initialize()
Set the default logging category priority levels.
LogPriority priority() const
Get the logging priority level.
static void set_logging_priority(int cat, LogPriority prio)
Set the logging priority for a log category.
std::ostringstream & output_stream()
Get the log message.
static const std::string priority_prefixes_[NOM_NUM_LOG_PRIORITIES]
Human-friendly descriptions of the logging priorities.
static bool initialized_
Ensure one-time initialization of the setting of logging category priorities.
SDL2Logger()
Default constructor; initialize the log category to NOM and the log priority to LogPriority::NOM_LOG_...
std::ostringstream os_
The log message stream.
SDL2Logger(const self_type &rhs)=delete
Copy constructor.
static LogPriority priority(SDL_LogPriority prio)
Convert an underlying implementation's logging priority level.
nom::LogPriority priority_
The logging priority of the log message.
SDL2Logger(int cat, nom::LogPriority prio)
Construct a logging object.
void write(const Type &f, const Args &... rest)
Write one or more log messages to the output stream.
static SDL_LogPriority SDL_priority(LogPriority prio)
Convert the logging priority level to the underlying implementation's type.
self_type & operator=(const self_type &rhs)=delete
Copy assignment constructor.
void write(const Type &f)
Write a log message to the output stream.
static bool initialized(void)
Get initialization status of the default logging category priorities.
~SDL2Logger()
Destructor; the logging category, logging priority level and output stream is sent to the underlying ...
int category() const
Get the logging category.