The Gaudi Framework  v30r3 (a5ef0a68)
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 {
43  generate_( name, const std::string&, () )
44 
45  generate_( serviceLocator, SmartIF<ISvcLocator>&, () )
46 }
47 #undef generate_
48 
49 template <typename Base>
51  implementation_detail::add_name<Base>>::type;
52 template <typename Base>
54  implementation_detail::add_serviceLocator<Base>>::type;
55 
56 template <typename Base>
58 
60 {
61 public:
63  virtual ~CommonMessagingBase() = default;
65  virtual void create_msgStream() const = 0;
66 
70  inline const SmartIF<IMessageSvc>& msgSvc() const { return m_msgsvc; }
71 
73  inline MsgStream& msgStream() const
74  {
75  if ( UNLIKELY( ( !m_msgStream.get() ) ) ) create_msgStream();
76  return *m_msgStream;
77  }
78 
89  inline MsgStream& msgStream( const MSG::Level level ) const { return msgStream() << level; }
90 
92  inline MsgStream& always() const { return msgStream( MSG::ALWAYS ); }
93 
95  inline MsgStream& fatal() const { return msgStream( MSG::FATAL ); }
96 
98  inline MsgStream& err() const { return msgStream( MSG::ERROR ); }
99 
101  inline MsgStream& error() const { return msgStream( MSG::ERROR ); }
102 
104  inline MsgStream& warning() const { return msgStream( MSG::WARNING ); }
105 
107  inline MsgStream& info() const { return msgStream( MSG::INFO ); }
108 
110  inline MsgStream& debug() const { return msgStream( MSG::DEBUG ); }
111 
113  inline MsgStream& verbose() const { return msgStream( MSG::VERBOSE ); }
114 
116  inline MsgStream& msg() const { return msgStream( MSG::INFO ); }
117 
118 private:
119  template <typename Base>
120  friend class CommonMessaging;
121 
122  mutable bool m_commonMessagingReady = false;
124 
126  mutable boost::thread_specific_ptr<MsgStream> m_msgStream;
127 
130 };
131 
132 template <typename BASE>
133 class GAUDI_API CommonMessaging : public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase
134 {
135 public:
137 
140 
142  inline MSG::Level msgLevel() const
143  {
144  if ( LIKELY( m_commonMessagingReady ) ) return m_level;
145  return setUpMessaging();
146  }
147 
149  [[deprecated( "please use msgLevel() instead of outputLevel()" )]] inline MSG::Level outputLevel() const
150  {
151  return msgLevel();
152  }
153 
155  inline bool msgLevel( MSG::Level lvl ) const { return UNLIKELY( msgLevel() <= lvl ); }
156 
157 private:
158  // out-of-line 'cold' functions -- put here so as to not blow up the inline 'hot' functions
159  void create_msgStream() const override final { m_msgStream.reset( new MsgStream( msgSvc(), this->name() ) ); }
160 
162  void initMessaging() const
163  {
164  if ( !m_msgsvc ) {
165  // Get default implementation of the message service.
166  m_msgsvc = this->serviceLocator();
167  }
168  create_msgStream();
169  m_level = MSG::Level( m_msgStream.get() ? m_msgStream->level() : MSG::NIL );
170  // if we could not get a MessageSvc, we should try again the initial set up
171  m_commonMessagingReady = m_msgsvc;
172  }
173 
174 protected:
177  {
178  if ( UNLIKELY( !m_commonMessagingReady ) ) {
179  initMessaging();
180  }
181  return m_level;
182  }
185  {
186  m_commonMessagingReady = false;
187  return setUpMessaging();
188  }
192  {
193  setUpMessaging();
194  if ( level != MSG::NIL && level != m_level ) {
195  if ( msgSvc() ) msgSvc()->setOutputLevel( this->name(), level );
196  if ( m_msgStream.get() ) m_msgStream->setLevel( level );
197  if ( UNLIKELY( MSG::Level( level ) <= MSG::DEBUG ) )
198  debug() << "Property update for OutputLevel : new value = " << level << endmsg;
199  m_level = MSG::Level( level );
200  }
201  }
202 };
203 
204 #endif /* COMMONMESSAGING_H_ */
#define UNLIKELY(x)
Definition: Kernel.h:122
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)
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:121
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:104
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:209
void initMessaging() const
Initialise the messaging objects.
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)