Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  v31r0 (aeb156f0)
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_
12 #include "GaudiKernel/IService.h"
14 #include "GaudiKernel/MsgStream.h"
15 #include "GaudiKernel/SmartIF.h"
16 #include "GaudiKernel/detected.h"
17 #include <boost/thread/tss.hpp>
18 
19 #include <memory>
20 #include <utility>
21 
26 #define generate_( method, ret, args ) \
27  \
28  template <typename T> \
29  using _has_##method = decltype( std::declval<const T&>().method args ); \
30  \
31  template <typename T> \
32  using has_##method = Gaudi::cpp17::is_detected<_has_##method, T>; \
33  \
34  template <typename Base> \
35  struct add_##method : public Base { \
36  using Base::Base; \
37  virtual ~add_##method() = default; \
38  virtual ret method args const = 0; \
39  };
40 
42  generate_( name, const std::string&, () )
43 
44  generate_( serviceLocator, SmartIF<ISvcLocator>&, () )
45 }
46 #undef generate_
47 
48 template <typename Base>
50  implementation_detail::add_name<Base>>::type;
51 template <typename Base>
53  implementation_detail::add_serviceLocator<Base>>::type;
54 
55 template <typename Base>
57 
59 public:
61  virtual ~CommonMessagingBase() = default;
63  virtual void create_msgStream() const = 0;
64 
68  inline const SmartIF<IMessageSvc>& msgSvc() const { return m_msgsvc; }
69 
71  inline MsgStream& msgStream() const {
72  if ( UNLIKELY( ( !m_msgStream.get() ) ) ) create_msgStream();
73  return *m_msgStream;
74  }
75 
86  inline MsgStream& msgStream( const MSG::Level level ) const { return msgStream() << level; }
87 
89  inline MsgStream& always() const { return msgStream( MSG::ALWAYS ); }
90 
92  inline MsgStream& fatal() const { return msgStream( MSG::FATAL ); }
93 
95  inline MsgStream& err() const { return msgStream( MSG::ERROR ); }
96 
98  inline MsgStream& error() const { return msgStream( MSG::ERROR ); }
99 
101  inline MsgStream& warning() const { return msgStream( MSG::WARNING ); }
102 
104  inline MsgStream& info() const { return msgStream( MSG::INFO ); }
105 
107  inline MsgStream& debug() const { return msgStream( MSG::DEBUG ); }
108 
110  inline MsgStream& verbose() const { return msgStream( MSG::VERBOSE ); }
111 
113  inline MsgStream& msg() const { return msgStream( MSG::INFO ); }
114 
115 private:
116  template <typename Base>
117  friend class CommonMessaging;
118 
119  mutable bool m_commonMessagingReady = false;
121 
123  mutable boost::thread_specific_ptr<MsgStream> m_msgStream;
124 
127 };
128 
129 template <typename BASE>
130 class GAUDI_API CommonMessaging : public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase {
131 public:
133 
136 
138  inline MSG::Level msgLevel() const {
139  if ( LIKELY( m_commonMessagingReady ) ) return m_level;
140  return setUpMessaging();
141  }
142 
144  [[deprecated( "please use msgLevel() instead of outputLevel()" )]] inline MSG::Level outputLevel() const {
145  return msgLevel();
146  }
147 
149  inline bool msgLevel( MSG::Level lvl ) const { return UNLIKELY( msgLevel() <= lvl ); }
150 
151 private:
152  // out-of-line 'cold' functions -- put here so as to not blow up the inline 'hot' functions
153  void create_msgStream() const override final { m_msgStream.reset( new MsgStream( msgSvc(), this->name() ) ); }
154 
156  void initMessaging() const {
157  if ( !m_msgsvc ) {
158  // Get default implementation of the message service.
159  m_msgsvc = this->serviceLocator();
160  }
161  create_msgStream();
162  m_level = MSG::Level( m_msgStream.get() ? m_msgStream->level() : MSG::NIL );
163  // if we could not get a MessageSvc, we should try again the initial set up
164  m_commonMessagingReady = m_msgsvc;
165  }
166 
167 protected:
170  if ( UNLIKELY( !m_commonMessagingReady ) ) { initMessaging(); }
171  return m_level;
172  }
175  m_commonMessagingReady = false;
176  return setUpMessaging();
177  }
181  setUpMessaging();
182  if ( level != MSG::NIL && level != m_level ) {
183  if ( msgSvc() ) msgSvc()->setOutputLevel( this->name(), level );
184  if ( m_msgStream.get() ) m_msgStream->setLevel( level );
185  if ( UNLIKELY( MSG::Level( level ) <= MSG::DEBUG ) )
186  debug() << "Property update for OutputLevel : new value = " << level << endmsg;
187  m_level = MSG::Level( level );
188  }
189  }
190 };
191 
192 #endif /* COMMONMESSAGING_H_ */
#define UNLIKELY(x)
Definition: Kernel.h:89
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
MSG::Level resetMessaging()
Reinitialize internal states.
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;.
typename std::conditional< implementation_detail::has_name< Base >::value, Base, implementation_detail::add_name< Base >>::type add_name
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)
bool msgLevel(MSG::Level lvl) const
get the output level from the embedded MsgStream
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
MSG::Level outputLevel() const
Backward compatibility function for getting the output level.
MSG::Level setUpMessaging() const
Set up local caches.
boost::thread_specific_ptr< MsgStream > m_msgStream
The predefined message stream.
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
void create_msgStream() const override final
cold functionality
#define LIKELY(x)
Definition: Kernel.h:88
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
generate_(name, const std::string &,()) generate_(serviceLocator
MsgStream & msgStream() const
Return an uninitialized MsgStream.
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)
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
#define GAUDI_API
Definition: Kernel.h:71
typename std::conditional< implementation_detail::has_serviceLocator< Base >::value, Base, implementation_detail::add_serviceLocator< Base >>::type add_serviceLocator
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
void initMessaging() const
Initialise the messaging objects.
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)