Crombie Tools
old/CommonTools/interface/Debug.h
Go to the documentation of this file.
1 /**
2  @file Debug.h
3 
4  Defines the Debug class for setting verbosity level.
5 
6  @author Daniel Abercrombie <dabercro@mit.edu>
7 */
8 
9 #ifndef CROMBIETOOLS_COMMONTOOLS_DEBUG_H
10 #define CROMBIETOOLS_COMMONTOOLS_DEBUG_H
11 
12 #include <map>
13 #include <iostream>
14 
15 /**
16  @ingroup commongroup
17  @struct Debug
18  Class setting verbosity levels
19 */
20 
21 class Debug
22 {
23  public:
24  Debug () {}
25  virtual ~Debug() {}
26 
27  /// Different possible debug levels
28  enum DebugLevel {
29  eQuiet = 0, ///< Only prints out what ROOT insists on
30  eError, ///< Prints when it's obvious something goes wrong
31  eInfo, ///< Prints information that might be interesting
32  eDebug, ///< Prints debugging information
33  };
34 
35  /// Sets the verbosity for a class
36  void SetDebugLevel ( DebugLevel level ) { fDebugLevel = level; }
37 
38  /// Gets the verbosity for a class
40 
41  /// Sends a message if the verbosity level is appropriate
42  template<typename T, typename... V>
43  void Message ( DebugLevel level, T message, V... more);
44  void Message ( DebugLevel level );
45 
46  protected:
47  /// Sends the name of the function during debuggin
48  void DisplayFunc ( const char* func );
49 
50  private:
51  /// A map setting the tag to give a given debug level
52  std::map<DebugLevel, const char*> mLabels = {
53  {eError, "[ERROR]"}, {eInfo, "[INFO]"}, {eDebug, "[DEBUG]"}
54  };
55 
56  template<typename T>
57  void OneMessage ( DebugLevel level, T message);
58 
59  DebugLevel fDebugLevel = eError; ///< The verbosity for a class.
60  bool _printed = false; ///< Track if the label has bee printed
61 };
62 
63 //--------------------------------------------------------------------
64 void
65 Debug::DisplayFunc (const char* func)
66 {
67 
68  Message(eDebug, "-------------------------------------------------------");
69  Message(eDebug, " ", func);
70  Message(eDebug, "-------------------------------------------------------");
71 
72 }
73 
74 template<typename T> void Debug::OneMessage (DebugLevel level, T message)
75 {
76 
77  if (fDebugLevel >= level) {
78  auto& debug_output = (level > eError) ? std::cout : std::cerr;
79  if (not _printed) {
80  debug_output << mLabels[level] << " ";
81  _printed = true;
82  }
83 
84  debug_output << message << " ";
85  }
86 
87 }
88 
89 template<typename T, typename... V> void Debug::Message(DebugLevel level, T message, V... more) {
90  OneMessage(level, message);
91  Message(level, more...);
92 }
93 
95  _printed = false;
96  if (fDebugLevel >= level) {
97  auto& debug_output = (level > eError) ? std::cout : std::cerr;
98  debug_output << std::endl;
99  }
100 }
101 
102 #endif