LogBinding.h
Go to the documentation of this file.
00001 #ifndef GRASP_PLANNING_GRASPIT_LOGBINDING_H
00002 #define GRASP_PLANNING_GRASPIT_LOGBINDING_H
00003 
00023 #include <string>
00024 #include <iostream>
00025 #include <sstream>
00026 #include <grasp_planning_graspit/SharedPtr.h>
00027 
00028 namespace GraspIt
00029 {
00030 
00036 class Log
00037 {
00038 public:
00039     static void print(const std::stringstream& str)
00040     {
00041         if (sglOK(str.str().c_str())) Singleton->implPrint(str);
00042     }
00043     static void printError(const std::stringstream& str)
00044     {
00045         if (sglOK(str.str().c_str())) Singleton->implPrintError(str);
00046     }
00047     static void printWarn(const std::stringstream& str)
00048     {
00049         if (sglOK(str.str().c_str())) Singleton->implPrintWarn(str);
00050     }
00051 
00052     static void print(const char * str)
00053     {
00054         if (sglOK(str)) Singleton->implPrint(str);
00055     }
00056     static void printError(const char * str)
00057     {
00058         if (sglOK(str)) Singleton->implPrintError(str);
00059     }
00060     static void printWarn(const char * str)
00061     {
00062         if (sglOK(str)) Singleton->implPrintWarn(str);
00063     }
00064 
00065     static void printLn(const std::stringstream& str)
00066     {
00067         if (sglOK(str.str().c_str()))
00068         {
00069             Singleton->implPrint(str);
00070             Singleton->printNewLine(false);
00071         }
00072     }
00073     static void printErrorLn(const std::stringstream& str)
00074     {
00075         if (sglOK(str.str().c_str()))
00076         {
00077             Singleton->implPrintError(str);
00078             Singleton->printNewLine(false);
00079         }
00080     }
00081     static void printWarnLn(const std::stringstream& str)
00082     {
00083         if (sglOK(str.str().c_str()))
00084         {
00085             Singleton->implPrintWarn(str);
00086             Singleton->printNewLine(false);
00087         }
00088     }
00089 
00090     static void printLn(const char * str)
00091     {
00092         if (sglOK(str))
00093         {
00094             Singleton->implPrint(str);
00095             Singleton->printNewLine(false);
00096         }
00097     }
00098     static void printErrorLn(const char * str)
00099     {
00100         if (sglOK(str))
00101         {
00102             Singleton->implPrintError(str);
00103             Singleton->printNewLine(false);
00104         }
00105     }
00106     static void printWarnLn(const char * str)
00107     {
00108         if (sglOK(str))
00109         {
00110             Singleton->implPrintWarn(str);
00111             Singleton->printNewLine(false);
00112         }
00113     }
00114 
00115     static SHARED_PTR<Log> Singleton;
00116 
00117 protected:
00118     virtual void implPrint(const std::stringstream& str) = 0;
00119     virtual void implPrintError(const std::stringstream& str) = 0;
00120     virtual void implPrintWarn(const std::stringstream& str) = 0;
00121     virtual void implPrint(const char * str) = 0;
00122     virtual void implPrintError(const char * str) = 0;
00123     virtual void implPrintWarn(const char * str) = 0;
00130     virtual void printNewLine(bool errorStream) = 0;
00131 
00132 private:
00138     static bool sglOK(const char * msg)
00139     {
00140         if (Singleton.get()) return true;
00141 
00142         if (!initSglWarningPrinted)
00143         {
00144             std::cerr << "WARNING: Initialise Log Singleton to use the proper Logger. Now printing to std out." << std::endl;
00145             initSglWarningPrinted = true;
00146         }
00147         std::cout << msg << std::endl;
00148         return false;
00149     }
00150 
00151     // print warning that Singleton is not initialized only once.
00152     static bool initSglWarningPrinted;
00153 };
00154 
00160 class StdLog: public Log
00161 {
00162 protected:
00163     virtual void implPrint(const std::stringstream& str)
00164     {
00165         std::cout << str.str();
00166     }
00167     virtual void implPrintError(const std::stringstream& str)
00168     {
00169         std::cerr << "ERROR:" << str.str();
00170     }
00171     virtual void implPrintWarn(const std::stringstream& str)
00172     {
00173         std::cout << "WARNING:" << str.str();
00174     }
00175 
00176     virtual void implPrint(const char* str)
00177     {
00178         std::cout << str;
00179     }
00180     virtual void implPrintError(const char* str)
00181     {
00182         std::cerr << "ERROR: " << str;
00183     }
00184     virtual void implPrintWarn(const char* str)
00185     {
00186         std::cout << "WARNING: " << str;
00187     }
00188 
00189 
00190     virtual void printNewLine(bool errorStream)
00191     {
00192         if (errorStream) std::cerr << std::endl;
00193         else std::cout << std::endl;
00194     }
00195 };
00196 
00197 }  // namespace GraspIt
00198 
00199 extern std::string getFilenameFromPath(const std::string& path);
00200 
00201 // in a path to a file name, erase the filename from it and return only directory path
00202 extern std::string getFileDirectory(const std::string& pathToFile);
00203 
00204 // initialize the print log singleton to use std::out and std::err
00205 #define PRINT_INIT_STD() \
00206 {\
00207     if (GraspIt::Log::Singleton) \
00208     {\
00209         std::cerr << "Singleton already set, overwriting!" << std::endl;\
00210     }\
00211     GraspIt::Log::Singleton = SHARED_PTR<GraspIt::Log>(new GraspIt::StdLog()); \
00212 }
00213 
00214 
00215 #define PRINTMSG(msg) \
00216 {\
00217     std::stringstream _str_; \
00218     _str_ << msg << " - "<< getFilenameFromPath(__FILE__) << ", " << __LINE__; \
00219     GraspIt::Log::printLn(_str_); \
00220 }
00221 
00222 #define PRINTDEBUG(msg) \
00223 {\
00224     std::stringstream _str_; \
00225     _str_ << msg << " - "<< getFilenameFromPath(__FILE__) << ", " << __LINE__; \
00226     GraspIt::Log::printLn(_str_); \
00227 }
00228 
00229 #define PRINTERROR(msg) \
00230 {\
00231     std::stringstream _str_; \
00232     _str_ << msg << " - "<< getFilenameFromPath(__FILE__) << ", " << __LINE__; \
00233     GraspIt::Log::printErrorLn(_str_); \
00234 }
00235 
00236 #define PRINTWARN(msg) \
00237 {\
00238     std::stringstream _str_; \
00239     _str_ << msg << " - "<< getFilenameFromPath(__FILE__) << ", " << __LINE__; \
00240     GraspIt::Log::printWarnLn(_str_); \
00241 }
00242 
00243 
00244 #endif  // GRASP_PLANNING_GRASPIT_LOGBINDING_H


grasp_planning_graspit
Author(s): Jennifer Buehler
autogenerated on Wed May 8 2019 02:53:36