The Gaudi Framework  v29r0 (ff2e7097)
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 <boost/thread/tss.hpp>
17 
18 #include <memory>
19 #include <utility>
20 
25 #define generate_( method, ret, args ) \
26  \
27  template <typename T, typename SFINAE = void> \
28  struct has_##method : std::false_type { \
29  }; \
30  template <typename T> \
31  struct has_##method<T, is_valid_t<decltype( std::declval<const T&>().method args )>> : std::true_type { \
32  }; \
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  template <typename>
44  struct void_t {
45  using type = void;
46  };
47  template <typename T>
48  using is_valid_t = typename void_t<T>::type;
49 
50  generate_( name, const std::string&, () ) generate_( serviceLocator, SmartIF<ISvcLocator>&, () )
51 }
52 #undef generate_
53 
54 template <typename Base>
56  implementation_detail::add_name<Base>>::type;
57 template <typename Base>
59  implementation_detail::add_serviceLocator<Base>>::type;
60 
61 template <typename Base>
63 
65 {
66 public:
68  virtual ~CommonMessagingBase() = default;
70  virtual void create_msgSvc() const = 0;
71  virtual void create_msgStream() const = 0;
72 
76  inline SmartIF<IMessageSvc>& msgSvc() const
77  {
78  if ( UNLIKELY( !m_msgsvc ) ) create_msgSvc();
79  return m_msgsvc;
80  }
81 
83  inline MsgStream& msgStream() const
84  {
85  if ( UNLIKELY( ( !m_msgStream.get() ) ) ) create_msgStream();
86  return *m_msgStream;
87  }
88 
99  inline MsgStream& msgStream( const MSG::Level level ) const { return msgStream() << level; }
100 
102  inline MsgStream& always() const { return msgStream( MSG::ALWAYS ); }
103 
105  inline MsgStream& fatal() const { return msgStream( MSG::FATAL ); }
106 
108  inline MsgStream& err() const { return msgStream( MSG::ERROR ); }
109 
111  inline MsgStream& error() const { return msgStream( MSG::ERROR ); }
112 
114  inline MsgStream& warning() const { return msgStream( MSG::WARNING ); }
115 
117  inline MsgStream& info() const { return msgStream( MSG::INFO ); }
118 
120  inline MsgStream& debug() const { return msgStream( MSG::DEBUG ); }
121 
123  inline MsgStream& verbose() const { return msgStream( MSG::VERBOSE ); }
124 
126  inline MsgStream& msg() const { return msgStream( MSG::INFO ); }
127 
129  inline MSG::Level msgLevel() const
130  {
131  if ( UNLIKELY( ( !m_msgStream.get() ) ) ) create_msgStream();
132  if ( UNLIKELY( ( !m_level.get() ) ) ) {
133  return MSG::NIL;
134  }
135  return *m_level;
136  }
137 
139  inline MSG::Level outputLevel() const __attribute__( ( deprecated ) ) { return msgLevel(); }
140 
142  inline bool msgLevel( MSG::Level lvl ) const { return UNLIKELY( msgLevel() <= lvl ); }
143 
144 private:
145  template <typename Base>
146  friend class CommonMessaging;
147 
149  mutable boost::thread_specific_ptr<MsgStream> m_msgStream;
150 
151  // mutable MSG::Level m_level = MSG::NIL;
152  mutable boost::thread_specific_ptr<MSG::Level> m_level;
153 
156 };
157 
158 template <typename BASE>
159 class GAUDI_API CommonMessaging : public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase
160 {
161 public:
163 
166 
167 private:
168  // out-of-line 'cold' functions -- put here so as to not blow up the inline 'hot' functions
169  void create_msgSvc() const override final
170  {
171  // Get default implementation of the message service.
172  m_msgsvc = this->serviceLocator();
173  }
174  void create_msgStream() const override final
175  {
176  auto& ms = msgSvc();
177  m_msgStream.reset( new MsgStream( ms, this->name() ) );
178  m_level.reset( new MSG::Level( m_msgStream.get() ? m_msgStream->level() : MSG::NIL ) );
179  }
180 
181 protected:
185  {
186  if ( !m_msgStream.get() ) {
187  create_msgStream();
188  }
189  if ( level != MSG::NIL && level != *m_level ) {
190  if ( msgSvc() ) {
191  msgSvc()->setOutputLevel( this->name(), level );
192  }
193  if ( m_msgStream.get() ) m_msgStream->setLevel( level );
194  if ( UNLIKELY( MSG::Level( level ) <= MSG::DEBUG ) )
195  debug() << "Property update for OutputLevel : new value = " << level << endmsg;
196  *m_level = MSG::Level( level );
197  }
198  }
199 };
200 
201 #endif /* COMMONMESSAGING_H_ */
#define UNLIKELY(x)
Definition: Kernel.h:128
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:15
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
#define __attribute__(x)
Definition: System.cpp:79
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;.
class MergingTransformer< Out(const vector_of_const_< In > void
typename std::conditional< implementation_detail::has_name< Base >::value, Base, implementation_detail::add_name< Base >>::type add_name
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.
boost::thread_specific_ptr< MSG::Level > m_level
boost::thread_specific_ptr< MsgStream > m_msgStream
The predefined message stream.
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 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:110
typename std::conditional< implementation_detail::has_serviceLocator< Base >::value, Base, implementation_detail::add_serviceLocator< Base >>::type add_serviceLocator
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:209