Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032 #include <vlCore/Log.hpp>
00033 #include <vlCore/checks.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
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
00079
00080
00081
00082
00083
00084
00085
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 printf("%s", color);
00109 }
00110 ~ScopedColor()
00111 {
00112
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
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%n.%n.%n [%s]\n%s - %s - %s compiler [%s] [%s]\n")
00202 << VL_Major << VL_Minor << VL_Build
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
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
00262
00263 IMutex* Log::mLogMutex = NULL;
00264
00265
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