MINT2
MessageService.h
Go to the documentation of this file.
1 
23 #ifndef MESSAGE_SERVICE_HH
24 #define MESSAGE_SERVICE_HH
25 
26 #include <iostream>
27 #include <sstream>
28 #include <map>
29 
30 #include "TString.h"
31 
32 
34 
35  private:
36 
39 
41 
42  public:
43 
46 
48  std::ostream& _stream;
49 
53  std::map<ErrorType, bool> _outputOptions;
54 
57  std::map<ErrorType, TString> _outputHeaders;
58 
62 
67 
71 
74  long int _errorCount;
75 
76  static MessageSerivce& getMessageService(ErrorType errorType);
78 
79  void printErrorCount();
80 
83  template <class T>MessageSerivce &operator<< (const T &v)
84  {
85 
86  if (_printOrNot) {
87  if (_endlCalled){
89  if (_errorType == ERROR) _errorCount++;
90  _endlCalled = 0;
91  }
92  _stream << v;
93  }
94  return *this;
95  }
96 
97  //This allows you to make a custom endl (MessageSerivce::endl).
98  //Don't think I need it, but interesting
99 
100  // function that takes a custom stream, and returns it
101  //typedef MessageSerivce& (*MessageSerivceManipulator)(MessageSerivce&);
102 
103 
104  // take in a function with the custom signature
105  // MessageSerivce& operator<<(MessageSerivceManipulator manip)
106  // {
107  // call the function, and return it's value
108  // return manip(*this);
109  // }
110 
111  // define the custom endl for this stream.
112  // note how it matches the `MessageSerivceManipulator`
113  // function signature
114  // static MessageSerivce& endl(MessageSerivce& stream)
115  // {
116  // print a new line
117  // std::cout << std::endl;
118 
119  // do other stuff with the stream
120  // std::cout, for example, will flush the stream
121  // stream << "Called MessageSerivce::endl!" << std::endl;
122 
123  // return stream;
124  // }
125 
126  //The following makes endl work
127 
128 
131  typedef std::basic_ostream<char, std::char_traits<char> > CoutType;
132 
135  typedef CoutType& (*StandardEndLine)(CoutType&);
136 
140  {
141  //SAM -> I'm assuming that when this gets called it's
142  //always endl - I imagine this isn't always the case.
143  if (_printOrNot){
144  _endlCalled = true;
145 
146  // call the function, but we cannot return it's value
147  _stream << "\033[0m";
148  manip(_stream);
149 
150  }
151  return *this;
152  }
153 
154  ~MessageSerivce();
155 
156 };
157 
158 #define WELCOME_LOG MessageSerivce::getMessageService(MessageSerivce::WELCOME)
159 #define ERROR_LOG MessageSerivce::getMessageService(MessageSerivce::ERROR )
160 #define INFO_LOG MessageSerivce::getMessageService(MessageSerivce::INFO )
161 #define VERBOSE_LOG MessageSerivce::getMessageService(MessageSerivce::VERBOSE)
162 #define GOODBYE_LOG MessageSerivce::getMessageService(MessageSerivce::GOODBYE)
163 
164 #define ERROR_COUNT MessageSerivce::getMessageService().printErrorCount();
165 
166 #endif
std::ostream & _stream
the output stream - by default this is std:cout
std::basic_ostream< char, std::char_traits< char > > CoutType
static MessageSerivce * s_messageService
static variable that holds the singleton object
MessageSerivce & operator<<(StandardEndLine manip)
static MessageSerivce & getMessageService()
ErrorType _errorType
std::map< ErrorType, TString > _outputHeaders
MessageSerivce & operator<<(const T &v)
CoutType &(* StandardEndLine)(CoutType &)
std::map< ErrorType, bool > _outputOptions
ErrorType
Define the possible error types.
long int _errorCount