Visualization Library 2.0.0-b3

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

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
Log.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2011, 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 #include <vlCore/Log.hpp>
35 #include <vlCore/Vector3.hpp>
36 #include <vlCore/Say.hpp>
37 #include <vlCore/ScopedMutex.hpp>
38 #include <cstdio>
39 #include <cstdlib>
40 #include <iostream>
41 
42 using namespace vl;
43 
44 namespace
45 {
46 #if defined(VL_PLATFORM_WINDOWS)
47  struct ScopedColor
48  {
49  CONSOLE_SCREEN_BUFFER_INFO screen_info;
50  WORD color;
51  ScopedColor(WORD c): color(c)
52  {
53  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
54  GetConsoleScreenBufferInfo(
55  hConsole,
56  &screen_info
57  );
58  SetConsoleTextAttribute(hConsole, c);
59  }
60  ~ScopedColor()
61  {
62  // restore the color
63  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
64  SetConsoleTextAttribute(hConsole,screen_info.wAttributes);
65  }
66  };
67  #define SET_TEXT_COLOR_YELLOW() ScopedColor set_scoped_color(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
68  #define SET_TEXT_COLOR_RED() ScopedColor set_scoped_color(FOREGROUND_RED|FOREGROUND_INTENSITY);
69  #define SET_TEXT_COLOR_PURPLE() ScopedColor set_scoped_color(FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
70  #define SET_TEXT_COLOR_GREEN() ScopedColor set_scoped_color(FOREGROUND_GREEN|FOREGROUND_INTENSITY);
71  #define SET_TEXT_COLOR_BLUE() ScopedColor set_scoped_color(FOREGROUND_BLUE|FOREGROUND_INTENSITY);
72 #else
73  struct ScopedColor
74  {
75  ScopedColor(const char* color)
76  {
77  //30 black foreground
78  //31 red foreground
79  //32 green foreground
80  //33 brown foreground
81  //34 blue foreground
82  //35 magenta (purple) foreground
83  //36 cyan (light blue) foreground
84  //37 gray foreground
85 
86  //40 black background
87  //41 red background
88  //42 green background
89  //43 brown background
90  //44 blue background
91  //45 magenta background
92  //46 cyan background
93  //47 white background
94 
95  //0 reset all attributes to their defaults
96  //1 set bold
97  //5 set blink
98  //7 set reverse video
99  //22 set normal intensity
100  //25 blink off
101  //27 reverse video off
102 
103  // example:
104  // "\033[34mThis is blue.\033[0m"
105  // "\033[45;37mGrey on purple.\033[0m"
106 
107  printf("%s", color);
108  }
109  ~ScopedColor()
110  {
111  // restore normal color
112  printf("%s", "\033[0m");
113  }
114  };
115  #define SET_TEXT_COLOR_YELLOW() ScopedColor set_scoped_color("\033[1;33m");
116  #define SET_TEXT_COLOR_RED() ScopedColor set_scoped_color("\033[31m");
117  #define SET_TEXT_COLOR_PURPLE() ScopedColor set_scoped_color("\033[1;31m");
118  #define SET_TEXT_COLOR_GREEN() ScopedColor set_scoped_color("\033[1;32m");
119  #define SET_TEXT_COLOR_BLUE() ScopedColor set_scoped_color("\033[1;34m");
120 #endif
121 }
122 //-----------------------------------------------------------------------------
123 // Log
124 //-----------------------------------------------------------------------------
125 void Log::notify(const String& log)
126 {
128  ScopedMutex mutex(Log::logMutex());
129 
131  if(defLogger() && globalSettings()->verbosityLevel() != vl::VEL_VERBOSITY_SILENT)
133 }
134 //-----------------------------------------------------------------------------
135 void Log::print(const String& log)
136 {
138  ScopedMutex mutex(Log::logMutex());
139 
140  if(defLogger() && globalSettings()->verbosityLevel() != vl::VEL_VERBOSITY_SILENT)
142 }
143 //-----------------------------------------------------------------------------
144 void Log::debug(const String& log)
145 {
147  ScopedMutex mutex(Log::logMutex());
148 
150  if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_DEBUG)
152 }
153 //-----------------------------------------------------------------------------
154 void Log::warning(const String& log)
155 {
157  ScopedMutex mutex(Log::logMutex());
158 
160  if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR)
162 }
163 //-----------------------------------------------------------------------------
164 void Log::error(const String& log)
165 {
167  ScopedMutex mutex(Log::logMutex());
168 
170  if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR)
172 }
173 //-----------------------------------------------------------------------------
174 void Log::bug(const String& log)
175 {
177  ScopedMutex mutex(Log::logMutex());
178 
180  if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR)
182 }
183 //------------------------------------------------------------------------------
185 {
186  #if defined(_MSC_VER)
187  const char* compiler = "MSVC";
188  #elif defined(__GNUG__)
189  const char* compiler = "GCC";
190  #else
191  const char* compiler = "UNKNOWN";
192  #endif
193 
194  #if defined(DEBUG) || !defined(NDEBUG)
195  const char* build_type = "DEBUG";
196  #else
197  const char* build_type = "RELEASE";
198  #endif
199 
200  print( Say("Visualization Library v%s [%s]\n%s - %s - %s compiler [%s] [%s]\n")
202  << (sizeof(vec3) == sizeof(fvec3) ? "f32" : "f64")
203  << __DATE__ << __TIME__ << compiler << build_type
204  << (sizeof(void*) == 4 ? "x32" : "x64") );
205 
206  print("\n --- Environment ---\n");
207  const char* val = getenv("VL_LOGFILE_PATH");
208  if (val)
209  print( Say("VL_LOGFILE_PATH = %s\n") << val );
210  else
211  print("VL_LOGFILE_PATH <not present>\n");
212 
213  val = getenv("VL_DATA_PATH");
214  if (val)
215  print( Say("VL_DATA_PATH = %s\n") << val );
216  else
217  print("VL_DATA_PATH <not present>\n");
218 
219  val = getenv("VL_VERBOSITY_LEVEL");
220  if (val)
221  print( Say("VL_VERBOSITY_LEVEL = %s\n") << val );
222  else
223  print("VL_VERBOSITY_LEVEL <not present>\n");
224 
225  val = getenv("VL_CHECK_GL_STATES");
226  if (val)
227  print( Say("VL_CHECK_GL_STATES = %s\n") << val );
228  else
229  print("VL_CHECK_GL_STATES <not present>\n");
230 
231  print("\n --- Global Settings --- \n");
232  print( Say("Log file = %s\n") << globalSettings()->defaultLogPath() );
233  print( Say("Data path = %s\n") << globalSettings()->defaultDataPath() );
234  print("Verbosity level = ");
235  switch(globalSettings()->verbosityLevel())
236  {
237  /*case vl::VEL_VERBOSITY_SILENT: print("SILENT\n"); break;*/
238  case vl::VEL_VERBOSITY_ERROR: print("ERROR\n"); break;
239  case vl::VEL_VERBOSITY_NORMAL: print("NORMAL\n"); break;
240  case vl::VEL_VERBOSITY_DEBUG: print("DEBUG\n"); break;
241  default: break;
242  }
243  print( Say("Check OpenGL States = %s\n") << (globalSettings()->checkOpenGLStates()?"YES":"NO") );
244 
245  print("\n");
246 }
247 //------------------------------------------------------------------------------
248 void vl::log_failed_check(const char* expr, const char* file, int line)
249 {
250  VL_LOG_ERROR << "Condition '" << expr << "' failed at " << file << ":" << line << "\n";
251  fflush(stdout);
252  fflush(stderr);
253 
254  #if defined(VL_PLATFORM_WINDOWS) && VL_MESSAGEBOX_CHECK == 1
255  String msg = Say("Condition \"%s\" failed.\n\n%s:%n\n") << expr << file << line;
256  MessageBox(NULL, (wchar_t*)msg.ptr(), L"Visualization Library Debug", MB_OK | MB_ICONEXCLAMATION);
257  #endif
258 }
259 //-----------------------------------------------------------------------------
260 // Log mutex.
261 //-----------------------------------------------------------------------------
262 IMutex* Log::mLogMutex = NULL;
263 //-----------------------------------------------------------------------------
264 // StandardLog
265 //-----------------------------------------------------------------------------
266 void StandardLog::setLogFile(const String& file)
267 {
268  mLogFile = file;
269 
270  if (mFile.is_open())
271  mFile.close();
272 
273  if (!file.empty())
274  mFile.open(file.toStdString().c_str());
275 }
276 //-----------------------------------------------------------------------------
278 {
279  if (log.empty())
280  return;
281 
282  std::string stdstr = log.toStdString();
283  std::cout << stdstr << std::flush;
284 
285  if (mFile.is_open())
286  mFile << stdstr << std::flush;
287 }
288 //-----------------------------------------------------------------------------
static void debug(const String &message)
Use this function to provide extra information useful to investigate and solve problems.
Definition: Log.cpp:144
virtual void printImplementation(ELogLevel level, const String &message)
Definition: Log.cpp:277
#define SET_TEXT_COLOR_RED()
Definition: Log.cpp:116
static VLCORE_EXPORT const char * versionString()
Returns the Visualization Library&#39;s version string.
Definition: init_core.cpp:104
A simple String formatting class.
Definition: Say.hpp:124
static void warning(const String &message)
Use this function to provide information about situations that might lead to errors or loss of data...
Definition: Log.cpp:154
< No log information is generated.
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
#define SET_TEXT_COLOR_BLUE()
Definition: Log.cpp:119
void setLogFile(const String &file)
Definition: Log.cpp:266
VLCORE_EXPORT void log_failed_check(const char *, const char *, int)
Definition: Log.cpp:248
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:164
const wchar_t * ptr() const
Returns the 0-terminated utf16 string.
Definition: String.hpp:127
< Outputs normal information messages, plus all error messages.
T log(T a)
Definition: glsl_math.hpp:486
#define SET_TEXT_COLOR_YELLOW()
Definition: Log.cpp:115
Visualization Library main namespace.
static void bug(const String &message)
Use this function to provide information about programming errors: wrong parameter initialization...
Definition: Log.cpp:174
#define SET_TEXT_COLOR_PURPLE()
Definition: Log.cpp:117
static void notify(const String &message)
Important application message for the user.
Definition: Log.cpp:125
An interface to implement simple platform-independent mutexes used to protect critical sections...
Definition: IMutex.hpp:44
static void print(const String &message)
Application message for the user.
Definition: Log.cpp:135
A class that locks the specified mutex when constructed and unlocks it when destructed.
Definition: ScopedMutex.hpp:43
bool empty() const
Returns true if length() == 0.
Definition: String.hpp:136
#define NULL
Definition: OpenGLDefs.hpp:81
#define SET_TEXT_COLOR_GREEN()
Definition: Log.cpp:118
< Outputs critical and runtime error messages.
static IMutex * logMutex()
The mutex used to synchronize concurrent calls to the log functions.
Definition: Log.hpp:175
< Outputs extra information messages useful for debugging, plus all normal and error messages...
#define VL_LOG_ERROR
Definition: Log.hpp:223
virtual void printImplementation(ELogLevel level, const String &message)=0
static void logSystemInfo()
Logs VL and system information.
Definition: Log.cpp:184
std::string toStdString() const
Returns a UTF8 encoded std::string.
Definition: String.cpp:1156
VLCORE_EXPORT Log * defLogger()
Returns the currently installed default logger.
Definition: init_core.cpp:127
VLCORE_EXPORT GlobalSettings * globalSettings()
Returns VisulizationLibrary&#39;s global settings.
Definition: init_core.cpp:112