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 "export.hpp"
33#include <sstream>
34#include <string>
35
36#include <SDL.h>
37
54enum
55{
61 NOM = SDL_LOG_CATEGORY_CUSTOM,
62
64 NOM_LOG_CATEGORY_APPLICATION,
65
67 NOM_LOG_CATEGORY_ERROR,
68
70 NOM_LOG_CATEGORY_ASSERT,
71
73 NOM_LOG_CATEGORY_SYSTEM,
74
76 NOM_LOG_CATEGORY_AUDIO,
77
79 NOM_LOG_CATEGORY_VIDEO,
80
82 NOM_LOG_CATEGORY_RENDER,
83
85 NOM_LOG_CATEGORY_ACTION,
86
88 NOM_LOG_CATEGORY_ACTION_PLAYER,
89
91 NOM_LOG_CATEGORY_ACTION_QUEUE,
92
94 NOM_LOG_CATEGORY_EVENT,
95
97 NOM_LOG_CATEGORY_TEST,
98
100 NOM_LOG_CATEGORY_GUI,
101
103 NOM_LOG_CATEGORY_MEMORY,
104
106 NOM_LOG_CATEGORY_MEMORY_TOTALS,
107
109 NOM_LOG_CATEGORY_STATES,
110
111 NOM_LOG_CATEGORY_TRACE,
112 NOM_LOG_CATEGORY_TRACE_AUDIO,
113
115 NOM_LOG_CATEGORY_TRACE_FONTS,
116
117 NOM_LOG_CATEGORY_TRACE_VIDEO,
118 NOM_LOG_CATEGORY_TRACE_RENDER,
119 NOM_LOG_CATEGORY_TRACE_GUI,
120 NOM_LOG_CATEGORY_TRACE_EVENT,
121
123 NOM_LOG_CATEGORY_TRACE_STATES,
124
125 NOM_LOG_CATEGORY_TRACE_SYSTEM,
126
128 NOM_LOG_CATEGORY_TRACE_UNIT_TEST,
129
131 NOM_LOG_CATEGORY_TRACE_ACTION,
132
145 NOM_LOG_CATEGORY_CUSTOM
146};
147
148namespace nom {
149
156enum LogPriority
157{
158 NOM_LOG_PRIORITY_VERBOSE = SDL_LOG_PRIORITY_VERBOSE,
159 NOM_LOG_PRIORITY_DEBUG = SDL_LOG_PRIORITY_DEBUG,
160 NOM_LOG_PRIORITY_INFO = SDL_LOG_PRIORITY_INFO,
161 NOM_LOG_PRIORITY_WARN = SDL_LOG_PRIORITY_WARN,
162 NOM_LOG_PRIORITY_ERROR = SDL_LOG_PRIORITY_ERROR,
163 NOM_LOG_PRIORITY_CRITICAL = SDL_LOG_PRIORITY_CRITICAL,
164 NOM_NUM_LOG_PRIORITIES = SDL_NUM_LOG_PRIORITIES
165};
166
167namespace priv {
168
173NOM_EXPORT void log_message( void* ptr, int cat, SDL_LogPriority prio, const char* msg );
174
175} // namespace priv
176
183template<typename Type>
184NOM_EXPORT void write_debug_output( std::ostream& out, const Type& f )
185{
186 out << f;
187}
188
190class NOM_EXPORT SDL2Logger
191{
192 public:
193 typedef SDL2Logger self_type;
194
196 static const std::string priority_prefixes_[NOM_NUM_LOG_PRIORITIES];
197
200 static bool initialized( void );
201
205 static void initialize();
206
210
219 SDL2Logger( int cat, nom::LogPriority prio );
220
229
233 template <typename Type, typename ... Args>
234 void write( const Type& f, const Args& ... rest )
235 {
236 nom::write_debug_output( this->output_stream(), f );
237
238 this->write( " " );
239
240 this->write( rest... );
241 }
242
249 template <typename Type>
250 void write( const Type& f )
251 {
252 nom::write_debug_output( this->output_stream(), f );
253 }
254
255 void write(uint8_t f)
256 {
257 nom::write_debug_output(this->output_stream(), (int)f);
258 }
259
266 void write();
267
269 int category() const;
270
272 LogPriority priority() const;
273
277 std::ostringstream& output_stream();
278
282 static LogPriority priority( SDL_LogPriority prio );
283
286 static SDL_LogPriority SDL_priority( LogPriority prio );
287
291 static void set_logging_priorities( LogPriority prio );
292
296 static void set_logging_priority( int cat, LogPriority prio );
297
298 private:
305 SDL2Logger( const self_type& rhs ) = delete;
306
313 self_type& operator =( const self_type& rhs ) = delete;
314
317
319 nom::LogPriority priority_;
320
322 std::ostringstream os_;
323
328 static bool initialized_;
329};
330
331} // namespace nom
332
333#endif // include guard defined
334
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.
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.