// // File : ErrorLoggerStream.h // // Purpose: Declaration of the class ErrorLoggerStream // // $Id: ErrorLoggerStream.hh 8758 2010-09-27 12:16:24Z mathes $ // /** @file ErrorLoggerStream.hh * Declaration of the class ErrorLoggerStream. * @author H.-J. Mathes, FzK */ #ifndef _FdUtil_ErrorLoggerStream_hh_ #define _FdUtil_ErrorLoggerStream_hh_ #include #include #include #ifndef NO_VERSION # include #endif // NO_VERSION #include #include namespace FdUtil { /** Interface to the FD (Error) logger stream. * * This class provides a std::ostream compatible stream for logging messages. */ class ErrorLoggerStream : public std::ostream, public FdUtil::Singleton { friend class FdUtil::Singleton; friend class ErrorLogger; public: using FdUtil::Singleton::GetInstance; /** Get a reference to the instance of the ErrorLoggerStream singleton. * * If the instance in created with this method, the logging stream will * have the passed name. */ static ErrorLoggerStream& GetInstance(const std::string name); /** Set the name of the logging stream, i.e. overwrite any previous * settings (via GetInstance()). */ void SetName(const std::string name) { fName = name; } /** Set the print level (= level of verbosity for all instances of this * class). */ static void SetPrintLevel(unsigned int level=1) { fgPrintLevel = level; } public: // --- inserters for severity and verbosity levels /** operator<<() to insert ErrorLogger::ESeverityLevel into the * ErrorLoggerStream. * * This will set the severity level of the actual message. */ ErrorLoggerStream& operator<<(ErrorLogger::ESeverityLevel); /** operator<<() to insert ErrorLogger::EVerbosityLevel into the * ErrorLoggerStream. * * This will actually change the verbosity level. */ ErrorLoggerStream& operator<<(ErrorLogger::EVerbosityLevel); public: // --- inserters for standard types /** operator<<(std::string) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(std::string str) { return (*this) << str.c_str(); } // --- if ostringstream is templatized then this would be easier ... // so let's hope and wait for gcc-3.x ... /** operator<<(char) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(char c) { fStream << c; return *this; } /** operator<<(unsigned char) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(unsigned char c) { fStream << (char)c; return *this; } /** operator<<(signed char) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(signed char c) { fStream << (char)c; return *this; } /** operator<<(const char *) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(const char *s) { fStream << s; return *this; } /** operator<<(const unsigned char *) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(const unsigned char *s) { fStream << (const char*)s ; return *this; } /** operator<<(const signed char *) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(const signed char *s) { fStream << (const char*)s; return *this; } /** operator<<(const void *) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(const void *p) { fStream << p; return *this; } /** operator<<(int) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(int n) { fStream << n; return *this; } /** operator<<(unsigned int) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(unsigned int n) { fStream << n; return *this; } /** operator<<(long) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(long n) { fStream << n; return *this; } /** operator<<(unsigned long) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(unsigned long n) { fStream << n; return *this; } /** operator<<(short) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(short n) { fStream << (int)n; return *this; } /** operator<<(unsigned short) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(unsigned short n) { fStream << (unsigned int)n; return *this; } /** operator<<(double) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(double n) { fStream << n; return *this; } /** operator<<(float) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(float n) { fStream << (double)n; return *this; } // --- needed to let "<< endl" work ... #ifdef __GNUC__ # if __GNUC__ < 3 /** operator<<(__omanip) for the class ErrorLoggerStream. */ ErrorLoggerStream& operator<<(__omanip func) { (*func)(*this); return *this; } # else /** Stream manipulator endl (insert CR and flush). */ ErrorLoggerStream& endl(ErrorLoggerStream& os); # endif // __GNUC__ < 3 // LogStream& operator<<(__manip func) // { (*func)(*this); return *this; } #else # error This software was never compiled with compilers other than gcc ! #endif // __GNUC__ public: /** Some method to tell the stream to send the message (flush()). * * To be replaced by some intelligent form of endl !!! */ ErrorLoggerStream& endm(); protected: /** Constructor for the class ErrorLoggerStream. * * The name 'name' is assigned to the create ErrorLoggerStream. */ ErrorLoggerStream(const std::string name="default"); private: ErrorLoggerStream(const ErrorLoggerStream&); ErrorLoggerStream& operator=(const ErrorLoggerStream&); static unsigned int fgPrintLevel; std::string fName; ErrorLogger::ESeverityLevel fSeverity; ErrorLogger::EVerbosityLevel fVerbosity; std::ostringstream fStream; }; } // namespace FdUtil // macros #define FDDEBUG \ FdUtil::ErrorLoggerStream::GetInstance() \ << FdUtil::ErrorLogger::eDebug #define FDINFO \ FdUtil::ErrorLoggerStream::GetInstance() \ << FdUtil::ErrorLogger::eInfo #define FDWARNING \ FdUtil::ErrorLoggerStream::GetInstance() \ << FdUtil::ErrorLogger::eWarning #define FDERROR \ FdUtil::ErrorLoggerStream::GetInstance() \ << FdUtil::ErrorLogger::eError #define FDCRITICAL \ FdUtil::ErrorLoggerStream::GetInstance() \ << FdUtil::ErrorLogger::eCritical #define FDSEVERE \ FdUtil::ErrorLoggerStream::GetInstance() \ << FdUtil::ErrorLogger::eSevere #define FDFATAL \ FdUtil::ErrorLoggerStream::GetInstance() \ << FdUtil::ErrorLogger::eFatal #define ENDM FdUtil::ErrorLoggerStream::GetInstance().endm(); #endif // _FdUtil_ErrorLoggerStream_hh_