Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
Log.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_LOG_H
22 #define GF_LOG_H
23 
24 #include <cstdarg>
25 #include <cstdlib>
26 #include <map>
27 
28 #include "Portability.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  /**
36  * @ingroup core
37  * @brief Logging functions
38  *
39  * Logging functions are used to report some events that have happened in
40  * the system. These functions print messages to the standard error. It uses
41  * `printf`-like format strings.
42  *
43  * A message is associated to a severity level and a category. The severity
44  * level indicates the severity of the message and range from a simple debug
45  * message to a fatal message. The category of the message indicates the
46  * origin of the message in the library.
47  *
48  * You can set the minimum severity for which messages are displayed either
49  * globally or for each category. By default, the minimum severity level is
50  * gf::Log::Warning (or gf::Log::Debug if debug mode is activated at build
51  * time).
52  *
53  * Example:
54  *
55  * ~~~{.cc}
56  * int entity = ...;
57  * gf::Log::warning(gf::Log::General, "Something weird happened to entity %i!\n", entity);
58  * ~~~
59  *
60  */
61  class GF_API Log {
62  public:
63  /**
64  * @brief Deleted constructor
65  */
66  Log() = delete;
67 
68  /**
69  * @brief The severity level of the log
70  */
71  enum Level : int {
72  Debug, ///< The debug level
73  Info, ///< The info level
74  Warn, ///< The warn level
75  Error, ///< The error level
76  Fatal, ///< The fatal level
77  };
78 
79  /**
80  * @brief The category of the log
81  */
82  enum Category : int {
83  General, ///< The general category
84  Graphics, ///< The graphics category
85  Network, ///< The network category
86  Physics, ///< The physics category
87  Resources, ///< The resources category
88  };
89 
90  /**
91  * @brief Set a global severity level
92  *
93  * @param level The severity level
94  */
95  static void setLevel(Level level);
96 
97  /**
98  * @brief Set a severity level for a category
99  *
100  * @param category The category
101  * @param level The severity level
102  */
103  static void setLevel(Category category, Level level);
104 
105  /**
106  * @brief Print a debug message
107  *
108  * @param category The category
109  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
110  */
111  static void debug(Category category, const char *fmt, ...) {
112  va_list ap;
113  va_start(ap, fmt);
114  log(Level::Debug, category, fmt, ap);
115  va_end(ap);
116  }
117 
118  /**
119  * @brief Print an info message
120  *
121  * @param category The category
122  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
123  */
124  static void info(Category category, const char *fmt, ...) {
125  va_list ap;
126  va_start(ap, fmt);
127  log(Level::Info, category, fmt, ap);
128  va_end(ap);
129  }
130 
131  /**
132  * @brief Print a warning message
133  *
134  * @param category The category
135  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
136  */
137  static void warning(Category category, const char *fmt, ...) {
138  va_list ap;
139  va_start(ap, fmt);
140  log(Level::Warn, category, fmt, ap);
141  va_end(ap);
142  }
143 
144  /**
145  * @brief Print an error message
146  *
147  * @param category The category
148  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
149  */
150  static void error(Category category, const char *fmt, ...) {
151  va_list ap;
152  va_start(ap, fmt);
153  log(Level::Error, category, fmt, ap);
154  va_end(ap);
155  }
156 
157  /**
158  * @brief Print a fatal message and quit
159  *
160  * @param category The category
161  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
162  */
163  static void fatal(Category category, const char *fmt, ...) {
164  va_list ap;
165  va_start(ap, fmt);
166  log(Level::Fatal, category, fmt, ap);
167  va_end(ap);
168 
169  std::abort();
170  }
171 
172  /**
173  * @brief Print a message without any level or category
174  *
175  * This function can be used for printing messages during the development.
176  *
177  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
178  */
179  static void print(const char *fmt, ...);
180 
181  private:
182  static void log(Level level, Category category, const char *fmt, va_list ap);
183 
184  private:
185  static std::map<Category, Level> s_levels;
186  };
187 
188 #ifndef DOXYGEN_SHOULD_SKIP_THIS
189 }
190 #endif
191 }
192 
193 #endif // GF_LOG_H
The error level.
Definition: Log.h:75
static void debug(Category category, const char *fmt,...)
Print a debug message.
Definition: Log.h:111
Category
The category of the log.
Definition: Log.h:82
static void warning(Category category, const char *fmt,...)
Print a warning message.
Definition: Log.h:137
Level
The severity level of the log.
Definition: Log.h:71
The network category.
Definition: Log.h:85
The resources category.
Definition: Log.h:87
static void setLevel(Category category, Level level)
Set a severity level for a category.
static void print(const char *fmt,...)
Print a message without any level or category.
static void fatal(Category category, const char *fmt,...)
Print a fatal message and quit.
Definition: Log.h:163
Definition: Action.h:34
static void info(Category category, const char *fmt,...)
Print an info message.
Definition: Log.h:124
The debug level.
Definition: Log.h:72
Log()=delete
Deleted constructor.
The warn level.
Definition: Log.h:74
static void error(Category category, const char *fmt,...)
Print an error message.
Definition: Log.h:150
The fatal level.
Definition: Log.h:76
The graphics category.
Definition: Log.h:84
The physics category.
Definition: Log.h:86
Logging functions.
Definition: Log.h:61
The info level.
Definition: Log.h:73
#define GF_API
Definition: Portability.h:35
static void setLevel(Level level)
Set a global severity level.
The general category.
Definition: Log.h:83