Gamedev Framework (gf)  0.4.0
A C++11 framework for 2D games
Log.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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. The severity level indicates
44  * the severity of the message and range from a simple debug message to a
45  * fatal message.
46  *
47  * You can set the minimum severity for which messages are displayed. By
48  * default, the minimum severity level is gf::Log::Warning (or
49  * gf::Log::Debug if debug mode is activated at build time).
50  *
51  * Example:
52  *
53  * ~~~{.cc}
54  * int entity = ...;
55  * gf::Log::warning("Something weird happened to entity %i!\n", entity);
56  * ~~~
57  *
58  */
59  class GF_API Log {
60  public:
61  /**
62  * @brief Deleted constructor
63  */
64  Log() = delete;
65 
66  /**
67  * @brief The severity level of the log
68  */
69  enum Level : int {
70  Debug, ///< The debug level
71  Info, ///< The info level
72  Warn, ///< The warn level
73  Error, ///< The error level
74  Fatal, ///< The fatal level
75  };
76 
77  /**
78  * @brief Set a global severity level
79  *
80  * @param level The severity level
81  */
82  static void setLevel(Level level);
83 
84  /**
85  * @brief Print a debug message
86  *
87  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
88  */
89  static void debug(const char *fmt, ...) {
90  va_list ap;
91  va_start(ap, fmt);
92  log(Level::Debug, fmt, ap);
93  va_end(ap);
94  }
95 
96  /**
97  * @brief Print an info message
98  *
99  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
100  */
101  static void info(const char *fmt, ...) {
102  va_list ap;
103  va_start(ap, fmt);
104  log(Level::Info, fmt, ap);
105  va_end(ap);
106  }
107 
108  /**
109  * @brief Print a warning message
110  *
111  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
112  */
113  static void warning(const char *fmt, ...) {
114  va_list ap;
115  va_start(ap, fmt);
116  log(Level::Warn, fmt, ap);
117  va_end(ap);
118  }
119 
120  /**
121  * @brief Print an error message
122  *
123  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
124  */
125  static void error(const char *fmt, ...) {
126  va_list ap;
127  va_start(ap, fmt);
128  log(Level::Error, fmt, ap);
129  va_end(ap);
130  }
131 
132  /**
133  * @brief Print a fatal message and quit
134  *
135  * @param fmt The [format string](http://en.cppreference.com/w/cpp/io/c/fprintf)
136  */
137  static void fatal(const char *fmt, ...) {
138  va_list ap;
139  va_start(ap, fmt);
140  log(Level::Fatal, fmt, ap);
141  va_end(ap);
142 
143  std::abort();
144  }
145 
146  private:
147  static Level s_level;
148 
149  private:
150  static void log(Level level, const char *fmt, va_list ap);
151  };
152 
153 #ifndef DOXYGEN_SHOULD_SKIP_THIS
154 }
155 #endif
156 }
157 
158 #endif // GF_LOG_H
The error level.
Definition: Log.h:73
static void error(const char *fmt,...)
Print an error message.
Definition: Log.h:125
Level
The severity level of the log.
Definition: Log.h:69
static void fatal(const char *fmt,...)
Print a fatal message and quit.
Definition: Log.h:137
static void warning(const char *fmt,...)
Print a warning message.
Definition: Log.h:113
The namespace for gf classes.
Definition: Action.h:34
The debug level.
Definition: Log.h:70
Log()=delete
Deleted constructor.
The warn level.
Definition: Log.h:72
static void info(const char *fmt,...)
Print an info message.
Definition: Log.h:101
The fatal level.
Definition: Log.h:74
Logging functions.
Definition: Log.h:59
static void debug(const char *fmt,...)
Print a debug message.
Definition: Log.h:89
The info level.
Definition: Log.h:71
#define GF_API
Definition: Portability.h:35
static void setLevel(Level level)
Set a global severity level.