Sierra Toolkit Version of the Day
Writer.hpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010, 2011 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #ifndef STK_UTIL_DIAG_WRITER_HPP
00010 #define STK_UTIL_DIAG_WRITER_HPP
00011 
00012 #include <ostream>
00013 #include <string>
00014 #include <vector>
00015 #include <utility>
00016 #include <stdint.h>
00017 
00018 #include <stk_util/diag/Writer_fwd.hpp>
00019 
00020 namespace stk {
00021 namespace diag {
00022 
00027 
00028 class WriterThrowSafe
00029 {
00030 public:
00031   explicit WriterThrowSafe(Writer &writer);
00032 
00033   ~WriterThrowSafe();
00034 
00035 private:
00036   Writer &      m_writer;
00037   int           m_depth;
00038 
00039   WriterThrowSafe(const WriterThrowSafe &);
00040   void operator = (const WriterThrowSafe &);
00041 };
00042 
00043 
00049 class Writer
00050 {
00051 public:
00060   enum Flags {
00061     DISABLED    =  0x00,
00062     ENABLED    =  0x01
00063   };
00064 
00065 private:
00081   struct LineMaskStack : public std::vector<std::pair<int, PrintMask> >
00082   {
00088     LineMaskStack() {
00089       push_back(std::pair<int, PrintMask>(0, LOG_ALWAYS));
00090     }
00091 
00098     LineMaskStack &pushDepth() {
00099       push_back(std::make_pair(back().first + 1, back().second));
00100       return *this;
00101     }
00102 
00111     LineMaskStack &push(PrintMask line_mask) {
00112       push_back(std::make_pair(back().first, line_mask));
00113       return *this;
00114     }
00115 
00122     LineMaskStack &pop() {
00123       if (size() > 1)
00124         pop_back();
00125       return *this;
00126     }
00127 
00134     LineMaskStack &popLineMask() {
00135       if (size() > 1 && getNextDepth() == getDepth())
00136         pop_back();
00137       return *this;
00138     }
00139 
00146     int getDepth() const {
00147       return back().first;
00148     }
00149 
00157     int getLineMask() const {
00158       return back().second;
00159     }
00160 
00168     int getNextDepth() const {
00169       return (end() - 2)->first;
00170     }
00171 
00178     LineMaskStack &resetDepth() {
00179       while (size() > 1 && getNextDepth() == getDepth())
00180         pop_back();
00181       return *this;
00182     }
00183   };
00184 
00185 public:
00196   explicit Writer(std::streambuf *streambuf, PrintMask print_mask = static_cast<PrintMask>(LOG_MEMBERS), Flags flags = static_cast<Flags>(ENABLED));
00197 
00202   ~Writer();
00203 
00210   std::ostream &getStream() {
00211     return m_writerStream;
00212   }
00213 
00222   Writer &setFlags(int flags) {
00223     m_flags = (Flags) flags;
00224     return *this;
00225   }
00226 
00232   int getFlags() {
00233     return m_flags;
00234   }
00235 
00236   int getDepth() const {
00237     return m_lineMaskStack.getDepth();
00238   }
00239 
00240   Writer &restoreDepth(int depth) {
00241     while (m_lineMaskStack.getDepth() > depth)
00242       pop();
00243     return *this;
00244   }
00245 
00255   Writer &setPrintMask(PrintMask mask = 0) {
00256     m_printMask = mask;
00257     return *this;
00258   }
00259 
00269   //Writer &setPrintMask(const char *mask_string);
00270 
00280   Writer &setLineMask(PrintMask line_mask) {
00281     m_lineMaskStack.push(line_mask);
00282 
00283     return *this;
00284   }
00285 
00294   Writer &m(PrintMask line_mask) {
00295     setLineMask(line_mask);
00296 
00297     return *this;
00298   }
00299 
00308   Writer &w(bool on, PrintMask line_mask) {
00309     setLineMask(on ? line_mask : 0x80000000);
00310 
00311     return *this;
00312   }
00313 
00323   Writer &t(PrintMask line_mask = 0) {
00324     setLineMask(line_mask | stk::LOG_TRACE);
00325 
00326     return *this;
00327   }
00328 
00334   PrintMask getPrintMask() {
00335     return m_printMask;
00336   }
00337 
00345   bool isEnabled() {
00346     return (m_flags & ENABLED) != 0;
00347   }
00348 
00356   bool isLoggable(PrintMask line_mask) {
00357     return line_mask == 0            // Always
00358       || ((line_mask & m_printMask & stk::LOG_TRACE)      // LOG_TRACE?
00359           ? isTracing()              // Yes, must be tracing
00360 //    : (line_mask & m_printMask) != 0);        // No, any matching bits
00361           : (line_mask & m_printMask) == line_mask);      // No, all matching bits
00362   }
00363 
00369   bool shouldPrint() {
00370     return shouldPrint(m_lineMaskStack.getLineMask());
00371   }
00372 
00380   bool shouldPrint(PrintMask line_mask) {
00381     return isEnabled() && isLoggable(line_mask);
00382   }
00383 
00392   bool shouldTrace(int line_mask) {
00393     return line_mask == 0            // Always
00394       || (line_mask & m_printMask) != 0;        // Any set
00395 //      || (line_mask & m_printMask) == line_mask;      // All set
00396   }
00397 
00403   Writer &dflush();
00404 
00413   Writer &dendl();
00414 
00421   Writer &push();
00422 
00429   Writer &pop();
00430 
00437   Writer &resetLineMask();
00438 
00439 #ifndef SWIG
00440 
00445   Writer& operator<<(Writer& (*f)(Writer&));
00446 
00453   Writer& operator<<(std::ios_base& (*f)(std::ios_base&));
00454 
00461   Writer& operator<<(std::ostream& (*f)(std::ostream&));
00462 #endif // SWIG
00463 
00468   int incTraceDepth() {
00469     return ++m_traceDepth;
00470   }
00471 
00477   int decTraceDepth() {
00478     return --m_traceDepth;
00479   }
00480 
00489   bool isTracing() {
00490     return m_traceDepth <= 0 ? false
00491       : (m_traceDepth == 1 || (m_traceDepth > 1 && (m_printMask & stk::LOG_TRACE_SUB_CALLS)));
00492   }
00493 
00501   bool isTraceable() {
00502     return isTracing() || (m_printMask & stk::LOG_TRACE) != 0; // Currently in a trace or tracing bit set
00503   }
00504 
00505 private:
00506   Flags        m_flags;    
00507   PrintMask      m_printMask;    
00508   LineMaskStack      m_lineMaskStack;  
00509   int        m_traceDepth;    
00510   std::ostream                  m_writerStream;
00511 };
00512 
00520 inline Writer &dendl(Writer &dout) {
00521   return dout.dendl();
00522 }
00523 
00532 inline Writer &dflush(Writer &dout) {
00533   return dout.dflush();
00534 }
00535 
00544 inline Writer &push(Writer &dout) {
00545   return dout.push();
00546 }
00547 
00556 inline Writer &pop(Writer &dout) {
00557   return dout.pop();
00558 }
00559 
00560 
00565 struct _setlinemask
00566 {
00572   _setlinemask(PrintMask line_mask)
00573     : m_lineMask(line_mask)
00574   {}
00575 
00576   PrintMask    m_lineMask;
00577 };
00578 
00585 inline _setlinemask setlinemask(PrintMask line_mask) {
00586   return _setlinemask(line_mask);
00587 }
00588 
00599 #ifndef SWIG
00600 inline Writer &operator<<(Writer &dout, _setlinemask set_line_mask) {
00601   return dout.setLineMask(set_line_mask.m_lineMask);
00602 }
00603 #endif // SWIG
00604 
00613 inline Writer &resetlinemask(Writer &dout) {
00614   return dout.resetLineMask();
00615 }
00616 
00628 #ifndef SWIG
00629 Writer &operator<<(Writer &dout, const char *c_str);
00630 Writer &operator<<(Writer &dout, const std::string &str);
00631 Writer &operator<<(Writer &dout, const void *ptr);
00632 Writer &operator<<(Writer &dout, const float &x);
00633 Writer &operator<<(Writer &dout, const double &x);
00634 Writer &operator<<(Writer &dout, const long double &x);
00635 Writer &operator<<(Writer &dout, const int &x);
00636 Writer &operator<<(Writer &dout, const unsigned int &x);
00637 Writer &operator<<(Writer &dout, const long &x);
00638 Writer &operator<<(Writer &dout, const unsigned long &x);
00639 Writer &operator<<(Writer &dout, const short &x);
00640 Writer &operator<<(Writer &dout, const unsigned short &x);
00641 Writer &operator<<(Writer &dout, const long long &x);
00642 Writer &operator<<(Writer &dout, const unsigned long long &x);
00643 #endif // SWIG
00644 
00650 template <class T>
00651 class c_ptr_
00652 {
00653 public:
00659   explicit c_ptr_(const T *t)
00660     : m_t(t)
00661   {}
00662 
00663 public:
00664   const T *  m_t;      
00665 };
00666 
00674 template <class T>
00675 c_ptr_<T> c_ptr(const T *t) {
00676   return c_ptr_<T>(t);
00677 }
00678 
00685 template <class T, typename R>
00686 class c_ptr_func_
00687 {
00688 public:
00697 #ifndef SWIG
00698   explicit c_ptr_func_(const T *t, R (T::*pmf)() const)
00699     : m_t(t),
00700       m_pmf(pmf)
00701   {}
00702 public:
00703   const T *  m_t;      
00704   R (T::*m_pmf)() const;    
00705 #endif // SWIG
00706 };
00707 
00722 #ifndef SWIG
00723 template <class T, typename R>
00724 c_ptr_func_<T, R> c_ptr_func(const T *t, R (T::*pmf)() const) {
00725   return c_ptr_func_<T, R>(t, pmf);
00726 }
00727 
00741 template <class T>
00742 Writer &operator<<(Writer &dout, const c_ptr_<T> &c) {
00743   dout << "(pointer " << (void *) c.m_t << "), ";
00744 
00745   if (c.m_t)
00746     dout << *c.m_t;
00747   else
00748     dout << "<not created>";
00749 
00750   return dout;
00751 }
00752 
00767 template <class T, typename R>
00768 Writer &operator<<(Writer &dout, const c_ptr_func_<T, R> &c) {
00769   if (c.m_t)
00770     dout << "(pointer), " << (c.m_t->*c.m_pmf)();
00771   else
00772     dout << "(pointer), <not created>";
00773 
00774   return dout;
00775 }
00776 #endif // SWIG
00777 
00781 
00782 } // namespace diag
00783 } // namespace stk
00784 
00785 #include <stk_util/diag/WriterManip.hpp>
00786 
00787 namespace sierra {
00788 
00789 
00790 using stk::diag::push;
00791 using stk::diag::pop;
00792 using stk::diag::dendl;
00793 using stk::diag::dflush;
00794 
00795 namespace Diag {
00796 
00797 using stk::diag::push;
00798 using stk::diag::pop;
00799 using stk::diag::dendl;
00800 using stk::diag::dflush;
00801 using stk::diag::setlinemask;
00802 using stk::diag::resetlinemask;
00803 using stk::diag::c_ptr;
00804 using stk::diag::c_ptr_func;
00805 using stk::diag::c_ptr_func_;
00806 
00807 }
00808 
00809 } // namespace sierra
00810 
00811 #include <stk_util/diag/WriterExt.hpp>
00812 
00813 #endif // STK_UTIL_DIAG_WRITER_HPP
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines