All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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"
13 #include "GaudiKernel/MsgStream.h"
14 #include "GaudiKernel/SmartIF.h"
16 
17 #include <memory>
18 #include <utility>
19 
24 #define generate_( method, ret, args ) \
25  \
26  template <typename T, typename SFINAE = void> \
27  struct has_ ## method : std::false_type {}; \
28  template <typename T> \
29  struct has_ ## method<T, is_valid_t<decltype(std::declval<const T&>().method args)>> : std::true_type {}; \
30  \
31  template <typename Base> \
32  struct add_ ## method : public Base { \
33  using Base::Base; \
34  virtual ~add_ ## method () = default; \
35  virtual ret method args const = 0; \
36  };
37 
39  template <typename> struct void_t { using type = void; };
40  template <typename T> using is_valid_t = typename void_t<T>::type;
41 
42  generate_( name, const std::string&, () )
43  generate_( serviceLocator, SmartIF<ISvcLocator>&, () )
44 }
45 #undef generate_
46 
47 template <typename Base> using add_name = typename std::conditional<implementation_detail::has_name<Base>::value,
48  Base,
49  implementation_detail::add_name< Base>>::type;
51  Base,
52  implementation_detail::add_serviceLocator< Base>>::type;
53 
54 
55 template <typename Base> class CommonMessaging;
56 
58 public:
60  virtual ~CommonMessagingBase() = default;
62  virtual void create_msgSvc() const = 0;
63  virtual void create_msgStream() const = 0;
64 
65 
66 
70  inline SmartIF<IMessageSvc>& msgSvc() const {
71  if (UNLIKELY(!m_msgsvc)) create_msgSvc();
72  return m_msgsvc;
73  }
74 
75 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NO_DEPRECATED)
76 
81  [[deprecated("will be removed in v29r0, use msgSvc() instead, see https://gitlab.cern.ch/gaudi/Gaudi/merge_requests/80")]]
82  inline SmartIF<IMessageSvc>& messageService() const {
83  return msgSvc();
84  }
85 #endif
86 
88  inline MsgStream& msgStream() const {
89  if (UNLIKELY((m_createMsgStream))) create_msgStream();
90  return *m_msgStream;
91  }
92 
103  inline MsgStream& msgStream(const MSG::Level level) const {
104  return msgStream() << level;
105  }
106 
108  inline MsgStream& always() const { return msgStream(MSG::ALWAYS); }
109 
111  inline MsgStream& fatal() const { return msgStream(MSG::FATAL); }
112 
114  inline MsgStream& err() const { return msgStream(MSG::ERROR); }
115 
117  inline MsgStream& error() const { return msgStream(MSG::ERROR); }
118 
120  inline MsgStream& warning() const { return msgStream(MSG::WARNING); }
121 
123  inline MsgStream& info() const { return msgStream(MSG::INFO); }
124 
126  inline MsgStream& debug() const { return msgStream(MSG::DEBUG); }
127 
129  inline MsgStream& verbose() const { return msgStream(MSG::VERBOSE); }
130 
132  inline MsgStream& msg() const { return msgStream(MSG::INFO); }
133 
135  inline MSG::Level msgLevel() const {
136  if (UNLIKELY(!m_msgStream)) create_msgStream();
137  return m_level;
138  }
139 
141  inline MSG::Level outputLevel() const __attribute__ ((deprecated)) { return msgLevel(); }
142 
144  inline bool msgLevel(MSG::Level lvl) const { return UNLIKELY(msgLevel() <= lvl); }
145 
146 private:
147  template <typename Base> friend class CommonMessaging;
148 
151 
154  mutable bool m_createMsgStream = true;
155 
158 
159 
160 };
161 
162 template <typename BASE>
163 class GAUDI_API CommonMessaging: public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase {
164 public:
166 
169 
170 private:
171  // out-of-line 'cold' functions -- put here so as to not blow up the inline 'hot' functions
172  void create_msgSvc() const override final {
173  // Get default implementation of the message service.
174  m_msgsvc = this->serviceLocator();
175  }
176  void create_msgStream() const override final {
177  auto& ms = msgSvc();
178  m_msgStream.reset(new MsgStream(ms, this->name()));
179  m_createMsgStream = (!ms.isValid() || !m_msgStream);
180  m_level = m_msgStream ? m_msgStream->level() : MSG::NIL;
181  }
182 
183 protected:
187  if (level != MSG::NIL && level != m_level) {
188  if (msgSvc()) {
189  msgSvc()->setOutputLevel(this->name(), level);
190  }
191  if (m_msgStream) m_msgStream->setLevel(level);
192  if (UNLIKELY(MSG::Level(level) <= MSG::DEBUG))
193  debug() << "Property update for OutputLevel : new value = " << level << endmsg;
194  m_level = MSG::Level(level);
195  }
196  }
197 
198 };
199 
200 
201 #endif /* COMMONMESSAGING_H_ */
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
typename std::conditional< implementation_detail::has_serviceLocator< Base >::value, Base, implementation_detail::add_serviceLocator< Base >>::type add_serviceLocator
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
#define __attribute__(x)
Definition: System.cpp:72
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
struct deprecated("use MergingTransformer instead")]] ListTransformer
SmartIF< IMessageSvc > m_msgsvc
Pointer to the message service;.
#define UNLIKELY(x)
Definition: Kernel.h:126
class MergingTransformer< Out(const vector_of_const_< In > void
std::unique_ptr< MsgStream > m_msgStream
The predefined message stream.
void create_msgSvc() const override final
cold functionality
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
STL class.
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
void create_msgStream() const override final
MSG::Level outputLevel() const __attribute__((deprecated))
Backward compatibility function for getting the output level.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
generate_(name, const std::string &,()) generate_(serviceLocator
MsgStream & msgStream() const
Return an uninitialized MsgStream.
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
typename std::conditional< implementation_detail::has_name< Base >::value, Base, implementation_detail::add_name< Base >>::type add_name
typename void_t< T >::type is_valid_t
MsgStream & msgStream(const MSG::Level level) const
Predefined configurable message stream for the efficient printouts.
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
#define GAUDI_API
Definition: Kernel.h:107
bool m_createMsgStream
Flag to trigger a new MsgStream.
bool msgLevel(MSG::Level lvl) const
get the output level from the embedded MsgStream
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244