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/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
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
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 puts(color);
00107 }
00108 ~ScopedColor()
00109 {
00110
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
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
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
00257
00258 IMutex* Log::mLogMutex = NULL;
00259
00260
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