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


logger_binding
Author(s): Jennifer Buehler
autogenerated on Sat Mar 2 2019 03:21:43