Sierra Toolkit  Version of the Day
Writer.hpp
1 /*------------------------------------------------------------------------*/
2 /* Copyright 2010, 2011 Sandia Corporation. */
3 /* Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive */
4 /* license for use of this work by or on behalf of the U.S. Government. */
5 /* Export of this program may require a license from the */
6 /* United States Government. */
7 /*------------------------------------------------------------------------*/
8 
9 #ifndef STK_UTIL_DIAG_WRITER_HPP
10 #define STK_UTIL_DIAG_WRITER_HPP
11 
12 #include <ostream>
13 #include <string>
14 #include <vector>
15 #include <utility>
16 #include <stdint.h>
17 
18 #include <stk_util/diag/Writer_fwd.hpp>
19 
20 namespace stk_classic {
21 namespace diag {
22 
27 
28 class WriterThrowSafe
29 {
30 public:
31  explicit WriterThrowSafe(Writer &writer);
32 
33  ~WriterThrowSafe();
34 
35 private:
36  Writer & m_writer;
37  int m_depth;
38 
39  WriterThrowSafe(const WriterThrowSafe &);
40  void operator = (const WriterThrowSafe &);
41 };
42 
43 
49 class Writer
50 {
51 public:
60  enum Flags {
61  DISABLED = 0x00,
62  ENABLED = 0x01
63  };
64 
65 private:
81  struct LineMaskStack : public std::vector<std::pair<int, PrintMask> >
82  {
88  LineMaskStack() {
89  push_back(std::pair<int, PrintMask>(0, LOG_ALWAYS));
90  }
91 
98  LineMaskStack &pushDepth() {
99  push_back(std::make_pair(back().first + 1, back().second));
100  return *this;
101  }
102 
111  LineMaskStack &push(PrintMask line_mask) {
112  push_back(std::make_pair(back().first, line_mask));
113  return *this;
114  }
115 
122  LineMaskStack &pop() {
123  if (size() > 1)
124  pop_back();
125  return *this;
126  }
127 
134  LineMaskStack &popLineMask() {
135  if (size() > 1 && getNextDepth() == getDepth())
136  pop_back();
137  return *this;
138  }
139 
146  int getDepth() const {
147  return back().first;
148  }
149 
157  int getLineMask() const {
158  return back().second;
159  }
160 
168  int getNextDepth() const {
169  return (end() - 2)->first;
170  }
171 
178  LineMaskStack &resetDepth() {
179  while (size() > 1 && getNextDepth() == getDepth())
180  pop_back();
181  return *this;
182  }
183  };
184 
185 public:
196  explicit Writer(std::streambuf *streambuf, PrintMask print_mask = static_cast<PrintMask>(LOG_MEMBERS), Flags flags = static_cast<Flags>(ENABLED));
197 
202  ~Writer();
203 
210  std::ostream &getStream() {
211  return m_writerStream;
212  }
213 
222  Writer &setFlags(int flags) {
223  m_flags = (Flags) flags;
224  return *this;
225  }
226 
232  int getFlags() {
233  return m_flags;
234  }
235 
236  int getDepth() const {
237  return m_lineMaskStack.getDepth();
238  }
239 
240  Writer &restoreDepth(int depth) {
241  while (m_lineMaskStack.getDepth() > depth)
242  pop();
243  return *this;
244  }
245 
255  Writer &setPrintMask(PrintMask mask = 0) {
256  m_printMask = mask;
257  return *this;
258  }
259 
269  //Writer &setPrintMask(const char *mask_string);
270 
280  Writer &setLineMask(PrintMask line_mask) {
281  m_lineMaskStack.push(line_mask);
282 
283  return *this;
284  }
285 
294  Writer &m(PrintMask line_mask) {
295  setLineMask(line_mask);
296 
297  return *this;
298  }
299 
308  Writer &w(bool on, PrintMask line_mask) {
309  setLineMask(on ? line_mask : 0x80000000);
310 
311  return *this;
312  }
313 
323  Writer &t(PrintMask line_mask = 0) {
324  setLineMask(line_mask | stk_classic::LOG_TRACE);
325 
326  return *this;
327  }
328 
334  PrintMask getPrintMask() {
335  return m_printMask;
336  }
337 
345  bool isEnabled() {
346  return (m_flags & ENABLED) != 0;
347  }
348 
356  bool isLoggable(PrintMask line_mask) {
357  return line_mask == 0 // Always
358  || ((line_mask & m_printMask & stk_classic::LOG_TRACE) // LOG_TRACE?
359  ? isTracing() // Yes, must be tracing
360 // : (line_mask & m_printMask) != 0); // No, any matching bits
361  : (line_mask & m_printMask) == line_mask); // No, all matching bits
362  }
363 
369  bool shouldPrint() {
370  return shouldPrint(m_lineMaskStack.getLineMask());
371  }
372 
380  bool shouldPrint(PrintMask line_mask) {
381  return isEnabled() && isLoggable(line_mask);
382  }
383 
392  bool shouldTrace(int line_mask) {
393  return line_mask == 0 // Always
394  || (line_mask & m_printMask) != 0; // Any set
395 // || (line_mask & m_printMask) == line_mask; // All set
396  }
397 
403  Writer &dflush();
404 
413  Writer &dendl();
414 
421  Writer &push();
422 
429  Writer &pop();
430 
438 
439 #ifndef SWIG
440 
445  Writer& operator<<(Writer& (*f)(Writer&));
446 
453  Writer& operator<<(std::ios_base& (*f)(std::ios_base&));
454 
461  Writer& operator<<(std::ostream& (*f)(std::ostream&));
462 #endif // SWIG
463 
469  return ++m_traceDepth;
470  }
471 
478  return --m_traceDepth;
479  }
480 
489  bool isTracing() {
490  return m_traceDepth <= 0 ? false
491  : (m_traceDepth == 1 || (m_traceDepth > 1 && (m_printMask & stk_classic::LOG_TRACE_SUB_CALLS)));
492  }
493 
501  bool isTraceable() {
502  return isTracing() || (m_printMask & stk_classic::LOG_TRACE) != 0; // Currently in a trace or tracing bit set
503  }
504 
505 private:
506  Flags m_flags;
507  PrintMask m_printMask;
508  LineMaskStack m_lineMaskStack;
509  int m_traceDepth;
510  std::ostream m_writerStream;
511 };
512 
520 inline Writer &dendl(Writer &dout) {
521  return dout.dendl();
522 }
523 
532 inline Writer &dflush(Writer &dout) {
533  return dout.dflush();
534 }
535 
544 inline Writer &push(Writer &dout) {
545  return dout.push();
546 }
547 
556 inline Writer &pop(Writer &dout) {
557  return dout.pop();
558 }
559 
560 
566 {
572  _setlinemask(PrintMask line_mask)
573  : m_lineMask(line_mask)
574  {}
575 
576  PrintMask m_lineMask;
577 };
578 
585 inline _setlinemask setlinemask(PrintMask line_mask) {
586  return _setlinemask(line_mask);
587 }
588 
599 #ifndef SWIG
600 inline Writer &operator<<(Writer &dout, _setlinemask set_line_mask) {
601  return dout.setLineMask(set_line_mask.m_lineMask);
602 }
603 #endif // SWIG
604 
613 inline Writer &resetlinemask(Writer &dout) {
614  return dout.resetLineMask();
615 }
616 
628 #ifndef SWIG
629 Writer &operator<<(Writer &dout, const char *c_str);
630 Writer &operator<<(Writer &dout, const std::string &str);
631 Writer &operator<<(Writer &dout, const void *ptr);
632 Writer &operator<<(Writer &dout, const float &x);
633 Writer &operator<<(Writer &dout, const double &x);
634 Writer &operator<<(Writer &dout, const long double &x);
635 Writer &operator<<(Writer &dout, const int &x);
636 Writer &operator<<(Writer &dout, const unsigned int &x);
637 Writer &operator<<(Writer &dout, const long &x);
638 Writer &operator<<(Writer &dout, const unsigned long &x);
639 Writer &operator<<(Writer &dout, const short &x);
640 Writer &operator<<(Writer &dout, const unsigned short &x);
641 Writer &operator<<(Writer &dout, const long long &x);
642 Writer &operator<<(Writer &dout, const unsigned long long &x);
643 #endif // SWIG
644 
650 template <class T>
651 class c_ptr_
652 {
653 public:
659  explicit c_ptr_(const T *t)
660  : m_t(t)
661  {}
662 
663 public:
664  const T * m_t;
665 };
666 
674 template <class T>
675 c_ptr_<T> c_ptr(const T *t) {
676  return c_ptr_<T>(t);
677 }
678 
685 template <class T, typename R>
687 {
688 public:
697 #ifndef SWIG
698  explicit c_ptr_func_(const T *t, R (T::*pmf)() const)
699  : m_t(t),
700  m_pmf(pmf)
701  {}
702 public:
703  const T * m_t;
704  R (T::*m_pmf)() const;
705 #endif // SWIG
706 };
707 
722 #ifndef SWIG
723 template <class T, typename R>
724 c_ptr_func_<T, R> c_ptr_func(const T *t, R (T::*pmf)() const) {
725  return c_ptr_func_<T, R>(t, pmf);
726 }
727 
741 template <class T>
742 Writer &operator<<(Writer &dout, const c_ptr_<T> &c) {
743  dout << "(pointer " << (void *) c.m_t << "), ";
744 
745  if (c.m_t)
746  dout << *c.m_t;
747  else
748  dout << "<not created>";
749 
750  return dout;
751 }
752 
767 template <class T, typename R>
768 Writer &operator<<(Writer &dout, const c_ptr_func_<T, R> &c) {
769  if (c.m_t)
770  dout << "(pointer), " << (c.m_t->*c.m_pmf)();
771  else
772  dout << "(pointer), <not created>";
773 
774  return dout;
775 }
776 #endif // SWIG
777 
781 
782 } // namespace diag
783 } // namespace stk_classic
784 
785 #include <stk_util/diag/WriterManip.hpp>
786 
787 namespace sierra {
788 
789 
794 
795 namespace Diag {
796 
806 
807 }
808 
809 } // namespace sierra
810 
811 #include <stk_util/diag/WriterExt.hpp>
812 
813 #endif // STK_UTIL_DIAG_WRITER_HPP
Writer & resetLineMask()
Member function pop is a manipulator which decreases the line mask depth by one, but not less than ze...
Definition: Writer.cpp:118
const T * m_t
Pointer to object.
Definition: Writer.hpp:664
_setlinemask(PrintMask line_mask)
Creates a new setlinemask instance.
Definition: Writer.hpp:572
Writer & dflush()
Member function dflush flushes the output stream.
Definition: Writer.cpp:56
Writer & operator<<(Writer &(*f)(Writer &))
Member function operator<< is the manipulator instantiation function.
Definition: Writer.cpp:131
R(T::* m_pmf)() const
Function to call for dump.
Definition: Writer.hpp:704
Definition: Env.cpp:53
int incTraceDepth()
Member function incTraceDepth increments the tracing count.
Definition: Writer.hpp:468
Writer & pop(Writer &dout)
Member function pop calls the Writer::pop manipulator.
Definition: Writer.hpp:556
Writer & setFlags(int flags)
Member function setFlags sets the flags bitmask which describes the output line prefix content...
Definition: Writer.hpp:222
Writer & setLineMask(PrintMask line_mask)
Member function setPrintMask sets the print output mask.
Definition: Writer.hpp:280
PrintMask getPrintMask()
Member function getLineMask returns the current line mask.
Definition: Writer.hpp:334
Writer & dendl()
Member function dendl is a manipulator which sets the output stream to a new line.
Definition: Writer.cpp:70
bool isTraceable()
Member function isTraceable returns true if currently tracing or tracing is enabled.
Definition: Writer.hpp:501
Writer & dflush(Writer &dout)
Writer function dflush calls the Writer::dflush manipulator.
Definition: Writer.hpp:532
const T * m_t
Pointer to object.
Definition: Writer.hpp:703
bool isEnabled()
Member function isEnabled returns true if the ENABLED bit is set in the flags bitmask.
Definition: Writer.hpp:345
Writer & push()
Member function push is a manipulator which increases the line mask depth by one. ...
Definition: Writer.cpp:86
c_ptr_< T > c_ptr(const T *t)
Definition: Writer.hpp:675
Writer(std::streambuf *streambuf, PrintMask print_mask=static_cast< PrintMask >(LOG_MEMBERS), Flags flags=static_cast< Flags >(ENABLED))
Creates a new Writer instance with the specified print mask and output flags.
Definition: Writer.cpp:34
Class c_ptr_func_ simply stores a pointer to an object of type T. This allows pointers which want to ...
Definition: Writer.hpp:686
~Writer()
Destroys a Writer instance.
Definition: Writer.cpp:46
std::ostream & getStream()
Member function getStream returns the output stream.
Definition: Writer.hpp:210
bool isLoggable(PrintMask line_mask)
Member function isLoggable returns true if any corresponding bit in the line mask matches a bit in th...
Definition: Writer.hpp:356
bool shouldPrint()
Member function shouldPrint returns true if the line should print.
Definition: Writer.hpp:369
Writer & t(PrintMask line_mask=0)
Member function t sets the line mask of this line to line_make bitwise or&#39;ed with LOG_TRACE...
Definition: Writer.hpp:323
Writer & dendl(Writer &dout)
Writer function dendl calls the Writer::dendl manipulator.
Definition: Writer.hpp:520
c_ptr_func_(const T *t, R(T::*pmf)() const)
Definition: Writer.hpp:698
Class c_ptr_ simply stores a pointer to an object of type T. This allows pointers which want to be de...
Definition: Writer.hpp:651
Sierra Toolkit.
Writer & setPrintMask(PrintMask mask=0)
Member function setPrintMask sets the print output mask.
Definition: Writer.hpp:255
Class Writer implements a runtime selectable diagnostic output writer to aid in the development and d...
Definition: Writer.hpp:49
int getFlags()
Member function getFlags returns the flags bitmask.
Definition: Writer.hpp:232
Writer & m(PrintMask line_mask)
Member function m sets the line mask of this line.
Definition: Writer.hpp:294
int decTraceDepth()
Member function decTraceDepth decrements the tracing count.
Definition: Writer.hpp:477
bool shouldPrint(PrintMask line_mask)
Member function shouldPrint returns true if the line should print.
Definition: Writer.hpp:380
Writer & resetlinemask(Writer &dout)
Function resetlinemask calls the Writer::resetLineMask manipulator.
Definition: Writer.hpp:613
bool isTracing()
Member function isTracing returns true of the trace depth is greater than zero. The value of -1 is in...
Definition: Writer.hpp:489
bool shouldTrace(int line_mask)
Member function shouldTrace returns true if any corresponding bit in the line mask matches a bit in t...
Definition: Writer.hpp:392
c_ptr_func_< T, R > c_ptr_func(const T *t, R(T::*pmf)() const)
Template function c_ptr creates a c_ptr_func_ object of type T ala std::make_pair. This T must implement a member function which takes no arguments and returns a value of type R.
Definition: Writer.hpp:724
_setlinemask setlinemask(PrintMask line_mask)
Function setlinemask sets the active line mask bits as a manipulator.
Definition: Writer.hpp:585
Class _setlinemask is the line mask manipulator.
Definition: Writer.hpp:565
Writer & pop()
Member function pop is a manipulator which decreases the line mask depth by one, but not less than ze...
Definition: Writer.cpp:102
Writer & push(Writer &dout)
Function push calls the Writer::push manipulator.
Definition: Writer.hpp:544
Flags
Enumeration Flags.
Definition: Writer.hpp:60
Writer & w(bool on, PrintMask line_mask)
Member function m sets the line mask of this line.
Definition: Writer.hpp:308