Messenger.h
Go to the documentation of this file.
1 //____________________________________________________________________________
2 /*!
3 
4 \class genie::Messenger
5 
6 \brief A more convenient interface to the log4cpp Message Service
7 
8 \author Costas Andreopoulos <costas.andreopoulos \at stfc.ac.uk>
9  University of Liverpool & STFC Rutherford Appleton Lab
10 
11 \created June 16, 2004
12 
13 \cpright Copyright (c) 2003-2019, The GENIE Collaboration
14  For the full text of the license visit http://copyright.genie-mc.org
15  or see $GENIE/LICENSE
16 */
17 //____________________________________________________________________________
18 
19 #ifndef _MESSENGER_H_
20 #define _MESSENGER_H_
21 
22 #include <iostream>
23 #include <cstring>
24 #include <string>
25 #include <map>
26 
27 // ROOT5 has difficulty with parsing log4cpp headers
28 #if !defined(__CINT__) && !defined(__MAKECINT__)
29  #include "log4cpp/Category.hh"
30  #include "log4cpp/Appender.hh"
31  #include "log4cpp/OstreamAppender.hh"
32  #include "log4cpp/BasicLayout.hh"
33  #include "log4cpp/Priority.hh"
34 #else
35  namespace log4cpp {
36  class Priority {
37  typedef int Value;
38  };
39  class Category;
40  }
41 #endif
42 
44 
45 using std::string;
46 
47 // comment defined priority levels for the document generator
48 /*! \def pFATAL \brief Defines the FATAL priority level */
49 /*! \def pALERT \brief Defines the ALERT priority level */
50 /*! \def pCRIT \brief Defines the CRIT priority level */
51 /*! \def pERROR \brief Defines the ERROR priority level */
52 /*! \def pWARN \brief Defines the WARN priority level */
53 /*! \def pNOTICE \brief Defines the NOTICE priority level */
54 /*! \def pINFO \brief Defines the INFO priority level */
55 /*! \def pDEBUG \brief Defines the DEBUG priority level */
56 
57 #define pFATAL log4cpp::Priority::FATAL
58 #define pALERT log4cpp::Priority::ALERT
59 #define pCRIT log4cpp::Priority::CRIT
60 #define pERROR log4cpp::Priority::ERROR
61 #define pWARN log4cpp::Priority::WARN
62 #define pNOTICE log4cpp::Priority::NOTICE
63 #define pINFO log4cpp::Priority::INFO
64 #define pDEBUG log4cpp::Priority::DEBUG
65 
66 /*! \def ENDL \brief A shortcut for log4cpp's CategoryStream::ENDLINE or std manipulators*/
67 
68 #ifdef __GENIE_USES_LOG4CPP_VERSION__
69  #if __GENIE_USES_LOG4CPP_VERSION__==0
70  #define ENDL log4cpp::CategoryStream::ENDLINE
71  #else
72  #define ENDL std::endl
73  #endif
74 #else
75  #define ENDL std::endl
76 #endif
77 
78 /*!
79  \def SLOG(stream, priority)
80  \brief A macro that returns the requested log4cpp::Category
81  appending a short string (using the __FUNCTION__ and __LINE__ macros)
82  with information for the calling method [produces short message].
83 */
84 
85 #define SLOG(stream, priority) \
86  (*Messenger::Instance())(stream) \
87  << priority << "[s] <" \
88  << __FUNCTION__ << " (" << __LINE__ << ")> : "
89 
90 /*!
91  \def LOG(stream, priority)
92  \brief A macro that returns the requested log4cpp::Category
93  appending a string (using the __FILE__, __FUNCTION__ and __LINE__ macros)
94  with information for the calling method [produces normal messages].
95 */
96 
97 #define LOG(stream, priority) \
98  (*Messenger::Instance())(stream) \
99  << priority << "[n] <" \
100  << __FILE__ << "::" << __FUNCTION__ << " (" << __LINE__ << ")> : "
101 
102 /*!
103  \def HIDE_GENIE_MSG_LOG_MACROS
104  \brief Use this cpp flag variable to ensure LOG_DEBUG ... LOG_FATAL macros are not exposed.
105  This allows this header to be used in conjunction with the art framework's
106  conflicting MessengeFacility's macros of the same name. The two argument
107  LOG macro (see above) is still available for use.
108  Currently this comes up only via Algorithm.h's inclusion of Algorithm.icc
109  which included Messenger.h.
110 */
111 #ifndef HIDE_GENIE_MSG_LOG_MACROS
112 
113 #ifndef HIDE_GENIE_LOG_XXX
114 #define LOG_FATAL(stream) \
115  (*Messenger::Instance())(stream) \
116  << log4cpp::Priority::FATAL << "[n] <" \
117  << __FILE__ << "::" << __FUNCTION__ << " (" << __LINE__ << ")> : "
118 
119 #define LOG_ALERT(stream) \
120  (*Messenger::Instance())(stream) \
121  << log4cpp::Priority::ALERT << "[n] <" \
122  << __FILE__ << "::" << __FUNCTION__ << " (" << __LINE__ << ")> : "
123 
124 #define LOG_CRIT(stream) \
125  (*Messenger::Instance())(stream) \
126  << log4cpp::Priority::CRIT << "[n] <" \
127  << __FILE__ << "::" << __FUNCTION__ << " (" << __LINE__ << ")> : "
128 
129 #define LOG_ERROR(stream) \
130  (*Messenger::Instance())(stream) \
131  << log4cpp::Priority::ERROR << "[n] <" \
132  << __FILE__ << "::" << __FUNCTION__ << " (" << __LINE__ << ")> : "
133 
134 #define LOG_WARN(stream) \
135  (*Messenger::Instance())(stream) \
136  << log4cpp::Priority::WARN << "[n] <" \
137  << __FILE__ << "::" << __FUNCTION__ << " (" << __LINE__ << ")> : "
138 
139 #define LOG_NOTICE(stream) \
140  (*Messenger::Instance())(stream) \
141  << log4cpp::Priority::NOTICE << "[n] <" \
142  << __FILE__ << "::" << __FUNCTION__ << " (" << __LINE__ << ")> : "
143 
144 #define LOG_INFO(stream) \
145  (*Messenger::Instance())(stream) \
146  << log4cpp::Priority::INFO << "[n] <" \
147  << __FILE__ << "::" << __FUNCTION__ << " (" << __LINE__ << ")> : "
148 
149 #define LOG_DEBUG(stream) \
150  (*Messenger::Instance())(stream) \
151  << log4cpp::Priority::DEBUG << "[n] <" \
152  << __FILE__ << "::" << __FUNCTION__ << " (" << __LINE__ << ")> : "
153 #endif
154 
155 #endif // HIDE_GENIE_MSG_LOG_MACROS
156 
157 /*!
158  \def LLOG(stream, priority)
159  \brief A macro that returns the requested log4cpp::Category
160  appending a string (using the __PRETTY_FUNCTION__ and __LINE__ macros)
161  with information for the calling method [produces long messages].
162 */
163 
164 #define LLOG(stream, priority) \
165  (*Messenger::Instance())(stream) \
166  << priority << "[l] <" \
167  << __PRETTY_FUNCTION__ << " (" << __LINE__ << ")> : "
168 
169 #define LLOG_FATAL(stream) \
170  (*Messenger::Instance())(stream) \
171  << log4cpp::Priority::FATAL << "[l] <" \
172  << __PRETTY_FUNCTION__ << " (" << __LINE__ << ")> : "
173 
174 #define LLOG_ALERT(stream) \
175  (*Messenger::Instance())(stream) \
176  << log4cpp::Priority::ALERT << "[l] <" \
177  << __PRETTY_FUNCTION__ << " (" << __LINE__ << ")> : "
178 
179 #define LLOG_CRIT(stream) \
180  (*Messenger::Instance())(stream) \
181  << log4cpp::Priority::CRIT << "[l] <" \
182  << __PRETTY_FUNCTION__ << " (" << __LINE__ << ")> : "
183 
184 #define LLOG_ERROR(stream) \
185  (*Messenger::Instance())(stream) \
186  << log4cpp::Priority::ERROR << "[l] <" \
187  << __PRETTY_FUNCTION__ << " (" << __LINE__ << ")> : "
188 
189 #define LLOG_WARN(stream) \
190  (*Messenger::Instance())(stream) \
191  << log4cpp::Priority::WARN << "'[l] <" \
192  << __PRETTY_FUNCTION__ << " (" << __LINE__ << ")> : "
193 
194 #define LLOG_NOTICE(stream) \
195  (*Messenger::Instance())(stream) \
196  << log4cpp::Priority::NOTICE << "[l] <" \
197  << __PRETTY_FUNCTION__ << " (" << __LINE__ << ")> : "
198 
199 #define LLOG_INFO(stream) \
200  (*Messenger::Instance())(stream) \
201  << log4cpp::Priority::INFO << "[l] <" \
202  << __PRETTY_FUNCTION__ << " (" << __LINE__ << ")> : "
203 
204 #define LLOG_DEBUG(stream) \
205  (*Messenger::Instance())(stream) \
206  << log4cpp::Priority::DEBUG << "[l] <" \
207  << __PRETTY_FUNCTION__ << " (" << __LINE__ << ")> : "
208 
209 /*!
210  \def BLOG(stream, priority)
211  \brief A macro that returns the requested log4cpp::Category appending no
212  additional information
213 */
214 
215 #define BLOG(stream, priority) \
216  (*Messenger::Instance())(stream) << priority
217 
218 /*!
219  \def MAXSLOG(stream, priority, maxcount)
220  \brief Similar to SLOG(stream,priority) but quits after "maxcount" messages
221 
222  \def MAXLOG(stream, priority, maxcount)
223  \brief Similar to LOG(stream,priority) but quits after "maxcount" messages
224 
225  \def MAXLLOG(stream, priority, maxcount)
226  \brief Similar to LLOG(stream,priority) but quits after "maxcount" messages
227 
228 
229 */
230 
231 // Macro to concatenate two symbols:
232 #define TOKCAT(x,y) x##y
233 // Macro to expand preprocessor variables and concatenate:
234 #define TOKCAT2(x,y) TOKCAT(x,y)
235 // Macro to concatenate source line with a symbol:
236 #define LINECAT(x) TOKCAT2(x, __LINE__ )
237 
238 #define MAXSLOG(s,l,c) \
239  static int LINECAT(MSGCNT) = 0; \
240  const char* LINECAT(MSGADD) = (++LINECAT(MSGCNT)==c) ? "..Last Message .. " : ""; \
241  if (LINECAT(MSGCNT) > c || LINECAT(MSGCNT) < 0) \
242  {;} else SLOG(s,l) << LINECAT(MSGADD)
243 
244 #define MAXLOG(s,l,c) \
245  static int LINECAT(MSGCNT) = 0; \
246  const char* LINECAT(MSGADD) = (++LINECAT(MSGCNT)==c) ? "..Last Message .. " : ""; \
247  if (LINECAT(MSGCNT) > c || LINECAT(MSGCNT) < 0) \
248  {;} else LOG(s,l) << LINECAT(MSGADD)
249 
250 #define MAXLLOG(s,l,c) \
251  static int LINECAT(MSGCNT) = 0; \
252  const char* LINECAT(MSGADD) = (++LINECAT(MSGCNT)==c) ? "..Last Message .. " : ""; \
253  if (LINECAT(MSGCNT) > c || LINECAT(MSGCNT) < 0) \
254  {;} else LLOG(s,l) << LINECAT(MSGADD)
255 
256 
257 namespace genie {
258 
259 extern bool gAbortingInErr;
260 
262 {
263 public:
264  static Messenger * Instance(void);
265 
266  log4cpp::Category & operator () (const char * stream);
267  void SetPriorityLevel(const char * stream, log4cpp::Priority::Value p);
268 
269  bool SetPrioritiesFromXmlFile(string filename);
270 
271 private:
272  Messenger();
273  Messenger(const Messenger & config_pool);
274  virtual ~Messenger();
275 
277 
278  void Configure(void);
279 
280  log4cpp::Priority::Value PriorityFromString(string priority);
281 
282  struct Cleaner {
285  if (Messenger::fInstance !=0) {
286  delete Messenger::fInstance;
287  Messenger::fInstance = 0;
288  }
289  }
290  };
291  friend struct Cleaner;
292 };
293 
294 } // genie namespace
295 #endif // _MESSENGER_H_
THE MAIN GENIE PROJECT NAMESPACE
Definition: GeneratorBase.h:8
void DummyMethodAndSilentCompiler()
Definition: Messenger.h:283
const char * p
Definition: xmltok.h:285
string filename
Definition: shutoffs.py:106
A more convenient interface to the log4cpp Message Service.
Definition: Messenger.h:261
::xsd::cxx::tree::string< char, simple_type > string
Definition: Database.h:154
void Configure(string mesg)
Definition: gEvServ.cxx:196
bool gAbortingInErr
Definition: Messenger.cxx:56
static Messenger * fInstance
Definition: Messenger.h:276