Visualization Library v1.0.3A lightweight C++ OpenGL middleware for 2D/3D graphics |
[Download] [Tutorials] [All Classes] [Grouped Classes] |
00001 /**************************************************************************************/ 00002 /* */ 00003 /* Visualization Library */ 00004 /* http://visualizationlibrary.org */ 00005 /* */ 00006 /* Copyright (c) 2005-2011, Michele Bosi */ 00007 /* All rights reserved. */ 00008 /* */ 00009 /* Redistribution and use in source and binary forms, with or without modification, */ 00010 /* are permitted provided that the following conditions are met: */ 00011 /* */ 00012 /* - Redistributions of source code must retain the above copyright notice, this */ 00013 /* list of conditions and the following disclaimer. */ 00014 /* */ 00015 /* - Redistributions in binary form must reproduce the above copyright notice, this */ 00016 /* list of conditions and the following disclaimer in the documentation and/or */ 00017 /* other materials provided with the distribution. */ 00018 /* */ 00019 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */ 00020 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ 00021 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */ 00022 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */ 00023 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */ 00024 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */ 00025 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */ 00026 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */ 00027 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */ 00028 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ 00029 /* */ 00030 /**************************************************************************************/ 00031 00032 #include <vlCore/Log.hpp> 00033 #include <vlCore/VisualizationLibrary.hpp> 00034 #include <vlCore/GlobalSettings.hpp> 00035 #include <vlCore/Vector3.hpp> 00036 #include <vlCore/Say.hpp> 00037 #include <vlCore/ScopedMutex.hpp> 00038 #include <vlCore/version.hpp> 00039 #include <cstdio> 00040 #include <cstdlib> 00041 #include <iostream> 00042 00043 using namespace vl; 00044 00045 namespace 00046 { 00047 #if defined(VL_PLATFORM_WINDOWS) 00048 struct ScopedColor 00049 { 00050 CONSOLE_SCREEN_BUFFER_INFO screen_info; 00051 WORD color; 00052 ScopedColor(WORD c): color(c) 00053 { 00054 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 00055 GetConsoleScreenBufferInfo( 00056 hConsole, 00057 &screen_info 00058 ); 00059 SetConsoleTextAttribute(hConsole, c); 00060 } 00061 ~ScopedColor() 00062 { 00063 // restore the color 00064 HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 00065 SetConsoleTextAttribute(hConsole,screen_info.wAttributes); 00066 } 00067 }; 00068 #define SET_TEXT_COLOR_YELLOW() ScopedColor set_scoped_color(FOREGROUND_RED|FOREGROUND_GREEN|FOREGROUND_INTENSITY); 00069 #define SET_TEXT_COLOR_RED() ScopedColor set_scoped_color(FOREGROUND_RED|FOREGROUND_INTENSITY); 00070 #define SET_TEXT_COLOR_PURPLE() ScopedColor set_scoped_color(FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_INTENSITY); 00071 #define SET_TEXT_COLOR_GREEN() ScopedColor set_scoped_color(FOREGROUND_GREEN|FOREGROUND_INTENSITY); 00072 #define SET_TEXT_COLOR_BLUE() ScopedColor set_scoped_color(FOREGROUND_BLUE|FOREGROUND_INTENSITY); 00073 #else 00074 struct ScopedColor 00075 { 00076 ScopedColor(const char* color) 00077 { 00078 //30 black foreground 00079 //31 red foreground 00080 //32 green foreground 00081 //33 brown foreground 00082 //34 blue foreground 00083 //35 magenta (purple) foreground 00084 //36 cyan (light blue) foreground 00085 //37 gray foreground 00086 00087 //40 black background 00088 //41 red background 00089 //42 green background 00090 //43 brown background 00091 //44 blue background 00092 //45 magenta background 00093 //46 cyan background 00094 //47 white background 00095 00096 //0 reset all attributes to their defaults 00097 //1 set bold 00098 //5 set blink 00099 //7 set reverse video 00100 //22 set normal intensity 00101 //25 blink off 00102 //27 reverse video off 00103 00104 // example: 00105 // "\033[34mThis is blue.\033[0m" 00106 // "\033[45;37mGrey on purple.\033[0m" 00107 00108 printf("%s", color); 00109 } 00110 ~ScopedColor() 00111 { 00112 // restore normal color 00113 printf("%s", "\033[0m"); 00114 } 00115 }; 00116 #define SET_TEXT_COLOR_YELLOW() ScopedColor set_scoped_color("\033[1;33m"); 00117 #define SET_TEXT_COLOR_RED() ScopedColor set_scoped_color("\033[31m"); 00118 #define SET_TEXT_COLOR_PURPLE() ScopedColor set_scoped_color("\033[1;31m"); 00119 #define SET_TEXT_COLOR_GREEN() ScopedColor set_scoped_color("\033[1;32m"); 00120 #define SET_TEXT_COLOR_BLUE() ScopedColor set_scoped_color("\033[1;34m"); 00121 #endif 00122 } 00123 //----------------------------------------------------------------------------- 00124 // Log 00125 //----------------------------------------------------------------------------- 00126 void Log::notify(const String& log) 00127 { 00129 ScopedMutex mutex(Log::logMutex()); 00130 00131 SET_TEXT_COLOR_GREEN() 00132 if(defLogger() && globalSettings()->verbosityLevel() != vl::VEL_VERBOSITY_SILENT) 00133 defLogger()->printImplementation(LL_LogNotify, log); 00134 } 00135 //----------------------------------------------------------------------------- 00136 void Log::print(const String& log) 00137 { 00139 ScopedMutex mutex(Log::logMutex()); 00140 00141 if(defLogger() && globalSettings()->verbosityLevel() != vl::VEL_VERBOSITY_SILENT) 00142 defLogger()->printImplementation(LL_LogPrint, log); 00143 } 00144 //----------------------------------------------------------------------------- 00145 void Log::debug(const String& log) 00146 { 00148 ScopedMutex mutex(Log::logMutex()); 00149 00150 SET_TEXT_COLOR_BLUE() 00151 if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_DEBUG) 00152 defLogger()->printImplementation(LL_LogDebug, log); 00153 } 00154 //----------------------------------------------------------------------------- 00155 void Log::warning(const String& log) 00156 { 00158 ScopedMutex mutex(Log::logMutex()); 00159 00160 SET_TEXT_COLOR_YELLOW() 00161 if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR) 00162 defLogger()->printImplementation(LL_LogWarning, log); 00163 } 00164 //----------------------------------------------------------------------------- 00165 void Log::error(const String& log) 00166 { 00168 ScopedMutex mutex(Log::logMutex()); 00169 00170 SET_TEXT_COLOR_RED() 00171 if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR) 00172 defLogger()->printImplementation(LL_LogError, log); 00173 } 00174 //----------------------------------------------------------------------------- 00175 void Log::bug(const String& log) 00176 { 00178 ScopedMutex mutex(Log::logMutex()); 00179 00180 SET_TEXT_COLOR_PURPLE() 00181 if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR) 00182 defLogger()->printImplementation(LL_LogBug, log); 00183 } 00184 //------------------------------------------------------------------------------ 00185 void Log::logSystemInfo() 00186 { 00187 #if defined(_MSC_VER) 00188 const char* compiler = "MSVC"; 00189 #elif defined(__GNUG__) 00190 const char* compiler = "GCC"; 00191 #else 00192 const char* compiler = "UNKNOWN"; 00193 #endif 00194 00195 #if defined(DEBUG) || !defined(NDEBUG) 00196 const char* build_type = "DEBUG"; 00197 #else 00198 const char* build_type = "RELEASE"; 00199 #endif 00200 00201 print( Say("Visualization Library v%s [%s]\n%s - %s - %s compiler [%s] [%s]\n") 00202 << VisualizationLibrary::versionString() 00203 << (sizeof(vec3) == sizeof(fvec3) ? "f32" : "f64") 00204 << __DATE__ << __TIME__ << compiler << build_type 00205 << (sizeof(void*) == 4 ? "x32" : "x64") ); 00206 00207 print("\n --- Environment ---\n"); 00208 const char* val = getenv("VL_LOGFILE_PATH"); 00209 if (val) 00210 print( Say("VL_LOGFILE_PATH = %s\n") << val ); 00211 else 00212 print("VL_LOGFILE_PATH <not present>\n"); 00213 00214 val = getenv("VL_DATA_PATH"); 00215 if (val) 00216 print( Say("VL_DATA_PATH = %s\n") << val ); 00217 else 00218 print("VL_DATA_PATH <not present>\n"); 00219 00220 val = getenv("VL_VERBOSITY_LEVEL"); 00221 if (val) 00222 print( Say("VL_VERBOSITY_LEVEL = %s\n") << val ); 00223 else 00224 print("VL_VERBOSITY_LEVEL <not present>\n"); 00225 00226 val = getenv("VL_CHECK_GL_STATES"); 00227 if (val) 00228 print( Say("VL_CHECK_GL_STATES = %s\n") << val ); 00229 else 00230 print("VL_CHECK_GL_STATES <not present>\n"); 00231 00232 print("\n --- Global Settings --- \n"); 00233 print( Say("Log file = %s\n") << globalSettings()->defaultLogPath() ); 00234 print( Say("Data path = %s\n") << globalSettings()->defaultDataPath() ); 00235 print("Verbosity level = "); 00236 switch(globalSettings()->verbosityLevel()) 00237 { 00238 /*case vl::VEL_VERBOSITY_SILENT: print("SILENT\n"); break;*/ 00239 case vl::VEL_VERBOSITY_ERROR: print("ERROR\n"); break; 00240 case vl::VEL_VERBOSITY_NORMAL: print("NORMAL\n"); break; 00241 case vl::VEL_VERBOSITY_DEBUG: print("DEBUG\n"); break; 00242 default: break; 00243 } 00244 print( Say("Check OpenGL States = %s\n") << (globalSettings()->checkOpenGLStates()?"YES":"NO") ); 00245 00246 print("\n"); 00247 } 00248 //------------------------------------------------------------------------------ 00249 void vl::log_failed_check(const char* expr, const char* file, int line) 00250 { 00251 VL_LOG_ERROR << "Condition '" << expr << "' failed at " << file << ":" << line << "\n"; 00252 fflush(stdout); 00253 fflush(stderr); 00254 00255 #if defined(VL_PLATFORM_WINDOWS) && VL_MESSAGEBOX_CHECK == 1 00256 String msg = Say("Condition \"%s\" failed.\n\n%s:%n\n") << expr << file << line; 00257 MessageBox(NULL, (wchar_t*)msg.ptr(), L"Visualization Library Debug", MB_OK | MB_ICONEXCLAMATION); 00258 #endif 00259 } 00260 //----------------------------------------------------------------------------- 00261 // Log mutex. 00262 //----------------------------------------------------------------------------- 00263 IMutex* Log::mLogMutex = NULL; 00264 //----------------------------------------------------------------------------- 00265 // StandardLog 00266 //----------------------------------------------------------------------------- 00267 void StandardLog::setLogFile(const String& file) 00268 { 00269 mLogFile = file; 00270 00271 if (mFile.is_open()) 00272 mFile.close(); 00273 00274 if (!file.empty()) 00275 mFile.open(file.toStdString().c_str()); 00276 } 00277 //----------------------------------------------------------------------------- 00278 void StandardLog::printImplementation(ELogLevel, const String& log) 00279 { 00280 if (log.empty()) 00281 return; 00282 00283 std::string stdstr = log.toStdString(); 00284 std::cout << stdstr << std::flush; 00285 00286 if (mFile.is_open()) 00287 mFile << stdstr << std::flush; 00288 } 00289 //-----------------------------------------------------------------------------