print.cpp
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  *  Copyright (c) 2010, Willow Garage, Inc.
00005  *  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions
00009  *  are met:
00010  *
00011  *   * Redistributions of source code must retain the above copyright
00012  *     notice, this list of conditions and the following disclaimer.
00013  *   * Redistributions in binary form must reproduce the above
00014  *     copyright notice, this list of conditions and the following
00015  *     disclaimer in the documentation and/or other materials provided
00016  *     with the distribution.
00017  *   * Neither the name of Willow Garage, Inc. nor the names of its
00018  *     contributors may be used to endorse or promote products derived
00019  *     from this software without specific prior written permission.
00020  *
00021  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028  *  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029  *  CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031  *  ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032  *  POSSIBILITY OF SUCH DAMAGE.
00033  *
00034  * $Id: print.cpp 4681 2012-02-22 23:48:44Z mdixon $
00035  *
00036  */
00037 #include <pcl/console/print.h>
00038 #include <algorithm>
00039 #include <cstdlib>
00040 #include <cctype> // for toupper
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,                        // TT_RESET
00055                                    FOREGROUND_INTENSITY ,    // TT_BRIGHT
00056                                    0,                        // TT_DIM
00057                                    COMMON_LVB_UNDERSCORE,    // TT_UNDERLINE
00058                                    0,                        // TT_BLINK
00059                                    COMMON_LVB_REVERSE_VIDEO, // TT_REVERSE
00060                                    0                         // TT_HIDDEN
00061                                  };
00062   static WORD wFgColors[8]  = { 0,                                                  // TT_BLACK
00063                                  FOREGROUND_RED,                                     // TT_RED
00064                                  FOREGROUND_GREEN ,                                  // TT_GREEN
00065                                  FOREGROUND_GREEN | FOREGROUND_RED ,                 // TT_YELLOW
00066                                  FOREGROUND_BLUE ,                                   // TT_BLUE
00067                                  FOREGROUND_RED | FOREGROUND_BLUE ,                  // TT_MAGENTA
00068                                  FOREGROUND_GREEN | FOREGROUND_BLUE,                 // TT_CYAN
00069                                  FOREGROUND_GREEN | FOREGROUND_BLUE | FOREGROUND_RED // TT_WHITE
00070                                };
00071   static WORD wBgColors[8]  = { 0,                                                  // TT_BLACK
00072                                  BACKGROUND_RED,                                     // TT_RED
00073                                  BACKGROUND_GREEN ,                                  // TT_GREEN
00074                                  BACKGROUND_GREEN | BACKGROUND_BLUE ,                // TT_YELLOW
00075                                  BACKGROUND_BLUE ,                                   // TT_BLUE
00076                                  BACKGROUND_RED | BACKGROUND_BLUE ,                  // TT_MAGENTA
00077                                  BACKGROUND_GREEN | BACKGROUND_BLUE,                 // TT_CYAN
00078                                  BACKGROUND_GREEN | BACKGROUND_BLUE | BACKGROUND_RED // TT_WHITE
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   // Command is the control command to the terminal
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   // Command is the control command to the terminal
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   // Command is the control command to the terminal
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   //if (!isVerbosityLevelEnabled (L_ALWAYS)) return;
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   //if (!isVerbosityLevelEnabled (L_ALWAYS)) return;
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   //if (!isVerbosityLevelEnabled (L_ALWAYS)) return;
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   //if (!isVerbosityLevelEnabled (L_ALWAYS)) return;
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     static VERBOSITY_LEVEL s_VerbosityLevel = pcl::console::L_INFO;
00344   }
00345 }
00346 
00348 void pcl::console::setVerbosityLevel (pcl::console::VERBOSITY_LEVEL level)
00349 {
00350   if (s_NeedVerbosityInit) pcl::console::initVerbosityLevel ();
00351   s_VerbosityLevel = level;
00352 }
00353 
00355 pcl::console::VERBOSITY_LEVEL
00356 pcl::console::getVerbosityLevel ()
00357 {
00358   if (s_NeedVerbosityInit) pcl::console::initVerbosityLevel ();
00359   return s_VerbosityLevel;
00360 }
00361 
00363 bool
00364 pcl::console::isVerbosityLevelEnabled (pcl::console::VERBOSITY_LEVEL level)
00365 {
00366   if (s_NeedVerbosityInit) pcl::console::initVerbosityLevel ();
00367   return level <= s_VerbosityLevel;  
00368 }
00369 
00371 bool 
00372 pcl::console::initVerbosityLevel ()
00373 {
00374   s_VerbosityLevel = pcl::console::L_INFO; // Default value
00375   char* pcl_verbosity_level = getenv ( "PCL_VERBOSITY_LEVEL");
00376   if (pcl_verbosity_level)
00377   {
00378     std::string s_pcl_verbosity_level (pcl_verbosity_level);
00379     std::transform (s_pcl_verbosity_level.begin (), s_pcl_verbosity_level.end (), s_pcl_verbosity_level.begin (), toupper);
00380 
00381     if (s_pcl_verbosity_level.find ("ALWAYS") != std::string::npos)          s_VerbosityLevel = L_ALWAYS;
00382     else if (s_pcl_verbosity_level.find ("ERROR") != std::string::npos)      s_VerbosityLevel = L_ERROR;
00383     else if (s_pcl_verbosity_level.find ("WARN") != std::string::npos)       s_VerbosityLevel = L_WARN;
00384     else if (s_pcl_verbosity_level.find ("INFO") != std::string::npos)       s_VerbosityLevel = L_INFO;
00385     else if (s_pcl_verbosity_level.find ("DEBUG") != std::string::npos)      s_VerbosityLevel = L_DEBUG;
00386     else if (s_pcl_verbosity_level.find ("VERBOSE") != std::string::npos)    s_VerbosityLevel = L_VERBOSE;
00387     else std::cout << "Warning: invalid PCL_VERBOSITY_LEVEL set (" << s_pcl_verbosity_level << ")" << std::endl;
00388   }
00389 
00390   s_NeedVerbosityInit = false;
00391   return true;
00392 }
00393 
00395 void 
00396 pcl::console::print (pcl::console::VERBOSITY_LEVEL level, FILE *stream, const char *format, ...)
00397 {
00398   if (!isVerbosityLevelEnabled (level)) return;
00399   switch (level)
00400   {
00401     case L_DEBUG:
00402       change_text_color (stream, TT_RESET, TT_GREEN);
00403       break;
00404     case L_WARN:
00405       change_text_color (stream, TT_BRIGHT, TT_YELLOW);
00406       break;
00407     case L_ERROR:
00408       change_text_color (stream, TT_BRIGHT, TT_RED);
00409       break;
00410     case L_ALWAYS:
00411     case L_INFO:
00412     case L_VERBOSE:
00413     default:
00414       break;
00415   }
00416 
00417   va_list ap;
00418 
00419   va_start (ap, format);
00420   vfprintf (stream, format, ap);
00421   va_end (ap);
00422   
00423   reset_text_color (stream);
00424 }
00425 
00427 void 
00428 pcl::console::print (pcl::console::VERBOSITY_LEVEL level, const char *format, ...)
00429 {
00430   if (!isVerbosityLevelEnabled (level)) return;
00431   FILE *stream = (level == L_WARN || level == L_ERROR) ? stderr : stdout;
00432   switch (level)
00433   {
00434     case L_DEBUG:
00435       change_text_color (stream, TT_RESET, TT_GREEN);
00436       break;
00437     case L_WARN:
00438       change_text_color (stream, TT_BRIGHT, TT_YELLOW);
00439       break;
00440     case L_ERROR:
00441       change_text_color (stream, TT_BRIGHT, TT_RED);
00442       break;
00443     case L_ALWAYS:
00444     case L_INFO:
00445     case L_VERBOSE:
00446     default:
00447       break;
00448   }
00449   
00450   va_list ap;
00451 
00452   va_start (ap, format);
00453   vfprintf (stream, format, ap);
00454   va_end (ap);
00455   
00456   reset_text_color (stream);
00457 
00458 }


pcl
Author(s): Open Perception
autogenerated on Mon Oct 6 2014 03:17:24