# Visualization Library

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

[Home] [Tutorials] [All Classes] [Grouped Classes]

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# D:/VL/BuildSystem/VL_DISTRO/Visualization_Library_SDK/src/vlCore/Log.cpp

Go to the documentation of this file.

    00001 /**************************************************************************************/
    00002 /*                                                                                    */
    00003 /*  Visualization Library                                                             */
    00004 /*  http://www.visualizationlibrary.com                                               */
    00005 /*                                                                                    */
    00006 /*  Copyright (c) 2005-2010, 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/checks.hpp>
    00034 #include <vlCore/VLSettings.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 #ifdef _WIN32
    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 #else
    00072   struct ScopedColor
    00073   {
    00074     ScopedColor(const char* color)
    00075     {
    00076       //30 black foreground
    00077       //31 red foreground
    00078       //32 green foreground
    00079       //33 brown foreground
    00080       //34 blue foreground
    00081       //35 magenta (purple) foreground
    00082       //36 cyan (light blue) foreground
    00083       //37 gray foreground
    00084
    00085       //40 black background
    00086       //41 red background
    00087       //42 green background
    00088       //43 brown background
    00089       //44 blue background
    00090       //45 magenta background
    00091       //46 cyan background
    00092       //47 white background
    00093
    00094       //0 reset all attributes to their defaults
    00095       //1 set bold
    00096       //5 set blink
    00097       //7 set reverse video
    00098       //22 set normal intensity
    00099       //25 blink off
    00100       //27 reverse video off
    00101
    00102       // example:
    00103       // "\033[34mThis is blue.\033[0m"
    00104       // "\033[45;37mGrey on purple.\033[0m"
    00105
    00106       puts(color);
    00107     }
    00108     ~ScopedColor()
    00109     {
    00110       // restore normal color
    00111       puts("\033[0m");
    00112     }
    00113   };
    00114   #define SET_TEXT_COLOR_YELLOW() ScopedColor set_scoped_color("\033[1;33m");
    00115   #define SET_TEXT_COLOR_RED()    ScopedColor set_scoped_color("\033[31m");
    00116   #define SET_TEXT_COLOR_PURPLE() ScopedColor set_scoped_color("\033[1;31m");
    00117 #endif
    00118 }
    00119
    00120 //-----------------------------------------------------------------------------
    00121 // Log
    00122 //-----------------------------------------------------------------------------
    00123 void Log::print(const String& log)
    00124 {
    00126   ScopedMutex mutex(Log::logMutex());
    00127
    00128   if(defLogger() && globalSettings()->verbosityLevel() != vl::VEL_VERBOSITY_SILENT)
    00129     defLogger()->printImplementation(LogNormal, log);
    00130 }
    00131 //-----------------------------------------------------------------------------
    00132 void Log::debug(const String& log)
    00133 {
    00135   ScopedMutex mutex(Log::logMutex());
    00136
    00137   if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_DEBUG)
    00138     defLogger()->printImplementation(LogDebug, log);
    00139 }
    00140 //-----------------------------------------------------------------------------
    00141 void Log::info(const String& log)
    00142 {
    00144   ScopedMutex mutex(Log::logMutex());
    00145
    00146   if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_NORMAL)
    00147     defLogger()->printImplementation(LogInfo, log);
    00148 }
    00149 //-----------------------------------------------------------------------------
    00150 void Log::warning(const String& log)
    00151 {
    00153   ScopedMutex mutex(Log::logMutex());
    00154
    00155   SET_TEXT_COLOR_YELLOW()
    00156   if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR)
    00157     defLogger()->printImplementation(LogWarning, log);
    00158 }
    00159 //-----------------------------------------------------------------------------
    00160 void Log::error(const String& log)
    00161 {
    00163   ScopedMutex mutex(Log::logMutex());
    00164
    00165   SET_TEXT_COLOR_RED()
    00166   if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR)
    00167     defLogger()->printImplementation(LogError, log);
    00168 }
    00169 //-----------------------------------------------------------------------------
    00170 void Log::bug(const String& log)
    00171 {
    00173   ScopedMutex mutex(Log::logMutex());
    00174
    00175   SET_TEXT_COLOR_PURPLE()
    00176   if(defLogger() && globalSettings()->verbosityLevel() >= vl::VEL_VERBOSITY_ERROR)
    00177     defLogger()->printImplementation(LogBug, log);
    00178 }
    00179 //------------------------------------------------------------------------------
    00180 void Log::logSystemInfo()
    00181 {
    00182   #if defined(_MSC_VER)
    00183     const char* compiler = "MSVC";
    00184   #elif defined(__GNUG__)
    00185     const char* compiler = "GCC";
    00186   #else
    00187     const char* compiler = "UNKNOWN";
    00188   #endif
    00189
    00190   #if defined(DEBUG) || !defined(NDEBUG)
    00191     const char* build_type = "DEBUG";
    00192   #else
    00193     const char* build_type = "RELEASE";
    00194   #endif
    00195
    00196   print( Say("Visualization Library v%n.%n.%n [%s]\n%s - %s - %s compiler [%s] [%s]\n")
    00197     << VL_Major << VL_Minor << VL_Build
    00198     << (sizeof(vec3) == sizeof(fvec3) ? "f32" : "f64")
    00199     << __DATE__ << __TIME__ << compiler << build_type
    00200     << (sizeof(void*) == 4 ? "x32" : "x64") );
    00201
    00202   print("\n --- Environment ---\n");
    00203   const char* val = getenv("VL_LOGFILE_PATH");
    00204   if (val)
    00205     print( Say("VL_LOGFILE_PATH = %s\n") << val );
    00206   else
    00207     print("VL_LOGFILE_PATH <not present>\n");
    00208
    00209   val = getenv("VL_DATA_PATH");
    00210   if (val)
    00211     print( Say("VL_DATA_PATH = %s\n") << val );
    00212   else
    00213     print("VL_DATA_PATH <not present>\n");
    00214
    00215   val = getenv("VL_VERBOSITY_LEVEL");
    00216   if (val)
    00217     print( Say("VL_VERBOSITY_LEVEL = %s\n") << val );
    00218   else
    00219     print("VL_VERBOSITY_LEVEL <not present>\n");
    00220
    00221   val = getenv("VL_CHECK_GL_STATES");
    00222   if (val)
    00223     print( Say("VL_CHECK_GL_STATES = %s\n") << val );
    00224   else
    00225     print("VL_CHECK_GL_STATES <not present>\n");
    00226
    00227   print("\n --- Global Settings --- \n");
    00228   print( Say("Log file  = %s\n") << globalSettings()->defaultLogPath() );
    00229   print( Say("Data path = %s\n") << globalSettings()->defaultDataPath() );
    00230   print("Verbosity level = ");
    00231   switch(globalSettings()->verbosityLevel())
    00232   {
    00233     /*case vl::VEL_VERBOSITY_SILENT: print("SILENT\n"); break;*/
    00234     case vl::VEL_VERBOSITY_ERROR:  print("ERROR\n"); break;
    00235     case vl::VEL_VERBOSITY_NORMAL: print("NORMAL\n"); break;
    00236     case vl::VEL_VERBOSITY_DEBUG:  print("DEBUG\n"); break;
    00237     default: break;
    00238   }
    00239   print( Say("Check OpenGL States = %s\n") << (globalSettings()->checkOpenGLStates()?"YES":"NO") );
    00240
    00241   print("\n");
    00242 }
    00243 //------------------------------------------------------------------------------
    00244 void vl::log_failed_check(const char* expr, const char* file, int line)
    00245 {
    00246   Log::error( Say("Condition \"%s\" failed at %s:%n\n") << expr << file << line );
    00247   fflush(stdout);
    00248   fflush(stderr);
    00249
    00250   #if _WIN32 && VL_MESSAGEBOX_CHECK == 1
    00251      String msg = Say("Condition \"%s\" failed.\n\n%s:%n\n") << expr << file << line;
    00252      MessageBox(NULL, (wchar_t*)msg.ptr(), L"Visualization Library Debug", MB_OK | MB_ICONEXCLAMATION);
    00253   #endif
    00254 }
    00255 //-----------------------------------------------------------------------------
    00256 // Log mutex.
    00257 //-----------------------------------------------------------------------------
    00258 IMutex* Log::mLogMutex = NULL;
    00259 //-----------------------------------------------------------------------------
    00260 // StandardLog
    00261 //-----------------------------------------------------------------------------
    00262 void StandardLog::setLogFile(const String& file)
    00263 {
    00264   mLogFile = file;
    00265
    00266   if (mFile.is_open())
    00267     mFile.close();
    00268
    00269   if (!file.empty())
    00270     mFile.open(file.toStdString().c_str());
    00271 }
    00272 //-----------------------------------------------------------------------------
    00273 void StandardLog::printImplementation(ELogLevel, const String& log)
    00274 {
    00275   if (log.empty())
    00276     return;
    00277
    00278   std::string stdstr = log.toStdString();
    00279   std::cout << stdstr << std::flush;
    00280
    00281   if (mFile.is_open())
    00282     mFile << stdstr << std::flush;
    00283 }
    00284 //-----------------------------------------------------------------------------

Visualization Library v2011.05.1142 Reference Documentation
Copyright 2005-2011 Michele Bosi. All rights reserved.
Updated on Thu May 26 2011 22:51:21.
Permission is granted to use this page to write and publish articles regarding Visualization Library.