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.cpp
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 #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  WORD wAttributes;
50  ScopedColor(WORD c)
51  {
52  CONSOLE_SCREEN_BUFFER_INFO screen_info;
53  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
54  GetConsoleScreenBufferInfo(
55  hConsole,
56  &screen_info
57  );
58  wAttributes = screen_info.wAttributes;
59  SetConsoleTextAttribute(hConsole, c);
60  }
61  ~ScopedColor()
62  {
63  // restore the color
64  HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE);
65  SetConsoleTextAttribute(hConsole, wAttributes );
66  }
67  };
68  #define SET_TEXT_COLOR_YELLOW() ScopedColor scoped_color(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY);
69  #define SET_TEXT_COLOR_RED() ScopedColor scoped_color(FOREGROUND_RED|FOREGROUND_INTENSITY);
70  #define SET_TEXT_COLOR_PURPLE() ScopedColor scoped_color(FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY);
71  #define SET_TEXT_COLOR_GREEN() ScopedColor scoped_color(FOREGROUND_GREEN|FOREGROUND_INTENSITY);
72  #define SET_TEXT_COLOR_BLUE() ScopedColor scoped_color(FOREGROUND_BLUE|FOREGROUND_INTENSITY);
73 #else
74  struct ScopedColor
75  {
76  ScopedColor(const char* color)
77  {
78  //30 black foreground
79  //31 red foreground
80  //32 green foreground
81  //33 brown foreground
82  //34 blue foreground
83  //35 magenta (purple) foreground
84  //36 cyan (light blue) foreground
85  //37 gray foreground
86 
87  //40 black background
88  //41 red background
89  //42 green background
90  //43 brown background
91  //44 blue background
92  //45 magenta background
93  //46 cyan background
94  //47 white background
95 
96  //0 reset all attributes to their defaults
97  //1 set bold
98  //5 set blink
99  //7 set reverse video
100  //22 set normal intensity
101  //25 blink off
102  //27 reverse video off
103 
104  // example:
105  // "\033[34mThis is blue.\033[0m"
106  // "\033[45;37mGrey on purple.\033[0m"
107 
108  printf("%s", color);
109  }
110  ~ScopedColor()
111  {
112  // restore normal color
113  printf("%s", "\033[0m");
114  }
115  };
116  #define SET_TEXT_COLOR_YELLOW() ScopedColor scoped_color("\033[1;33m");
117  #define SET_TEXT_COLOR_RED() ScopedColor scoped_color("\033[31m");
118  #define SET_TEXT_COLOR_PURPLE() ScopedColor scoped_color("\033[1;31m");
119  #define SET_TEXT_COLOR_GREEN() ScopedColor scoped_color("\033[1;32m");
120  #define SET_TEXT_COLOR_BLUE() ScopedColor scoped_color("\033[1;34m");
121 #endif
122 }
123 //-----------------------------------------------------------------------------
124 // Log
125 //-----------------------------------------------------------------------------
126 void Log::notify(const String& log)
127 {
129  ScopedMutex mutex(Log::logMutex());
130 
132  if(defLogger() && globalSettings()->verbosityLevel() != vl::VEL_VERBOSITY_SILENT)
134 }
135 //-----------------------------------------------------------------------------
136 void Log::print(const String& log)
137 {
139  ScopedMutex mutex(Log::logMutex());
140 
141  if(defLogger() && globalSettings()->verbosityLevel() != vl::VEL_VERBOSITY_SILENT)
143 }
144 //-----------------------------------------------------------------------------
145 void Log::debug(const String& log)
146 {
148  ScopedMutex mutex(Log::logMutex());
149 
151  if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_DEBUG)
153 }
154 //-----------------------------------------------------------------------------
155 void Log::warning(const String& log)
156 {
158  ScopedMutex mutex(Log::logMutex());
159 
161  if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR)
163 }
164 //-----------------------------------------------------------------------------
165 void Log::error(const String& log)
166 {
168  ScopedMutex mutex(Log::logMutex());
169 
171  if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR)
173 }
174 //-----------------------------------------------------------------------------
175 void Log::bug(const String& log)
176 {
178  ScopedMutex mutex(Log::logMutex());
179 
181  if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR)
183 }
184 //------------------------------------------------------------------------------
185 void vl::log_failed_check(const char* expr, const char* file, int line)
186 {
187  VL_LOG_ERROR << "Condition '" << expr << "' failed at " << file << ":" << line << "\n";
188  fflush(stdout);
189  fflush(stderr);
190 
191  #if defined(VL_PLATFORM_WINDOWS) && VL_MESSAGEBOX_CHECK == 1
192  String msg = Say("Condition \"%s\" failed.\n\n%s:%n\n") << expr << file << line;
193  MessageBox(NULL, (wchar_t*)msg.ptr(), L"Visualization Library Debug", MB_OK | MB_ICONEXCLAMATION);
194  #endif
195 }
196 //-----------------------------------------------------------------------------
197 // Log mutex.
198 //-----------------------------------------------------------------------------
199 IMutex* Log::mLogMutex = NULL;
200 //-----------------------------------------------------------------------------
201 // StandardLog
202 //-----------------------------------------------------------------------------
204 {
205  mLogFile = file;
206 
207  if (mFile.is_open())
208  mFile.close();
209 
210  if (!file.empty())
211  mFile.open(file.toStdString().c_str());
212 }
213 //-----------------------------------------------------------------------------
215 {
216  if (log.empty())
217  return;
218 
219  std::string stdstr = log.toStdString();
220  std::cout << stdstr << std::flush;
221 
222  if (mFile.is_open())
223  mFile << stdstr << std::flush;
224 }
225 //-----------------------------------------------------------------------------
static void debug(const String &message)
Use this function to provide extra information useful to investigate and solve problems.
Definition: Log.cpp:145
virtual void printImplementation(ELogLevel level, const String &message)
Definition: Log.cpp:214
#define SET_TEXT_COLOR_RED()
Definition: Log.cpp:117
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:155
< 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:120
void setLogFile(const String &file)
Definition: Log.cpp:203
VLCORE_EXPORT void log_failed_check(const char *, const char *, int)
Definition: Log.cpp:185
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:165
const wchar_t * ptr() const
Returns the 0-terminated utf16 string.
Definition: String.hpp:127
T log(T a)
Definition: glsl_math.hpp:486
#define SET_TEXT_COLOR_YELLOW()
Definition: Log.cpp:116
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:175
#define SET_TEXT_COLOR_PURPLE()
Definition: Log.cpp:118
static void notify(const String &message)
Important application message for the user.
Definition: Log.cpp:126
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:136
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:119
< 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:220
virtual void printImplementation(ELogLevel level, const String &message)=0
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: pimpl.cpp:71
VLCORE_EXPORT GlobalSettings * globalSettings()
Returns VisulizationLibrary&#39;s global settings.
Definition: pimpl.cpp:52