Lunchbox  1.12.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-2015, 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 {
41 
44 {
45  LOG_ERROR = 1,
50  LOG_ALL
51 };
52 
55 {
56  LOG_EXCEPTION = 0x01,
57  LOG_BUG = 0x04,
58  LOG_CUSTOM = 0x10,
59  LOG_ANY = 0xffffu
60 };
61 
62 namespace detail { class Log; }
63 
70 class Log : public std::ostream
71 {
72 public:
74  LUNCHBOX_API void indent();
75 
77  LUNCHBOX_API void exdent();
78 
80  LUNCHBOX_API void disableFlush();
81 
83  LUNCHBOX_API void enableFlush();
84 
86  LUNCHBOX_API void forceFlush();
87 
89  LUNCHBOX_API void disableHeader();
90 
92  LUNCHBOX_API void enableHeader();
93 
95  static LUNCHBOX_API int level;
96 
98  static LUNCHBOX_API unsigned topics;
99 
101  static LUNCHBOX_API Log& instance();
102 
104  static LUNCHBOX_API Log& instance( const char* file, const int line );
105 
107  static LUNCHBOX_API void exit();
108 
110  static LUNCHBOX_API void reset();
111 
113  static std::string& getLogLevelString();
114 
116  static LUNCHBOX_API int getLogLevel( const char* level );
117 
119  static LUNCHBOX_API void setOutput( std::ostream& stream );
120 
122  static LUNCHBOX_API bool setOutput( const std::string& file );
123 
125  static LUNCHBOX_API std::ostream& getOutput ();
126 
135  static LUNCHBOX_API void setClock( Clock* clock );
136 
137  static const Clock& getClock();
138 
139  LUNCHBOX_API void setThreadName( const std::string& name );
140  LUNCHBOX_API const std::string& getThreadName() const;
141 
142 private:
143  detail::Log* const impl_;
144 
145  Log();
146 
147  template< class T > friend void perThreadDelete( T* );
148  virtual ~Log();
149 
150  Log( const Log& );
151  Log& operator = ( const Log& );
152 
153  void setLogInfo( const char* file, const int line );
154 };
155 
161 LUNCHBOX_API std::ostream& indent( std::ostream& os );
163 LUNCHBOX_API std::ostream& exdent( std::ostream& os );
164 
166 LUNCHBOX_API std::ostream& disableFlush( std::ostream& os );
168 LUNCHBOX_API std::ostream& enableFlush( std::ostream& os );
170 LUNCHBOX_API std::ostream& forceFlush( std::ostream& os );
171 
173 LUNCHBOX_API std::ostream& disableHeader( std::ostream& os );
175 LUNCHBOX_API std::ostream& enableHeader( std::ostream& os );
176 
178 inline std::ostream& startBlock( std::ostream& os )
179  { return os << indent << disableFlush << disableHeader; }
181 inline std::ostream& stopBlock( std::ostream& os )
182  { return os << enableHeader << enableFlush << exdent; }
183 
184 }
185 
187 #define LBERROR (lunchbox::Log::level >= lunchbox::LOG_ERROR) && \
188  lunchbox::Log::instance( __FILE__, __LINE__ )
189 
190 #define LBWARN LBERROR
191 
192 #define LBINFO (lunchbox::Log::level >= lunchbox::LOG_INFO) && \
193  lunchbox::Log::instance( __FILE__, __LINE__ )
194 
195 #define LBDEBUG (lunchbox::Log::level >= lunchbox::LOG_DEBUG) && \
196  lunchbox::Log::instance( __FILE__, __LINE__ )
197 
198 #ifdef NDEBUG
199 # define LBVERB if( false ) \
200  lunchbox::Log::instance( __FILE__, __LINE__ )
201 #else
202 
203 # define LBVERB (lunchbox::Log::level >= lunchbox::LOG_VERB) && \
204  lunchbox::Log::instance( __FILE__, __LINE__ )
205 #endif
206 
211 #define LBLOG(topic) (lunchbox::Log::topics & (topic)) && \
212  lunchbox::Log::instance( __FILE__, __LINE__ )
213 
218 #define LBTHROW(exc) \
219  { \
220  LBDEBUG << exc.what() << std::endl; \
221  throw exc; \
222  }
223 
224 #endif //LUNCHBOX_LOG_H
LUNCHBOX_API void enableFlush()
Re-enable flushing of the log on endl.
The logging class.
Definition: log.h:70
LUNCHBOX_API std::ostream & forceFlush(std::ostream &os)
Flush the Log stream regardless of the auto-flush state.
Defines export visibility macros for library Lunchbox.
static LUNCHBOX_API void setOutput(std::ostream &stream)
Change the output stream.
Basic type definitions not provided by the operating system.
static std::string & getLogLevelString()
The string representation of the current log level.
Be noisy.
Definition: log.h:49
A class for time measurements.
Definition: clock.h:31
static LUNCHBOX_API std::ostream & getOutput()
Get the current output stream.
LogTopic
The logging topics.
Definition: log.h:54
LogLevel
The logging levels.
Definition: log.h:43
static LUNCHBOX_API void exit()
Exit the log instance for the current thread.
Output informational messages.
Definition: log.h:47
LUNCHBOX_API std::ostream & enableFlush(std::ostream &os)
Re-enable flushing of the Log stream.
std::ostream & stopBlock(std::ostream &os)
Exdent, denable flush and header to stop block print.
Definition: log.h:181
LUNCHBOX_API std::ostream & disableHeader(std::ostream &os)
Disable printing of the Log header for subsequent lines.
LUNCHBOX_API std::ostream & enableHeader(std::ostream &os)
Re-enable printing of the Log header.
Log all Lunchbox topics.
Definition: log.h:59
Output critical errors and warnings.
Definition: log.h:45
LUNCHBOX_API void disableFlush()
Disable flushing of the log on endl.
LUNCHBOX_API void disableHeader()
Disable prefix printing for subsequent new lines.
LUNCHBOX_API void enableHeader()
Re-enable prefix printing for subsequent new lines.
LUNCHBOX_API void indent()
Indent subsequent log output by one level.
friend void perThreadDelete(T *)
Default PerThread destructor deleting the object.
Definition: perThread.h:29
Output debugging information.
Definition: log.h:48
static LUNCHBOX_API Log & instance()
The per-thread logger.
static LUNCHBOX_API int level
The current log level.
Definition: log.h:95
LUNCHBOX_API std::ostream & exdent(std::ostream &os)
Decrease the indentation level of the Log stream.
Abstraction layer and common utilities for multi-threaded programming.
Definition: algorithm.h:32
LUNCHBOX_API 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:57
Log exception within LBTHROW.
Definition: log.h:56
LUNCHBOX_API std::ostream & disableFlush(std::ostream &os)
Disable flushing of the Log stream.
static LUNCHBOX_API unsigned topics
The current log topics.
Definition: log.h:98
LUNCHBOX_API void exdent()
Un-indent subsequent log output by one level.
LUNCHBOX_API void forceFlush()
Flush all buffered log data.
Log topics for other namespaces start here.
Definition: log.h:58
static LUNCHBOX_API void setClock(Clock *clock)
Set the reference clock.
std::ostream & startBlock(std::ostream &os)
Indent, disable flush and header for block printing.
Definition: log.h:178
static LUNCHBOX_API int getLogLevel(const char *level)