Libbarrett  1.2.4
include/barrett/detail/os.h
00001 
00034 #ifndef BARRETT_DETAIL_OS_H_
00035 #define BARRETT_DETAIL_OS_H_
00036 
00037 
00038 #include <string>
00039 #include <boost/format.hpp>
00040 
00041 
00042 namespace barrett {
00043 namespace detail {
00044 
00045 
00046 class LogFormatter : public boost::format {
00047 public:
00048         LogFormatter(const std::string& fmt, bool outputToStderr) :
00049                 boost::format(fmt), ose(outputToStderr), printed(false) {}
00050         ~LogFormatter() { print(); }
00051 
00052         template <typename ExceptionType>
00053         void raise(bool alsoPrint = false) {
00054                 if (alsoPrint) {
00055                         print();
00056                 } else {
00057                         printed = true;
00058                 }
00059 
00060                 // It's necessary to do this in a member function because it's unsafe to
00061                 // throw exceptions from a dtor.
00062                 throw ExceptionType(str());
00063         }
00064 
00065 
00066         // Reimplemented in order to return LogFormatter instead of boost::format
00067         template<class T>
00068         LogFormatter& operator%(const T& x) {
00069                 boost::format::operator%(x);  // Call super
00070                 return *this;
00071         }
00072 
00073 protected:
00074         void print();
00075 
00076         bool ose;
00077         bool printed;
00078 };
00079 
00080 
00081 }
00082 }
00083 
00084 
00085 #endif /* BARRETT_DETAIL_OS_H_ */
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Defines