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
00033
00034
00035
00036
00037 #include <pcl/console/print.h>
00038 #include <algorithm>
00039 #include <cstdlib>
00040 #include <cctype>
00041 #include <string>
00042
00043 #if defined WIN32
00044 # include <windows.h>
00045
00046 #ifndef _MSC_VER
00047 # define COMMON_LVB_UNDERSCORE 0
00048 # define COMMON_LVB_REVERSE_VIDEO 0
00049 #endif
00050
00051 WORD
00052 convertAttributesColor (int attribute, int fg, int bg=0)
00053 {
00054 static WORD wAttributes[7] = { 0,
00055 FOREGROUND_INTENSITY ,
00056 0,
00057 COMMON_LVB_UNDERSCORE,
00058 0,
00059 COMMON_LVB_REVERSE_VIDEO,
00060 0
00061 };
00062 static WORD wFgColors[8] = { 0,
00063 FOREGROUND_RED,
00064 FOREGROUND_GREEN ,
00065 FOREGROUND_GREEN | FOREGROUND_RED ,
00066 FOREGROUND_BLUE ,
00067 FOREGROUND_RED | FOREGROUND_BLUE ,
00068 FOREGROUND_GREEN | FOREGROUND_BLUE,
00069 FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED
00070 };
00071 static WORD wBgColors[8] = { 0,
00072 BACKGROUND_RED,
00073 BACKGROUND_GREEN ,
00074 BACKGROUND_GREEN | BACKGROUND_BLUE ,
00075 BACKGROUND_BLUE ,
00076 BACKGROUND_RED | BACKGROUND_BLUE ,
00077 BACKGROUND_GREEN | BACKGROUND_BLUE,
00078 BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED
00079 };
00080
00081 return wAttributes[attribute] | wFgColors[fg] | wBgColors[bg];
00082 }
00083
00084 #endif
00085
00087 void
00088 pcl::console::change_text_color (FILE *stream, int attribute, int fg, int bg)
00089 {
00090 #ifdef WIN32
00091 HANDLE h = GetStdHandle ((stream == stdout) ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
00092 SetConsoleTextAttribute (h, convertAttributesColor (attribute, fg, bg));
00093 #else
00094 char command[13];
00095
00096 sprintf (command, "%c[%d;%d;%dm", 0x1B, attribute, fg + 30, bg + 40);
00097 fprintf (stream, "%s", command);
00098 #endif
00099 }
00100
00102 void
00103 pcl::console::change_text_color (FILE *stream, int attribute, int fg)
00104 {
00105 #ifdef WIN32
00106 HANDLE h = GetStdHandle ((stream == stdout) ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
00107 SetConsoleTextAttribute (h, convertAttributesColor (attribute, fg));
00108 #else
00109 char command[13];
00110
00111 sprintf (command, "%c[%d;%dm", 0x1B, attribute, fg + 30);
00112 fprintf (stream, "%s", command);
00113 #endif
00114 }
00115
00117 void
00118 pcl::console::reset_text_color (FILE *stream)
00119 {
00120 #ifdef WIN32
00121 HANDLE h = GetStdHandle ((stream == stdout) ? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE);
00122 SetConsoleTextAttribute (h, convertAttributesColor (0, TT_WHITE, TT_BLACK));
00123 #else
00124 char command[13];
00125
00126 sprintf (command, "%c[0;m", 0x1B);
00127 fprintf (stream, "%s", command);
00128 #endif
00129 }
00130
00132 void
00133 pcl::console::print_color (FILE *stream, int attr, int fg, const char *format, ...)
00134 {
00135 change_text_color (stream, attr, fg);
00136 va_list ap;
00137
00138 va_start (ap, format);
00139 vfprintf (stream, format, ap);
00140 va_end (ap);
00141
00142 reset_text_color (stream);
00143 }
00144
00146 void
00147 pcl::console::print_info (const char *format, ...)
00148 {
00149 if (!isVerbosityLevelEnabled (L_INFO)) return;
00150
00151 reset_text_color (stdout);
00152
00153 va_list ap;
00154
00155 va_start (ap, format);
00156 vfprintf (stdout, format, ap);
00157 va_end (ap);
00158 }
00159
00161 void
00162 pcl::console::print_info (FILE *stream, const char *format, ...)
00163 {
00164 if (!isVerbosityLevelEnabled (L_INFO)) return;
00165
00166 reset_text_color (stream);
00167
00168 va_list ap;
00169
00170 va_start (ap, format);
00171 vfprintf (stream, format, ap);
00172 va_end (ap);
00173 }
00174
00176 void
00177 pcl::console::print_highlight (const char *format, ...)
00178 {
00179
00180
00181 change_text_color (stdout, TT_BRIGHT, TT_GREEN);
00182 fprintf (stdout, "> ");
00183 reset_text_color (stdout);
00184
00185 va_list ap;
00186
00187 va_start (ap, format);
00188 vfprintf (stdout, format, ap);
00189 va_end (ap);
00190 }
00191
00193 void
00194 pcl::console::print_highlight (FILE *stream, const char *format, ...)
00195 {
00196
00197
00198 change_text_color (stream, TT_BRIGHT, TT_GREEN);
00199 fprintf (stream, "> ");
00200 reset_text_color (stream);
00201
00202 va_list ap;
00203
00204 va_start (ap, format);
00205 vfprintf (stream, format, ap);
00206 va_end (ap);
00207 }
00208
00210 void
00211 pcl::console::print_error (const char *format, ...)
00212 {
00213 if (!isVerbosityLevelEnabled (L_ERROR)) return;
00214
00215 change_text_color (stderr, TT_BRIGHT, TT_RED);
00216 va_list ap;
00217
00218 va_start (ap, format);
00219 vfprintf (stderr, format, ap);
00220 va_end (ap);
00221
00222 reset_text_color (stderr);
00223 }
00224
00226 void
00227 pcl::console::print_error (FILE *stream, const char *format, ...)
00228 {
00229 if (!isVerbosityLevelEnabled (L_ERROR)) return;
00230
00231 change_text_color (stream, TT_BRIGHT, TT_RED);
00232 va_list ap;
00233
00234 va_start (ap, format);
00235 vfprintf (stream, format, ap);
00236 va_end (ap);
00237
00238 reset_text_color (stream);
00239 }
00240
00242 void
00243 pcl::console::print_warn (const char *format, ...)
00244 {
00245 if (!isVerbosityLevelEnabled (L_WARN)) return;
00246
00247 change_text_color (stderr, TT_BRIGHT, TT_YELLOW);
00248 va_list ap;
00249
00250 va_start (ap, format);
00251 vfprintf (stderr, format, ap);
00252 va_end (ap);
00253
00254 reset_text_color (stderr);
00255 }
00256
00258 void
00259 pcl::console::print_warn (FILE *stream, const char *format, ...)
00260 {
00261 if (!isVerbosityLevelEnabled (L_WARN)) return;
00262
00263 change_text_color (stream, TT_BRIGHT, TT_YELLOW);
00264 va_list ap;
00265
00266 va_start (ap, format);
00267 vfprintf (stream, format, ap);
00268 va_end (ap);
00269
00270 reset_text_color (stream);
00271 }
00272
00274 void
00275 pcl::console::print_value (const char *format, ...)
00276 {
00277
00278
00279 change_text_color (stdout, TT_RESET, TT_CYAN);
00280 va_list ap;
00281
00282 va_start (ap, format);
00283 vfprintf (stdout, format, ap);
00284 va_end (ap);
00285
00286 reset_text_color (stdout);
00287 }
00288
00290 void
00291 pcl::console::print_value (FILE *stream, const char *format, ...)
00292 {
00293
00294
00295 change_text_color (stream, TT_RESET, TT_CYAN);
00296 va_list ap;
00297
00298 va_start (ap, format);
00299 vfprintf (stream, format, ap);
00300 va_end (ap);
00301
00302 reset_text_color (stream);
00303 }
00304
00306 void
00307 pcl::console::print_debug (const char *format, ...)
00308 {
00309 if (!isVerbosityLevelEnabled (L_DEBUG)) return;
00310
00311 change_text_color (stdout, TT_RESET, TT_GREEN);
00312 va_list ap;
00313
00314 va_start (ap, format);
00315 vfprintf (stdout, format, ap);
00316 va_end (ap);
00317
00318 reset_text_color (stdout);
00319 }
00320
00322 void
00323 pcl::console::print_debug (FILE *stream, const char *format, ...)
00324 {
00325 if (!isVerbosityLevelEnabled (L_DEBUG)) return;
00326
00327 change_text_color (stream, TT_RESET, TT_GREEN);
00328 va_list ap;
00329
00330 va_start (ap, format);
00331 vfprintf (stream, format, ap);
00332 va_end (ap);
00333
00334 reset_text_color (stream);
00335 }
00336
00338 namespace pcl
00339 {
00340 namespace console
00341 {
00342 static bool s_NeedVerbosityInit = true;
00343 #ifdef VERBOSITY_LEVEL_ALWAYS
00344 static VERBOSITY_LEVEL s_VerbosityLevel = pcl::console::L_ALWAYS;
00345 #elif defined VERBOSITY_LEVEL_ERROR
00346 static VERBOSITY_LEVEL s_VerbosityLevel = pcl::console::L_ERROR;
00347 #elif defined VERBOSITY_LEVEL_WARN
00348 static VERBOSITY_LEVEL s_VerbosityLevel = pcl::console::L_WARN;
00349 #elif defined VERBOSITY_LEVEL_DEBUG
00350 static VERBOSITY_LEVEL s_VerbosityLevel = pcl::console::L_DEBUG;
00351 #elif defined VERBOSITY_LEVEL_VERBOSE
00352 static VERBOSITY_LEVEL s_VerbosityLevel = pcl::console::L_VERBOSE;
00353 #else
00354 static VERBOSITY_LEVEL s_VerbosityLevel = pcl::console::L_INFO;
00355 #endif
00356 }
00357 }
00358
00360 void pcl::console::setVerbosityLevel (pcl::console::VERBOSITY_LEVEL level)
00361 {
00362 if (s_NeedVerbosityInit) pcl::console::initVerbosityLevel ();
00363 s_VerbosityLevel = level;
00364 }
00365
00367 pcl::console::VERBOSITY_LEVEL
00368 pcl::console::getVerbosityLevel ()
00369 {
00370 if (s_NeedVerbosityInit) pcl::console::initVerbosityLevel ();
00371 return s_VerbosityLevel;
00372 }
00373
00375 bool
00376 pcl::console::isVerbosityLevelEnabled (pcl::console::VERBOSITY_LEVEL level)
00377 {
00378 if (s_NeedVerbosityInit) pcl::console::initVerbosityLevel ();
00379 return level <= s_VerbosityLevel;
00380 }
00381
00383 bool
00384 pcl::console::initVerbosityLevel ()
00385 {
00386 #ifdef VERBOSITY_LEVEL_ALWAYS
00387 s_VerbosityLevel = pcl::console::L_ALWAYS;
00388 #elif defined VERBOSITY_LEVEL_ERROR
00389 s_VerbosityLevel = pcl::console::L_ERROR;
00390 #elif defined VERBOSITY_LEVEL_WARN
00391 s_VerbosityLevel = pcl::console::L_WARN;
00392 #elif defined VERBOSITY_LEVEL_DEBUG
00393 s_VerbosityLevel = pcl::console::L_DEBUG;
00394 #elif defined VERBOSITY_LEVEL_VERBOSE
00395 s_VerbosityLevel = pcl::console::L_VERBOSE;
00396 #else
00397 s_VerbosityLevel = pcl::console::L_INFO;
00398 #endif
00399
00400 char* pcl_verbosity_level = getenv ( "PCL_VERBOSITY_LEVEL");
00401 if (pcl_verbosity_level)
00402 {
00403 std::string s_pcl_verbosity_level (pcl_verbosity_level);
00404 std::transform (s_pcl_verbosity_level.begin (), s_pcl_verbosity_level.end (), s_pcl_verbosity_level.begin (), toupper);
00405
00406 if (s_pcl_verbosity_level.find ("ALWAYS") != std::string::npos) s_VerbosityLevel = L_ALWAYS;
00407 else if (s_pcl_verbosity_level.find ("ERROR") != std::string::npos) s_VerbosityLevel = L_ERROR;
00408 else if (s_pcl_verbosity_level.find ("WARN") != std::string::npos) s_VerbosityLevel = L_WARN;
00409 else if (s_pcl_verbosity_level.find ("INFO") != std::string::npos) s_VerbosityLevel = L_INFO;
00410 else if (s_pcl_verbosity_level.find ("DEBUG") != std::string::npos) s_VerbosityLevel = L_DEBUG;
00411 else if (s_pcl_verbosity_level.find ("VERBOSE") != std::string::npos) s_VerbosityLevel = L_VERBOSE;
00412 else printf ("Warning: invalid PCL_VERBOSITY_LEVEL set (%s)\n", s_pcl_verbosity_level.c_str ());
00413 }
00414
00415 s_NeedVerbosityInit = false;
00416 return true;
00417 }
00418
00420 void
00421 pcl::console::print (pcl::console::VERBOSITY_LEVEL level, FILE *stream, const char *format, ...)
00422 {
00423 if (!isVerbosityLevelEnabled (level)) return;
00424 switch (level)
00425 {
00426 case L_DEBUG:
00427 change_text_color (stream, TT_RESET, TT_GREEN);
00428 break;
00429 case L_WARN:
00430 change_text_color (stream, TT_BRIGHT, TT_YELLOW);
00431 break;
00432 case L_ERROR:
00433 change_text_color (stream, TT_BRIGHT, TT_RED);
00434 break;
00435 case L_ALWAYS:
00436 case L_INFO:
00437 case L_VERBOSE:
00438 default:
00439 break;
00440 }
00441
00442 va_list ap;
00443
00444 va_start (ap, format);
00445 vfprintf (stream, format, ap);
00446 va_end (ap);
00447
00448 reset_text_color (stream);
00449 }
00450
00452 void
00453 pcl::console::print (pcl::console::VERBOSITY_LEVEL level, const char *format, ...)
00454 {
00455 if (!isVerbosityLevelEnabled (level)) return;
00456 FILE *stream = (level == L_WARN || level == L_ERROR) ? stderr : stdout;
00457 switch (level)
00458 {
00459 case L_DEBUG:
00460 change_text_color (stream, TT_RESET, TT_GREEN);
00461 break;
00462 case L_WARN:
00463 change_text_color (stream, TT_BRIGHT, TT_YELLOW);
00464 break;
00465 case L_ERROR:
00466 change_text_color (stream, TT_BRIGHT, TT_RED);
00467 break;
00468 case L_ALWAYS:
00469 case L_INFO:
00470 case L_VERBOSE:
00471 default:
00472 break;
00473 }
00474
00475 va_list ap;
00476
00477 va_start (ap, format);
00478 vfprintf (stream, format, ap);
00479 va_end (ap);
00480
00481 reset_text_color (stream);
00482
00483 }