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_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 
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 
55 template <typename Base> using add_name = implementation_detail::add_name< Base, ! implementation_detail::has_name<Base>::value >;
56 template <typename Base> using add_serviceLocator = implementation_detail::add_serviceLocator< Base, ! implementation_detail::has_serviceLocator<Base>::value >;
57 
58 template <typename Base> class CommonMessaging;
59 
61 public:
63  virtual ~CommonMessagingBase() = default;
65  virtual void create_msgSvc() const = 0;
66  virtual void create_msgStream() const = 0;
67 
68 
69 
73  inline SmartIF<IMessageSvc>& msgSvc() const {
74  if (UNLIKELY(!m_msgsvc)) create_msgSvc();
75  return m_msgsvc;
76  }
77 
78 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NO_DEPRECATED)
79 
84  [[deprecated("will be removed in v28r1, use msgSvc() instead, see https://gitlab.cern.ch/gaudi/Gaudi/merge_requests/80")]]
85  inline SmartIF<IMessageSvc>& messageService() const {
86  return msgSvc();
87  }
88 #endif
89 
91  inline MsgStream& msgStream() const {
92  if (UNLIKELY((m_createMsgStream))) create_msgStream();
93  return *m_msgStream;
94  }
95 
106  inline MsgStream& msgStream(const MSG::Level level) const {
107  return msgStream() << level;
108  }
109 
111  inline MsgStream& always() const { return msgStream(MSG::ALWAYS); }
112 
114  inline MsgStream& fatal() const { return msgStream(MSG::FATAL); }
115 
117  inline MsgStream& err() const { return msgStream(MSG::ERROR); }
118 
120  inline MsgStream& error() const { return msgStream(MSG::ERROR); }
121 
123  inline MsgStream& warning() const { return msgStream(MSG::WARNING); }
124 
126  inline MsgStream& info() const { return msgStream(MSG::INFO); }
127 
129  inline MsgStream& debug() const { return msgStream(MSG::DEBUG); }
130 
132  inline MsgStream& verbose() const { return msgStream(MSG::VERBOSE); }
133 
135  inline MsgStream& msg() const { return msgStream(MSG::INFO); }
136 
138  inline MSG::Level msgLevel() const {
139  if (UNLIKELY(!m_msgStream)) create_msgStream();
140  return m_level;
141  }
142 
144  inline MSG::Level outputLevel() const __attribute__ ((deprecated)) { return msgLevel(); }
145 
147  inline bool msgLevel(MSG::Level lvl) const { return UNLIKELY(msgLevel() <= lvl); }
148 
149 private:
150  template <typename Base> friend class CommonMessaging;
151 
154 
157  mutable bool m_createMsgStream = true;
158 
161 
162 
163 };
164 
165 template <typename BASE>
166 class GAUDI_API CommonMessaging: public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase {
167 public:
169 
172 
173 private:
174  // out-of-line 'cold' functions -- put here so as to not blow up the inline 'hot' functions
175  void create_msgSvc() const override final {
176  // Get default implementation of the message service.
177  m_msgsvc = this->serviceLocator();
178  }
179  void create_msgStream() const override final {
180  auto& ms = msgSvc();
181  m_msgStream.reset(new MsgStream(ms, this->name()));
182  m_createMsgStream = (!ms.isValid() || !m_msgStream);
183  m_level = m_msgStream ? m_msgStream->level() : MSG::NIL;
184  }
185 
186 protected:
190  if (level != MSG::NIL && level != m_level) {
191  if (msgSvc()) {
192  msgSvc()->setOutputLevel(this->name(), level);
193  }
194  if (m_msgStream) m_msgStream->setLevel(level);
195  if (UNLIKELY(MSG::Level(level) <= MSG::DEBUG))
196  debug() << "Property update for OutputLevel : new value = " << level << endmsg;
197  m_level = MSG::Level(level);
198  }
199  }
200 
201 };
202 
203 
204 #endif /* COMMONMESSAGING_H_ */
#define generate_add_(method, ret, args)
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
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)
SmartIF< IMessageSvc > m_msgsvc
Pointer to the message service;.
#define UNLIKELY(x)
Definition: Kernel.h:126
STL namespace.
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)
generate_has_(name,()) generate_add_(name
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
implementation_detail::add_serviceLocator< Base,!implementation_detail::has_serviceLocator< Base >::value > add_serviceLocator
MSG::Level outputLevel() const __attribute__((deprecated))
Backward compatibility function for getting the output level.
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
implementation_detail::add_name< Base,!implementation_detail::has_name< Base >::value > add_name
MsgStream & msgStream() const
Return an uninitialized MsgStream.
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
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