Visualization Library 2.0.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Log.hpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #ifndef Log_INCLUDE_ONCE
33 #define Log_INCLUDE_ONCE
34 
35 #include <vlCore/String.hpp>
36 #include <vlCore/IMutex.hpp>
37 #include <fstream>
38 
39 namespace vl
40 {
41  //------------------------------------------------------------------------------
42  // Log
43  //-----------------------------------------------------------------------------
45  class VLCORE_EXPORT Log: public Object
46  {
48 
49  public:
50  Log()
51  {
52  VL_DEBUG_SET_OBJECT_NAME()
53  mLogLevel = LL_LogPrint; // only used by operator<<()
54  }
55 
56  Log& operator<<( ELogLevel log_level ) { mLogLevel = log_level; return *this; }
57 
58  Log& operator<<( const String& str )
59  {
60  switch(mLogLevel)
61  {
62  case LL_LogNotify: notify(str); break;
63  case LL_LogPrint: print(str); break;
64  case LL_LogBug: bug(str); break;
65  case LL_LogError: error(str); break;
66  case LL_LogWarning: warning(str); break;
67  case LL_LogDebug: debug(str); break;
68  }
69  return *this;
70  }
71 
72  Log& operator<<( const std::string& v )
73  {
74  *this << String::fromStdString(v);
75  return *this;
76  }
77 
78  Log& operator<<( const void* v )
79  {
80  *this << String::fromPointer(v);
81  return *this;
82  }
83 
84  Log& operator<<( const char* v )
85  {
86  *this << String(v);
87  return *this;
88  }
89 
90  Log& operator<<( char v )
91  {
92  *this << String(v);
93  return *this;
94  }
95 
96  Log& operator<<( unsigned char v )
97  {
98  *this << String::fromUInt(v);
99  return *this;
100  }
101 
102  Log& operator<<( short v )
103  {
104  *this << String::fromInt(v);
105  return *this;
106  }
107 
108  Log& operator<<( unsigned short v )
109  {
110  *this << String::fromUInt(v);
111  return *this;
112  }
113 
114  Log& operator<<( int v )
115  {
116  *this << String::fromInt(v);
117  return *this;
118  }
119 
120  Log& operator<<( unsigned int v )
121  {
122  *this << String::fromUInt(v);
123  return *this;
124  }
125 
126  Log& operator<<( long v )
127  {
128  *this << String::fromLongLong(v);
129  return *this;
130  }
131 
132  Log& operator<<( unsigned long v )
133  {
134  *this << String::fromULongLong(v);
135  return *this;
136  }
137 
138  Log& operator<<( long long v )
139  {
140  *this << String::fromLongLong(v);
141  return *this;
142  }
143 
144  Log& operator<<( unsigned long long v )
145  {
146  *this << String::fromULongLong(v);
147  return *this;
148  }
149 
150  Log& operator<<( double v )
151  {
152  *this << String::fromDouble(v);
153  return *this;
154  }
155 
156  Log& operator<<( float v )
157  {
158  *this << String::fromDouble(v);
159  return *this;
160  }
161 
162  protected:
163  virtual void printImplementation(ELogLevel level, const String& message) = 0;
164 
165  ELogLevel mLogLevel; // only used by operator<<()
166 
167  // --- static methods ---
168 
169  public:
172  static void setLogMutex(IMutex* mutex) { mLogMutex = mutex; }
173 
175  static IMutex* logMutex() { return mLogMutex; }
176 
180  static void notify(const String& message);
181 
184  static void print(const String& message);
185 
188  static void debug(const String& message);
189 
192  static void warning(const String& message);
193 
196  static void error(const String& message);
197 
200  static void bug(const String& message);
201 
202  private:
203  static IMutex* mLogMutex;
204  };
205 
206  //-----------------------------------------------------------------------------
207  // Default logger
208  //-----------------------------------------------------------------------------
210  VLCORE_EXPORT void setDefLogger(Log* logger);
211 
214 
215  // Log macros
216  #define VL_LOG (*::vl::defLogger())
217  #define VL_LOG_NOTIFY (VL_LOG << ::vl::LL_LogNotify)
218  #define VL_LOG_PRINT (VL_LOG << ::vl::LL_LogPrint)
219  #define VL_LOG_BUG (VL_LOG << ::vl::LL_LogBug)
220  #define VL_LOG_ERROR (VL_LOG << ::vl::LL_LogError)
221  #define VL_LOG_WARNING (VL_LOG << ::vl::LL_LogWarning)
222  #define VL_LOG_DEBUG (VL_LOG << ::vl::LL_LogDebug)
223 
224  //-----------------------------------------------------------------------------
225  // StandardLog
226  //-----------------------------------------------------------------------------
229  {
231 
232  public:
233  void setLogFile(const String& file);
234  const String& logFile() const { return mLogFile; }
235 
236  protected:
237  virtual void printImplementation(ELogLevel level, const String& message);
239  std::ofstream mFile;
240  };
241 }
242 
243 #endif
Utility class to generate logs.
Definition: Log.hpp:45
ELogLevel mLogLevel
Definition: Log.hpp:165
Log & operator<<(ELogLevel log_level)
Definition: Log.hpp:56
static String fromUInt(unsigned int value)
Creates a string representing the given unsigned integer value.
Definition: String.cpp:1088
Log & operator<<(double v)
Definition: Log.hpp:150
Log & operator<<(const String &str)
Definition: Log.hpp:58
Log & operator<<(long v)
Definition: Log.hpp:126
static String fromLongLong(long long value)
Creates a string representing the given long long value.
Definition: String.cpp:1096
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
std::ofstream mFile
Definition: Log.hpp:239
Log & operator<<(const void *v)
Definition: Log.hpp:78
Log & operator<<(unsigned int v)
Definition: Log.hpp:120
Log & operator<<(float v)
Definition: Log.hpp:156
static String fromInt(int value)
Creates a string representing the given integer value.
Definition: String.cpp:1080
Log & operator<<(char v)
Definition: Log.hpp:90
#define VL_INSTRUMENT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:122
Log & operator<<(unsigned short v)
Definition: Log.hpp:108
Visualization Library main namespace.
Log & operator<<(unsigned char v)
Definition: Log.hpp:96
static String fromStdString(const std::string &str, bool utf8=true)
Initializes the string from a std::string using fromUTF() if utf8 == true (default) otherwise uses fr...
Definition: String.cpp:881
Log & operator<<(short v)
Definition: Log.hpp:102
The base class for all the reference counted objects.
Definition: Object.hpp:158
An interface to implement simple platform-independent mutexes used to protect critical sections...
Definition: IMutex.hpp:44
static String fromPointer(const void *value)
Creates a string representing the given pointer.
Definition: String.cpp:1072
Log & operator<<(unsigned long v)
Definition: Log.hpp:132
Log()
Definition: Log.hpp:50
The StandardLog class outputs the log messages on the stdout device and optionally also on a specifie...
Definition: Log.hpp:228
static String fromULongLong(unsigned long long value)
Creates a string representing the given unsigned long long value.
Definition: String.cpp:1104
Log & operator<<(int v)
Definition: Log.hpp:114
Log & operator<<(unsigned long long v)
Definition: Log.hpp:144
#define VL_INSTRUMENT_ABSTRACT_CLASS(ClassName, BaseClass)
Definition: TypeInfo.hpp:145
String mLogFile
Definition: Log.hpp:238
static IMutex * logMutex()
The mutex used to synchronize concurrent calls to the log functions.
Definition: Log.hpp:175
const String & logFile() const
Definition: Log.hpp:234
VLCORE_EXPORT void setDefLogger(Log *logger)
Installs the default logger used by Visualization Library. Setting this to NULL will disable logging...
Definition: pimpl.cpp:67
static String fromDouble(double value, int decimals=6)
Creates a string representing the given double value The value of &#39;decimals&#39; can be between 0 and 20...
Definition: String.cpp:1112
Log & operator<<(long long v)
Definition: Log.hpp:138
Log & operator<<(const std::string &v)
Definition: Log.hpp:72
static void setLogMutex(IMutex *mutex)
The mutex used to synchronize concurrent calls to the log functions.
Definition: Log.hpp:172
VLCORE_EXPORT Log * defLogger()
Returns the currently installed default logger.
Definition: pimpl.cpp:71
Log & operator<<(const char *v)
Definition: Log.hpp:84