Lunchbox  1.16.0
Multi-threaded C++ toolbox library for all application developers creating high-performance multi-threaded programs.
log.h
Go to the documentation of this file.
1 
2 /* Copyright (c) 2005-2017, Stefan Eilemann <eile@equalizergraphics.com>
3  * Daniel Nachbaur <danielnachbaur@gmail.com>
4  *
5  * This library is free software; you can redistribute it and/or modify it under
6  * the terms of the GNU Lesser General Public License version 2.1 as published
7  * by the Free Software Foundation.
8  *
9  * This library is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
11  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
12  * details.
13  *
14  * You should have received a copy of the GNU Lesser General Public License
15  * along with this library; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17  */
18 
27 #ifndef LUNCHBOX_LOG_H
28 #define LUNCHBOX_LOG_H
29 
30 #include <lunchbox/api.h>
31 #include <lunchbox/compiler.h>
32 #include <lunchbox/types.h>
33 
34 #include <assert.h>
35 #include <iomanip>
36 #include <iostream>
37 #include <sstream>
38 
39 namespace lunchbox
40 {
43 {
44  LOG_ERROR = 1,
49  LOG_ALL = 6
50 };
51 
54 {
55  LOG_EXCEPTION = 0x01,
56  LOG_BUG = 0x04,
57  LOG_CUSTOM = 0x10,
58  LOG_ANY = 0xffffu
59 };
60 
61 namespace detail
62 {
63 class Log;
64 }
65 
72 class Log : public std::ostream
73 {
74 public:
76  LUNCHBOX_API void indent();
77 
79  LUNCHBOX_API void exdent();
80 
82  LUNCHBOX_API void disableFlush();
83 
85  LUNCHBOX_API void enableFlush();
86 
88  LUNCHBOX_API void forceFlush();
89 
91  LUNCHBOX_API void disableHeader();
92 
94  LUNCHBOX_API void enableHeader();
95 
97  static LUNCHBOX_API int level;
98 
100  static LUNCHBOX_API unsigned topics;
101 
103  static LUNCHBOX_API Log& instance();
104 
106  static LUNCHBOX_API Log& instance(const char* file, const int line);
107 
109  static LUNCHBOX_API void exit();
110 
112  static LUNCHBOX_API void reset();
113 
115  static std::string& getLogLevelString();
116 
118  static LUNCHBOX_API int getLogLevel(const char* level);
119 
121  static LUNCHBOX_API void setOutput(std::ostream& stream);
122 
124  static LUNCHBOX_API bool setOutput(const std::string& file);
125 
127  static LUNCHBOX_API std::ostream& getOutput();
128 
137  static LUNCHBOX_API void setClock(Clock* clock);
138 
139  static const Clock& getClock();
140 
141  LUNCHBOX_API void setThreadName(const std::string& name);
142  LUNCHBOX_API const std::string& getThreadName() const;
143 
144 private:
145  detail::Log* const impl_;
146 
147  Log();
148 
149  template <class T>
150  friend void perThreadDelete(T*);
151  virtual ~Log();
152 
153  Log(const Log&);
154  Log& operator=(const Log&);
155 
156  void setLogInfo(const char* file, const int line);
157 };
158 
164 LUNCHBOX_API std::ostream& indent(std::ostream& os);
166 LUNCHBOX_API std::ostream& exdent(std::ostream& os);
167 
169 LUNCHBOX_API std::ostream& disableFlush(std::ostream& os);
171 LUNCHBOX_API std::ostream& enableFlush(std::ostream& os);
173 LUNCHBOX_API std::ostream& forceFlush(std::ostream& os);
174 
176 LUNCHBOX_API std::ostream& disableHeader(std::ostream& os);
178 LUNCHBOX_API std::ostream& enableHeader(std::ostream& os);
179 
181 inline std::ostream& startBlock(std::ostream& os)
182 {
183  return os << indent << disableFlush << disableHeader;
184 }
186 inline std::ostream& stopBlock(std::ostream& os)
187 {
188  return os << enableHeader << enableFlush << exdent;
189 }
190 }
191 
193 #define LBERROR \
194  (lunchbox::Log::level >= lunchbox::LOG_ERROR) && \
195  lunchbox::Log::instance(__FILE__, __LINE__)
196 
197 #define LBWARN LBERROR
198 
199 #define LBINFO \
200  (lunchbox::Log::level >= lunchbox::LOG_INFO) && \
201  lunchbox::Log::instance(__FILE__, __LINE__)
202 
203 #define LBDEBUG \
204  (lunchbox::Log::level >= lunchbox::LOG_DEBUG) && \
205  lunchbox::Log::instance(__FILE__, __LINE__)
206 
207 #ifdef NDEBUG
208 #define LBVERB \
209  if (false) \
210  lunchbox::Log::instance(__FILE__, __LINE__)
211 #else
212 
213 #define LBVERB \
214  (lunchbox::Log::level >= lunchbox::LOG_VERB) && \
215  lunchbox::Log::instance(__FILE__, __LINE__)
216 #endif
217 
222 #define LBLOG(topic) \
223  (lunchbox::Log::topics & (topic)) && \
224  lunchbox::Log::instance(__FILE__, __LINE__)
225 
230 #define LBTHROW(exc) \
231  { \
232  LBDEBUG << exc.what() << std::endl; \
233  throw exc; \
234  }
235 
236 #endif // LUNCHBOX_LOG_H
std::ostream & enableFlush(std::ostream &os)
Re-enable flushing of the Log stream.
The logging class.
Definition: log.h:72
std::ostream & exdent(std::ostream &os)
Decrease the indentation level of the Log stream.
Defines export visibility macros for library Lunchbox.
Basic type definitions not provided by the operating system.
bool exit()
De-initialize the Lunchbox base classes.
Be noisy.
Definition: log.h:48
A class for time measurements.
Definition: clock.h:33
static int level
The current log level.
Definition: log.h:97
LogTopic
The logging topics.
Definition: log.h:53
LogLevel
The logging levels.
Definition: log.h:42
Output informational messages.
Definition: log.h:46
std::ostream & enableHeader(std::ostream &os)
Re-enable printing of the Log header.
std::ostream & stopBlock(std::ostream &os)
Exdent, denable flush and header to stop block print.
Definition: log.h:186
Log all Lunchbox topics.
Definition: log.h:58
std::ostream & disableHeader(std::ostream &os)
Disable printing of the Log header for subsequent lines.
Output critical errors and warnings.
Definition: log.h:44
Output debugging information.
Definition: log.h:47
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:29
std::ostream & forceFlush(std::ostream &os)
Flush the Log stream regardless of the auto-flush state.
std::ostream & indent(std::ostream &os)
Increases the indentation level of the Log stream, causing subsequent lines to be intended by four ch...
Log potential bugs.
Definition: log.h:56
Log exception within LBTHROW.
Definition: log.h:55
static unsigned topics
The current log topics.
Definition: log.h:100
Log topics for other namespaces start here.
Definition: log.h:57
void perThreadDelete(T *object)
Default PerThread destructor deleting the object.
Definition: perThread.h:34
std::ostream & startBlock(std::ostream &os)
Indent, disable flush and header for block printing.
Definition: log.h:181
std::ostream & disableFlush(std::ostream &os)
Disable flushing of the Log stream.