HoverRace  2.0
Log.h
Go to the documentation of this file.
1 
2 // Log.h
3 //
4 // Copyright (c) 2013-2015 Michael Imamura.
5 //
6 // Licensed under GrokkSoft HoverRace SourceCode License v1.0(the "License");
7 // you may not use this file except in compliance with the License.
8 //
9 // A copy of the license should have been attached to the package from which
10 // you have taken this file. If you can not find the license you can not use
11 // this file.
12 //
13 //
14 // The author makes no representations about the suitability of
15 // this software for any purpose. It is provided "as is" "AS IS",
16 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
17 // implied.
18 //
19 // See the License for the specific language governing permissions
20 // and limitations under the License.
21 
22 #pragma once
23 
24 #include <boost/log/trivial.hpp>
25 
26 #if defined(_WIN32) && defined(HR_ENGINE_SHARED)
27 # ifdef MR_ENGINE
28 # define MR_DllDeclare __declspec( dllexport )
29 # else
30 # define MR_DllDeclare __declspec( dllimport )
31 # endif
32 #else
33 # define MR_DllDeclare
34 #endif
35 
36 // Previously defined in WinGDI.h.
37 #ifdef ERROR
38 # undef ERROR
39 #endif
40 #ifdef TRACE
41 # undef TRACE
42 #endif
43 
45 #define HR_LOG(lvl) BOOST_LOG_TRIVIAL(lvl)
46 
47 namespace HoverRace {
48 namespace Util {
49 
50 namespace Log {
51 
52 namespace detail {
53 
54 const size_t MAX_LOG = 512;
55 
56 MR_DllDeclare std::string Fmt(const char *fmt, va_list ap);
57 
58 } // namespace detail
59 
60 MR_DllDeclare void Init(bool verboseLog = false);
61 
62 enum class Level
63 {
64  // Intentionally aligned with boost::log::trivial to avoid conversions.
65  TRACE = boost::log::trivial::trace,
66  DEBUG = boost::log::trivial::debug,
68  WARN = boost::log::trivial::warning,
69  ERROR = boost::log::trivial::error,
70  FATAL = boost::log::trivial::fatal,
71 };
72 
73 struct Entry
74 {
75  constexpr Entry(const Level level, const char *message) noexcept :
76  level(level), message(message) { }
77  Entry &operator=(const Entry&) = delete;
78 
79  const Level level;
80  const char *message;
81 };
82 
83 using logAdded_t = boost::signals2::signal<void(const Entry&)>;
85 
86 inline void Debug(const char *fmt, ...)
87 {
88  va_list ap;
89  va_start(ap, fmt);
90  BOOST_LOG_TRIVIAL(debug) << detail::Fmt(fmt, ap);
91  va_end(ap);
92 }
93 
94 inline void Info(const char *fmt, ...)
95 {
96  va_list ap;
97  va_start(ap, fmt);
98  BOOST_LOG_TRIVIAL(info) << detail::Fmt(fmt, ap);
99  va_end(ap);
100 }
101 
102 inline void Warn(const char *fmt, ...)
103 {
104  va_list ap;
105  va_start(ap, fmt);
106  BOOST_LOG_TRIVIAL(warning) << detail::Fmt(fmt, ap);
107  va_end(ap);
108 }
109 
110 inline void Error(const char *fmt, ...)
111 {
112  va_list ap;
113  va_start(ap, fmt);
114  BOOST_LOG_TRIVIAL(error) << detail::Fmt(fmt, ap);
115  va_end(ap);
116 }
117 
118 inline void Fatal(const char *fmt, ...)
119 {
120  va_list ap;
121  va_start(ap, fmt);
122  BOOST_LOG_TRIVIAL(fatal) << detail::Fmt(fmt, ap);
123  va_end(ap);
124 }
125 
126 } // namespace Log
127 
128 } // namespace Util
129 } // namespace HoverRace
130 
131 #undef MR_DllDeclare
const size_t MAX_LOG
The maximum length of a vararg log.
Definition: Log.h:54
Definition: Log.h:73
void Warn(const char *fmt,...)
Definition: Log.h:102
Level
Definition: Log.h:62
SDL_RendererInfo info
Definition: SdlDisplay.cpp:253
Definition: Log.cpp:52
void Debug(const char *fmt,...)
Definition: Log.h:86
boost::signals2::signal< void(const Entry &)> logAdded_t
Definition: Log.h:83
const char * message
Definition: Log.h:80
#define MR_DllDeclare
Definition: Log.h:33
logAdded_t logAddedSignal
Definition: Log.cpp:54
void Info(const char *fmt,...)
Definition: Log.h:94
Definition: Announcement.h:24
std::string Fmt(const char *fmt, va_list ap)
Definition: Log.cpp:201
const Level level
Definition: Log.h:79
constexpr Entry(const Level level, const char *message) noexcept
Definition: Log.h:75
void Init(bool verboseLog)
Initialize the system log.
Definition: Log.cpp:222
void Error(const char *fmt,...)
Definition: Log.h:110
void Fatal(const char *fmt,...)
Definition: Log.h:118