Lunchbox  1.9.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
log.h
Go to the documentation of this file.
1 
2 /* Copyright (c) 2005-2014, Stefan Eilemann <eile@equalizergraphics.com>
3  * 2011, 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,
49  LOG_ALL
50 };
51 
59 {
60  LOG_EXCEPTION = 0x01,
61  LOG_PLUGIN = 0x02,
62  LOG_CUSTOM = 0x10,
63  LOG_ANY = 0xffffu
64 };
65 
66 namespace detail { class Log; }
67 
74 class Log : public std::ostream
75 {
76 public:
78  LUNCHBOX_API void indent();
79 
81  LUNCHBOX_API void exdent();
82 
84  LUNCHBOX_API void disableFlush();
85 
87  LUNCHBOX_API void enableFlush();
88 
90  LUNCHBOX_API void forceFlush();
91 
93  LUNCHBOX_API void disableHeader();
94 
96  LUNCHBOX_API void enableHeader();
97 
99  static LUNCHBOX_API int level;
100 
102  static LUNCHBOX_API unsigned topics;
103 
105  static LUNCHBOX_API Log& instance();
106 
108  static LUNCHBOX_API Log& instance( const char* file, const int line );
109 
111  static LUNCHBOX_API void exit();
112 
114  static LUNCHBOX_API void reset();
115 
117  static std::string& getLogLevelString();
118 
120  static LUNCHBOX_API int getLogLevel( const char* level );
121 
123  static LUNCHBOX_API void setOutput( std::ostream& stream );
124 
126  static LUNCHBOX_API bool setOutput( const std::string& file );
127 
129  static LUNCHBOX_API std::ostream& getOutput ();
130 
139  static LUNCHBOX_API void setClock( Clock* clock );
140 
141  static const Clock& getClock();
142 
143  LUNCHBOX_API void setThreadName( const std::string& name );
144  LUNCHBOX_API const std::string& getThreadName() const;
145 
146 private:
147  detail::Log* const impl_;
148 
149  Log();
150 
151  template< class T > friend void perThreadDelete( T* );
152  virtual ~Log();
153 
154  Log( const Log& );
155  Log& operator = ( const Log& );
156 
157  void setLogInfo( const char* file, const int line );
158 };
159 
165 LUNCHBOX_API std::ostream& indent( std::ostream& os );
167 LUNCHBOX_API std::ostream& exdent( std::ostream& os );
168 
170 LUNCHBOX_API std::ostream& disableFlush( std::ostream& os );
172 LUNCHBOX_API std::ostream& enableFlush( std::ostream& os );
174 LUNCHBOX_API std::ostream& forceFlush( std::ostream& os );
175 
177 LUNCHBOX_API std::ostream& disableHeader( std::ostream& os );
179 LUNCHBOX_API std::ostream& enableHeader( std::ostream& os );
180 
182 inline std::ostream& startBlock( std::ostream& os )
183  { return os << indent << disableFlush << disableHeader; }
185 inline std::ostream& stopBlock( std::ostream& os )
186  { return os << enableHeader << enableFlush << exdent; }
187 
188 }
189 
191 #define LBERROR (lunchbox::Log::level >= lunchbox::LOG_ERROR) && \
192  lunchbox::Log::instance( __FILE__, __LINE__ )
193 
194 #define LBWARN (lunchbox::Log::level >= lunchbox::LOG_WARN) && \
195  lunchbox::Log::instance( __FILE__, __LINE__ )
196 
197 #define LBINFO (lunchbox::Log::level >= lunchbox::LOG_INFO) && \
198  lunchbox::Log::instance( __FILE__, __LINE__ )
199 
200 #ifdef NDEBUG
201 # define LBVERB if( false ) \
202  lunchbox::Log::instance( __FILE__, __LINE__ )
203 #else
204 
205 # define LBVERB (lunchbox::Log::level >= lunchbox::LOG_VERB) && \
206  lunchbox::Log::instance( __FILE__, __LINE__ )
207 #endif
208 
213 #define LBLOG(topic) (lunchbox::Log::topics & (topic)) && \
214  lunchbox::Log::instance( __FILE__, __LINE__ )
215 
220 #define LBTHROW(exc) \
221  { \
222  LBLOG(lunchbox::LOG_EXCEPTION) << exc.what() << std::endl; \
223  throw exc; \
224  }
225 
226 #endif //LUNCHBOX_LOG_H
LUNCHBOX_API void enableFlush()
Re-enable flushing of the log on endl.
The logging class.
Definition: log.h:74
LUNCHBOX_API std::ostream & forceFlush(std::ostream &os)
Flush the Log stream regardless of the auto-flush state.
Defines export visibility macros for 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:48
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:58
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:185
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:63
Output critical errors.
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
static LUNCHBOX_API Log & instance()
The per-thread logger.
static LUNCHBOX_API int level
The current log level.
Definition: log.h:99
LUNCHBOX_API std::ostream & exdent(std::ostream &os)
Decrease the indentation level of the Log stream.
Log plugin handling.
Definition: log.h:61
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 exception within LBTHROW.
Definition: log.h:60
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:102
LUNCHBOX_API void exdent()
Un-indent subsequent log output by one level.
Output potentially critical warnings.
Definition: log.h:46
LUNCHBOX_API void forceFlush()
Flush all buffered log data.
Log topics for other namespaces start here.
Definition: log.h:62
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:182
static LUNCHBOX_API int getLogLevel(const char *level)