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 #include <utility>
19 
24 #define generate_has_( method, args ) \
25  template <typename T, typename SFINAE = void> \
26  struct has_ ## method : std::false_type {}; \
27  template <typename T> \
28  struct has_ ## method<T, is_valid_t<decltype(std::declval<const T&>().method args)>> : std::true_type {};
29 
30 #define generate_add_(method, ret, args ) \
31  template <typename Base, bool> struct add_ ## method; \
32  template <typename Base> \
33  struct add_ ## method <Base,false> : public Base { \
34  using Base::Base; \
35  }; \
36  template <typename Base> \
37  struct add_ ## method <Base, true> : public Base { \
38  using Base::Base; \
39  virtual ~add_ ## method () = default; \
40  virtual ret method args const = 0; \
41  };
42 
43 namespace implementation_detail {
44  template <typename> struct void_t { typedef void type; };
45  template <typename T> using is_valid_t = typename void_t<T>::type;
46 
47  generate_has_( name, () )
48  generate_add_( name, const std::string&, () )
49  generate_has_( serviceLocator, () )
50  generate_add_( serviceLocator, SmartIF<ISvcLocator>&, () )
51 }
52 #undef generate_has_
53 #undef generate_add_
54 
57 
58 
59 template <typename BASE>
60 class GAUDI_API CommonMessaging: public add_serviceLocator<add_name<BASE>> {
61 public:
62  using base_class = CommonMessaging;
63 
66 
68  ~CommonMessaging() override = default;
69 
73  inline SmartIF<IMessageSvc>& msgSvc() const {
74  if (!m_msgsvc) {
75  // Get default implementation of the message service.
76  m_msgsvc = this->serviceLocator();
77  }
78  return m_msgsvc;
79  }
80 
81 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NO_DEPRECATED)
82 
86  inline SmartIF<IMessageSvc>& messageService() const {
87  return msgSvc();
88  }
89 #endif
90 
92  inline MsgStream& msgStream() const {
93  if (UNLIKELY((!m_msgStream) || (!m_streamWithService))) {
94  auto& ms = msgSvc();
95  m_msgStream.reset(new MsgStream(ms, this->name()));
96  m_streamWithService = ms.isValid();
97  }
98  return *m_msgStream;
99  }
100 
111  inline MsgStream& msgStream(const MSG::Level level) const {
112  return msgStream() << level;
113  }
114 
116  inline MsgStream& always() const { return msgStream(MSG::ALWAYS); }
117 
119  inline MsgStream& fatal() const { return msgStream(MSG::FATAL); }
120 
122  inline MsgStream& err() const { return msgStream(MSG::ERROR); }
123 
125  inline MsgStream& error() const { return msgStream(MSG::ERROR); }
126 
128  inline MsgStream& warning() const { return msgStream(MSG::WARNING); }
129 
131  inline MsgStream& info() const { return msgStream(MSG::INFO); }
132 
134  inline MsgStream& debug() const { return msgStream(MSG::DEBUG); }
135 
137  inline MsgStream& verbose() const { return msgStream(MSG::VERBOSE); }
138 
140  inline MsgStream& msg() const { return msgStream(MSG::INFO); }
141 
143  inline MSG::Level msgLevel() const {
144  return msgStream().level();
145  }
146 
148  inline bool msgLevel(MSG::Level lvl) const {
149  return UNLIKELY(msgLevel() <= lvl);
150  }
151 
152 protected:
154  mutable SmartIF<IMessageSvc> m_msgsvc;
155 
157  mutable std::unique_ptr<MsgStream> m_msgStream;
158 
160  mutable bool m_streamWithService = false;
161 
164  void updateMsgStreamOutputLevel(int level) {
165  if (m_msgStream) m_msgStream->setLevel(level);
166  }
167 
168 };
169 
170 
171 #endif /* COMMONMESSAGING_H_ */
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
#define GAUDI_API
Definition: Kernel.h:107
STL namespace.
generate_has_(name,()) generate_add_(name
#define generate_add_(method, ret, args)
implementation_detail::add_serviceLocator< Base,!implementation_detail::has_serviceLocator< Base >::value > add_serviceLocator
void setLevel(int level)
Update outputlevel.
Definition: MsgStream.h:106
typename void_t< T >::type is_valid_t
#define UNLIKELY(x)
Definition: Kernel.h:126
implementation_detail::add_name< Base,!implementation_detail::has_name< Base >::value > add_name
constexpr double ms
string type
Definition: gaudirun.py:151