HOPS
HOPS class reference
MHO_Message.hh
Go to the documentation of this file.
1 #ifndef MHO_Message_HH__
2 #define MHO_Message_HH__
3 
4 #include <cstdint>
5 #include <cstdlib>
6 #include <iostream>
7 #include <mutex>
8 #include <ostream>
9 #include <set>
10 #include <sstream>
11 #include <stdio.h>
12 #include <string>
13 
14 #include "MHO_SelfName.hh"
15 #include "MHO_TestAssertions.hh"
16 
17 //include the profiler here to make it visible just about everywhere
18 #include "MHO_Profiler.hh"
19 
20 //MACROS handy for stringifying compiler defines
21 #define STR(str) #str
22 #define STRING(str) STR(str)
23 
24 #ifdef HOPS_ENABLE_DEV_TODO
25  #if defined(__clang__)
26  #define DO_PRAGMA(x) _Pragma(#x)
27  #define TODO_FIXME_MSG(x) DO_PRAGMA(message #x)
28  #elif defined(__GNUC__)
29  #define DO_PRAGMA(x) _Pragma(#x)
30  #define TODO_FIXME_MSG(x) DO_PRAGMA(message #x)
31  #else
32  #error Unsupported compiler
33  #endif
34 #else
35  #define TODO_FIXME_MSG(x)
36 #endif
37 
38 namespace hops
39 {
40 
49 namespace sn = selfname;
50 
55 {};
56 
61 {};
62 
63 static const MHO_MessageNewline ret = MHO_MessageNewline();
64 static const MHO_MessageNewline eol = MHO_MessageNewline();
65 static const MHO_MessageEndline eom = MHO_MessageEndline();
66 
67 enum MHO_MessageLevel : int
68 {
75  eInfoLevel = 4,
76  eDebugLevel = 5
77 };
78 
88 
89 using hops::eDebug;
90 using hops::eError;
91 using hops::eFatal;
92 using hops::eInfo;
93 using hops::eSilent;
94 using hops::eSpecial;
95 using hops::eStatus;
96 using hops::eWarning;
97 
98 #define MSG_ALIGN_PAD 32
99 
102 {
103 
104  public:
106  MHO_Message(MHO_Message const&) = delete;
110  MHO_Message& operator=(MHO_Message const&) = delete;
113 
118  {
119  static MHO_Message instance;
120  return instance;
121  }
122 
124  void Lock() { fMutex.lock(); };
125 
127  void Unlock() { fMutex.unlock(); };
128 
130  void AcceptAllKeys() { fAcceptAllKeys = true; }
131 
133  void LimitToKeySet() { fAcceptAllKeys = false; }
134 
137  void AddKey(const std::string& key);
138 
141  void RemoveKey(const std::string& key);
142 
144  void RemoveAllKeys();
145 
147  void Flush();
148 
151  void SetMessageLevel(MHO_MessageLevel level) { fAllowedLevel = level; }
152 
155  void SetLegacyMessageLevel(int legacy_message_level);
156 
159  MHO_MessageLevel GetMessageLevel() const { return fAllowedLevel; }
160 
166  MHO_Message& SendMessage(const MHO_MessageLevel& level, const std::string& key);
167  MHO_Message& SendMessage(const MHO_MessageLevel& level, const char* key);
168 
174  template< class XStreamableItemType > MHO_Message& operator<<(const XStreamableItemType& item);
175 
178 
179  private:
180  //no public access to constructor
181  //set up the stream, for now just point to std::cout
182  //but we may want to allow this to be configured post-construction
183  //perhaps we should also pipe information into log file(s)
184  MHO_Message()
185  : fTerminalStream(&std::cout), fAllowedLevel(eStatus), fCurrentLevel(eInfo), fCurrentKeyIsAllowed(false),
186  fAcceptAllKeys(false), fWasLastLineNewLine(false)
187  {
188  fPadding = std::string(MSG_ALIGN_PAD, ' ');
189  };
190 
191  virtual ~MHO_Message(){};
192 
193  bool PassMessage();
194  std::string GetCurrentPrefix(const MHO_MessageLevel& level, const std::string& key);
195 
196  std::mutex fMutex;
197 
198  std::ostream* fTerminalStream; //stream to terminal output
199  std::set< std::string > fKeys; //keys of which messages we will accept for output
200  MHO_MessageLevel fAllowedLevel;
201 
202  MHO_MessageLevel fCurrentLevel; //level of the current message
203  bool fCurrentKeyIsAllowed; //current key is in allowed set
204  bool fAcceptAllKeys;
205  std::stringstream fMessageStream; //the message container to be filled/flushed
206 
207  static std::string fRed; //fatal
208  static std::string fYellow; //error
209  static std::string fOrange; //orange
210  static std::string fBlue; //status
211  static std::string fGreen; //info
212  static std::string fCyan; //debug
213  static std::string fWhite; //default
214  static std::string fColorSuffix; //color close
215 
216  bool fWasLastLineNewLine;
217 
218  //whitespace padding
219  std::string fPadding;
220 };
221 
222 template< class XStreamableItemType > MHO_Message& MHO_Message::operator<<(const XStreamableItemType& item)
223 {
224  if(PassMessage())
225  {
226  fMessageStream << item;
227  }
228  return GetInstance();
229 }
230 
231 //abuse do-while for multiline message macros
232 
233 //FATAL/////////////////////////////////////////////////////////////////////////
234 
235 #ifndef HOPS_EXTRA_VERBOSE_MSG
236 
237  #define msg_fatal(xKEY, xCONTENT) \
238  do \
239  { \
240  MHO_Message::GetInstance().Lock(); \
241  MHO_Message::GetInstance().SendMessage(eFatal, xKEY) << xCONTENT; \
242  MHO_Message::GetInstance().Unlock(); \
243  } \
244  while(0)
245 
246  //ERROR/////////////////////////////////////////////////////////////////////////
247  #define msg_error(xKEY, xCONTENT) \
248  do \
249  { \
250  MHO_Message::GetInstance().Lock(); \
251  MHO_Message::GetInstance().SendMessage(eError, xKEY) << xCONTENT; \
252  MHO_Message::GetInstance().Unlock(); \
253  } \
254  while(0)
255 
256  //WARNING///////////////////////////////////////////////////////////////////////
257  #define msg_warn(xKEY, xCONTENT) \
258  do \
259  { \
260  MHO_Message::GetInstance().Lock(); \
261  MHO_Message::GetInstance().SendMessage(eWarning, xKEY) << xCONTENT; \
262  MHO_Message::GetInstance().Unlock(); \
263  } \
264  while(0)
265 
266  //STATUS////////////////////////////////////////////////////////////////////////
267  #define msg_status(xKEY, xCONTENT) \
268  do \
269  { \
270  MHO_Message::GetInstance().Lock(); \
271  MHO_Message::GetInstance().SendMessage(eStatus, xKEY) << xCONTENT; \
272  MHO_Message::GetInstance().Unlock(); \
273  } \
274  while(0)
275 
276  //INFO//////////////////////////////////////////////////////////////////////////
277  #define msg_info(xKEY, xCONTENT) \
278  do \
279  { \
280  MHO_Message::GetInstance().Lock(); \
281  MHO_Message::GetInstance().SendMessage(eInfo, xKEY) << xCONTENT; \
282  MHO_Message::GetInstance().Unlock(); \
283  } \
284  while(0)
285 
286  //allow debug messages when debug flag is active
287  #ifdef HOPS_ENABLE_DEBUG_MSG //this is defined as a compiler flag via build system
288 
289  //DEBUG/////////////////////////////////////////////////////////////////////////
290  #define msg_debug(xKEY, xCONTENT) \
291  do \
292  { \
293  MHO_Message::GetInstance().Lock(); \
294  MHO_Message::GetInstance().SendMessage(eDebug, xKEY) << xCONTENT; \
295  MHO_Message::GetInstance().Unlock(); \
296  } \
297  while(0)
298  #else
299  //debug is not enabled, so we remove them from compilation
300  #define msg_debug(xKEY, xCONTENT)
301  #endif
302 
303 #else //HOPS_EXTRA_VERBOSE_MSG is defined
304 
305  #define msg_fatal(xKEY, xCONTENT) \
306  do \
307  { \
308  MHO_Message::GetInstance().Lock(); \
309  MHO_Message::GetInstance().SendMessage(eFatal, xKEY) \
310  << "(" << sn::file_basename(__FILE__) << ":" << __LINE__ << ") " << xCONTENT; \
311  MHO_Message::GetInstance().Unlock(); \
312  } \
313  while(0)
314 
315  //ERROR/////////////////////////////////////////////////////////////////////////
316  #define msg_error(xKEY, xCONTENT) \
317  do \
318  { \
319  MHO_Message::GetInstance().Lock(); \
320  MHO_Message::GetInstance().SendMessage(eError, xKEY) \
321  << "(" << sn::file_basename(__FILE__) << ":" << __LINE__ << ") " << xCONTENT; \
322  MHO_Message::GetInstance().Unlock(); \
323  } \
324  while(0)
325 
326  //WARNING///////////////////////////////////////////////////////////////////////
327  #define msg_warn(xKEY, xCONTENT) \
328  do \
329  { \
330  MHO_Message::GetInstance().Lock(); \
331  MHO_Message::GetInstance().SendMessage(eWarning, xKEY) \
332  << "(" << sn::file_basename(__FILE__) << ":" << __LINE__ << ") " << xCONTENT; \
333  MHO_Message::GetInstance().Unlock(); \
334  } \
335  while(0)
336 
337  //STATUS////////////////////////////////////////////////////////////////////////
338  #define msg_status(xKEY, xCONTENT) \
339  do \
340  { \
341  MHO_Message::GetInstance().Lock(); \
342  MHO_Message::GetInstance().SendMessage(eStatus, xKEY) \
343  << "(" << sn::file_basename(__FILE__) << ":" << __LINE__ << ") " << xCONTENT; \
344  MHO_Message::GetInstance().Unlock(); \
345  } \
346  while(0)
347 
348  //INFO//////////////////////////////////////////////////////////////////////////
349  #define msg_info(xKEY, xCONTENT) \
350  do \
351  { \
352  MHO_Message::GetInstance().Lock(); \
353  MHO_Message::GetInstance().SendMessage(eInfo, xKEY) \
354  << "(" << sn::file_basename(__FILE__) << ":" << __LINE__ << ") " << xCONTENT; \
355  MHO_Message::GetInstance().Unlock(); \
356  } \
357  while(0)
358 
359  //allow debug messages when debug flag is active
360  #ifdef HOPS_ENABLE_DEBUG_MSG //this is defined as a compiler flag via build system
361 
362  //DEBUG/////////////////////////////////////////////////////////////////////////
363  #define msg_debug(xKEY, xCONTENT) \
364  do \
365  { \
366  MHO_Message::GetInstance().Lock(); \
367  MHO_Message::GetInstance().SendMessage(eDebug, xKEY) \
368  << "(" << sn::file_basename(__FILE__) << ":" << __LINE__ << ") " << xCONTENT; \
369  MHO_Message::GetInstance().Unlock(); \
370  } \
371  while(0)
372  #else
373  //debug is not enabled, so we remove them from compilation
374  #define msg_debug(xKEY, xCONTENT)
375  #endif
376 
377 #endif
378 
379 #ifdef HOPS_ENABLE_STEPWISE_CHECK //this is defined as a compiler flag via build system
380  //error check is enabled, so that we can verify a boolean return value is true
381  #define check_step_error(xVALUE, xKEY, xCONTENT) \
382  if(!xVALUE) \
383  { \
384  MHO_Message::GetInstance().SendMessage(eError, xKEY) << xCONTENT; \
385  }
386  #define check_step_fatal(xVALUE, xKEY, xCONTENT) \
387  if(!xVALUE) \
388  { \
389  MHO_Message::GetInstance().SendMessage(eFatal, xKEY) << xCONTENT; \
390  HOPS_ASSERT_THROW(xVALUE); \
391  }
392 #else
393  //error check is competely disabled
394  #define check_step_error(xVALUE, xKEY, xCONTENT)
395  #define check_step_fatal(xVALUE, xKEY, xCONTENT)
396 #endif
397 
398 } // namespace hops
399 
400 #endif
#define MSG_ALIGN_PAD
Definition: MHO_Message.hh:98
constexpr to strip path prefix from FILE macros
uses the singleton pattern (as we only have one terminal)
Definition: MHO_Message.hh:102
MHO_Message(MHO_Message const &)=delete
Deleted copy constructor (singleton).
MHO_Message & SendMessage(const MHO_MessageLevel &level, const std::string &key)
Begin a new message with a specific level and key.
Definition: MHO_Message.cc:45
void SetLegacyMessageLevel(int legacy_message_level)
Set the message level using a legacy integer interface.
Definition: MHO_Message.cc:189
MHO_Message & operator=(MHO_Message const &)=delete
Deleted copy assignment operator (singleton).
void AcceptAllKeys()
Allow all message keys (no category/library filtering).
Definition: MHO_Message.hh:130
void LimitToKeySet()
Restrict messages to a defined key set.
Definition: MHO_Message.hh:133
void RemoveAllKeys()
Remove all message keys (disables all filtering).
Definition: MHO_Message.cc:29
void SetMessageLevel(MHO_MessageLevel level)
Set the allowed message level threshold.
Definition: MHO_Message.hh:151
void RemoveKey(const std::string &key)
Remove a message key from the allowed key set.
Definition: MHO_Message.cc:20
MHO_Message(MHO_Message &&)=delete
Deleted move constructor (singleton).
MHO_Message & operator<<(const XStreamableItemType &item)
Stream an item into the current message.
Definition: MHO_Message.hh:222
void Flush()
Flush the current output stream buffer.
Definition: MHO_Message.cc:34
void Lock()
Lock the message handler for thread-safe operations.
Definition: MHO_Message.hh:124
void AddKey(const std::string &key)
Add a message key to the allowed key set.
Definition: MHO_Message.cc:15
void Unlock()
Unlock the message handler.
Definition: MHO_Message.hh:127
MHO_Message & operator=(MHO_Message &&)=delete
Deleted move assignment operator (singleton).
MHO_MessageLevel GetMessageLevel() const
Get the currently configured message level threshold.
Definition: MHO_Message.hh:159
static MHO_Message & GetInstance()
Access the singleton instance of the message handler.
Definition: MHO_Message.hh:117
Definition: MHO_AdhocFlagging.hh:18
MHO_MessageLevel
Definition: MHO_Message.hh:68
@ eStatusLevel
use to inform about unexpected state which may lead to errors
Definition: MHO_Message.hh:74
@ eDebugLevel
extra information to inform about configuration/state of program
Definition: MHO_Message.hh:76
@ eWarningLevel
use for non-fatal errors which may lead to unexpected behavior
Definition: MHO_Message.hh:73
@ eSpecialLevel
Definition: MHO_Message.hh:69
@ eErrorLevel
use for fatal errors (program termination imminent)
Definition: MHO_Message.hh:72
@ eInfoLevel
information about the current execution status of the program
Definition: MHO_Message.hh:75
@ eFatalErrorLevel
mute all messages entirely, including fatal ones
Definition: MHO_Message.hh:71
@ eSilentErrorLevel
special print level
Definition: MHO_Message.hh:70
Class MHO_MessageEndline.
Definition: MHO_Message.hh:61
Class MHO_MessageNewline.
Definition: MHO_Message.hh:55