ewmscp  ..
errMsgQueue.h
Go to the documentation of this file.
1 #ifndef __errMsgQueue_h__
2 #define __errMsgQueue_h__
3 
4 #include <sstream>
5 #include <iostream>
6 #include <syslog.h>
7 #include "waitQueues.h"
8 #include "syslogstream.h"
9 namespace errMsg {
14  class location {
15  const char*function;
16  const char*file;
17  int line;
18  public:
19  #if defined(__has_builtin) // we probably use CLANG
20  #if __has_builtin(__builtin_FUNCTION)
21 #define __useBuiltinFunction__
22  #endif
23  #else // we probably use GCC
24 #define __useBuiltinFunction__
25  #endif
26  #ifdef __useBuiltinFunction__
27  constexpr location(const char *aFunc = __builtin_FUNCTION(),
28  int aLine = __builtin_LINE(),
29  const char *aFile = __builtin_FILE()):
31  #else // no info available....
32  constexpr location(const char *aFunc = "unknown",
33  int aLine = 0,
34  const char *aFile = "unknown"):
35  #endif
36 
37  function(aFunc),
38  file(aFile),
39  line(aLine) {}
40  const char* getFunc() const {
41  return function;
42  };
43  const char* getFile() const {
44  return file;
45  };
46  int getLine() const {
47  return line;
48  };
49  };
50  enum class level {
51  emerg = LOG_EMERG,
52  alert = LOG_ALERT,
53  crit = LOG_CRIT,
54  err = LOG_ERR,
55  warning = LOG_WARNING,
56  notice = LOG_NOTICE,
57  info = LOG_INFO,
58  debug = LOG_DEBUG
59  };
63  class message {
64  private:
65  std::string object;
66  std::string action;
67  std::string msg;
69  std::thread::id threadId;
70  std::chrono::system_clock::time_point timeStamp;
72  message(level aLogLevel,
73  const location& aLocation,
74  const std::string& aObject,
75  const std::string& aAction,
76  const std::string& aMessage):
77  object(aObject),
78  action(aAction),
79  msg(aMessage),
80  loc(aLocation),
81  threadId(std::this_thread::get_id()),
82  timeStamp(std::chrono::system_clock::now()),
83  logLevel(aLogLevel) {
84  std::unique_ptr<message> u(this);
85  getQueue().enqueue(u);
86  }
87  public:
91  static std::remove_reference<decltype(getQueue())>::type queue;
92  return queue;
93  }
95  static void printMsgs() {
96  while (auto msg = getQueue().dequeue()) {
97  std::cerr << msg->loc.getFile() << ":" << msg->loc.getLine() << ": in " << msg->loc.getFunc() << ": " << msg->object << " " << msg->action << " " << msg->msg << "\n";
98  }
99  }
100 
102  }
103  static void generateMsg(level aLogLevel,
104  const location& aLocation,
105  const std::string& aObject,
106  const std::string& aAction,
107  const std::string& aMessage) {
108  new message(aLogLevel, aLocation, aObject, aAction, aMessage);
109  }
110  const std::string& getObject() const {
111  return object;
112  };
113  const std::string& getAction() const {
114  return action;
115  };
116  const std::string& getMessage() const {
117  return msg;
118  };
119  const location& getLoc() const {
120  return loc;
121  };
122  std::thread::id getThreadId() const {
123  return threadId;
124  };
125  std::chrono::system_clock::time_point getTimeStamp() const {
126  return timeStamp;
127  };
129  return static_cast<logstream::level>(logLevel);
130  };
131  };
132 
133 
134  template <typename T> void conCatString(std::ostringstream& msg, const T& begin) {
135  msg << begin;
136  }
137  template <typename T, typename ... Args> inline void conCatString(std::ostringstream& msg,
138  const T& begin,
139  const Args& ... args) {
140  msg << begin;
141  if (sizeof...(args)) {
142  conCatString(msg, args...);
143  }
144  }
145 
148  template <typename ... Args> inline void emit(level aLogLevel,
149  const location& loc,
150  const std::string& aObject,
151  const std::string& aAction,
152  const Args& ... args
153  ) {
154  std::ostringstream msg;
155  conCatString(msg, args...);
156  message::generateMsg(aLogLevel, loc, aObject, aAction, msg.str());
157  }
158  // template <typename ... Args> inline void debug(level aLogLevel,bool doit, const location& loc,const Args& ... args) {
159  // if (__builtin_expect(doit,false)) {
160  // std::ostringstream msg;
161  // conCatString(msg,args...);
162  // message::generateMsg(aLogLevel,loc,msg.str());
163  // }
164  //}
165 }; // end namespace errMsg
166 
167 #endif
__useBuiltinFunction__
#define __useBuiltinFunction__
Definition: errMsgQueue.h:24
errMsg::level::warning
@ warning
errMsg::level::emerg
@ emerg
waitQueues.h
errMsg::message::object
std::string object
Definition: errMsgQueue.h:65
errMsg::location
class for defining the location of a error message in the source code.
Definition: errMsgQueue.h:14
errMsg::level::alert
@ alert
errMsg::message::message
message(level aLogLevel, const location &aLocation, const std::string &aObject, const std::string &aAction, const std::string &aMessage)
Definition: errMsgQueue.h:72
errMsg::location::function
const char * function
Definition: errMsgQueue.h:15
errMsg::location::location
constexpr location(const char *aFunc=__builtin_FUNCTION(), int aLine=__builtin_LINE(), const char *aFile=__builtin_FILE())
Definition: errMsgQueue.h:27
errMsg::location::getFile
const char * getFile() const
Definition: errMsgQueue.h:43
errMsg::level::crit
@ crit
errMsg::level::info
@ info
errMsg::message::msg
std::string msg
Definition: errMsgQueue.h:67
errMsg::message::getThreadId
std::thread::id getThreadId() const
Definition: errMsgQueue.h:122
errMsg
Definition: errMsgQueue.h:9
errMsg::location::line
int line
Definition: errMsgQueue.h:17
errMsg::message::action
std::string action
Definition: errMsgQueue.h:66
errMsg::message::getQueue
static waitQueues::simple< message > & getQueue()
get reference to teh message queue
Definition: errMsgQueue.h:90
errMsg::message::getMessage
const std::string & getMessage() const
Definition: errMsgQueue.h:116
errMsg::level::debug
@ debug
errMsg::message
class for (error) messages.
Definition: errMsgQueue.h:63
errMsg::message::printMsgs
static void printMsgs()
simple printer function for messages, can be used as a std::thread
Definition: errMsgQueue.h:95
errMsg::message::~message
~message()
Definition: errMsgQueue.h:101
errMsg::message::logLevel
level logLevel
Definition: errMsgQueue.h:71
errMsg::location::getFunc
const char * getFunc() const
Definition: errMsgQueue.h:40
errMsg::message::getTimeStamp
std::chrono::system_clock::time_point getTimeStamp() const
Definition: errMsgQueue.h:125
errMsg::message::timeStamp
std::chrono::system_clock::time_point timeStamp
Definition: errMsgQueue.h:70
errMsg::message::getObject
const std::string & getObject() const
Definition: errMsgQueue.h:110
errMsg::message::getAction
const std::string & getAction() const
Definition: errMsgQueue.h:113
errMsg::conCatString
void conCatString(std::ostringstream &msg, const T &begin)
Definition: errMsgQueue.h:134
errMsg::message::generateMsg
static void generateMsg(level aLogLevel, const location &aLocation, const std::string &aObject, const std::string &aAction, const std::string &aMessage)
Definition: errMsgQueue.h:103
errMsg::message::threadId
std::thread::id threadId
Definition: errMsgQueue.h:69
errMsg::message::loc
location loc
Definition: errMsgQueue.h:68
errMsg::level::err
@ err
errMsg::level
level
Definition: errMsgQueue.h:50
errMsg::location::file
const char * file
Definition: errMsgQueue.h:16
errMsg::emit
void emit(level aLogLevel, const location &loc, const std::string &aObject, const std::string &aAction, const Args &... args)
function to create and enqueue a message, this is the only way that messages should be created!
Definition: errMsgQueue.h:148
errMsg::message::getLoc
const location & getLoc() const
Definition: errMsgQueue.h:119
waitQueues::simple
Definition: waitQueues.h:31
logstream::level
level
Definition: syslogstream.h:10
errMsg::message::getLogLevel
logstream::level getLogLevel() const
Definition: errMsgQueue.h:128
errMsg::level::notice
@ notice
syslogstream.h
errMsg::location::getLine
int getLine() const
Definition: errMsgQueue.h:46