CommonMessaging.h
Go to the documentation of this file.
1 /*
2  * CommonMessaging.h
3  *
4  * Created on: Feb 20, 2009
5  * Author: Marco Clemencic
6  */
7 
8 #ifndef COMMONMESSAGING_H_
9 #define COMMONMESSAGING_H_
10 #include "GaudiKernel/IService.h"
11 #include "GaudiKernel/ISvcLocator.h"
12 #include "GaudiKernel/IMessageSvc.h"
13 #include "GaudiKernel/MsgStream.h"
14 #include "GaudiKernel/SmartIF.h"
15 #include "GaudiKernel/GaudiException.h"
16 
17 #include <memory>
18 
22 template <class BASE>
23 class GAUDI_API CommonMessaging: public BASE {
24 public:
25  typedef CommonMessaging base_class;
26 
28  template <typename A1, typename A2, typename A3> CommonMessaging(const A1& a1, const A2& a2, const A3& a3):
29  BASE(a1,a2,a3), m_streamWithService(false) {}
31  template <typename A1, typename A2> CommonMessaging(const A1& a1, const A2& a2):
32  BASE(a1, a2), m_streamWithService(false) {}
34  template <typename A1> CommonMessaging(const A1& a1):
35  BASE(a1), m_streamWithService(false) {}
38  BASE(), m_streamWithService(false) {}
39 
41  virtual ~CommonMessaging() {}
42 
44  virtual SmartIF<ISvcLocator>& serviceLocator() const = 0;
45 
47  virtual const std::string& name() const = 0;
48 
52  inline SmartIF<IMessageSvc>& msgSvc() const {
53  if (!m_msgsvc.get()) {
54  // Get default implementation of the message service.
55  m_msgsvc = this->serviceLocator();
56  }
57  return m_msgsvc;
58  }
59 
60 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NO_DEPRECATED)
61 
65  inline SmartIF<IMessageSvc>& messageService() const {
66  return msgSvc();
67  }
68 #endif
69 
71  inline MsgStream& msgStream() const {
72  if (UNLIKELY((!m_msgStream.get()) || (!m_streamWithService))) {
73  SmartIF<IMessageSvc>& ms = msgSvc();
74  m_msgStream.reset(new MsgStream(ms, this->name()));
75  m_streamWithService = ms.get() != 0;
76  }
77  return *m_msgStream;
78  }
79 
90  inline MsgStream& msgStream(const MSG::Level level) const {
91  return msgStream() << level;
92  }
93 
95  inline MsgStream& always() const { return msgStream(MSG::ALWAYS); }
96 
98  inline MsgStream& fatal() const { return msgStream(MSG::FATAL); }
99 
101  inline MsgStream& err() const { return msgStream(MSG::ERROR); }
102 
104  inline MsgStream& error() const { return msgStream(MSG::ERROR); }
105 
107  inline MsgStream& warning() const { return msgStream(MSG::WARNING); }
108 
110  inline MsgStream& info() const { return msgStream(MSG::INFO); }
111 
113  inline MsgStream& debug() const { return msgStream(MSG::DEBUG); }
114 
116  inline MsgStream& verbose() const { return msgStream(MSG::VERBOSE); }
117 
119  inline MsgStream& msg() const { return msgStream(MSG::INFO); }
120 
122  inline MSG::Level msgLevel() const {
123  return msgStream().level();
124  }
125 
127  inline bool msgLevel(MSG::Level lvl) const {
128  return UNLIKELY(msgLevel() <= lvl);
129  }
130 
131 protected:
133  mutable SmartIF<IMessageSvc> m_msgsvc;
134 
136  mutable std::auto_ptr<MsgStream> m_msgStream;
137 
139  mutable bool m_streamWithService;
140 
143  void updateMsgStreamOutputLevel(int level) {
144  if (m_msgStream.get()) m_msgStream->setLevel(level);
145  }
146 
147 };
148 
149 
150 #endif /* COMMONMESSAGING_H_ */
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
#define GAUDI_API
Definition: Kernel.h:108
return false
Definition: Bootstrap.cpp:338
TYPE * get() const
Get interface pointer.
Definition: SmartIF.h:62
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:106
Templated class to add the standard messaging functionalities.
#define UNLIKELY(x)
Definition: Kernel.h:127
constexpr double ms