| The Gaudi Framework v32r1 (f65d50dc)

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/GaudiException.h"
11 #include "GaudiKernel/IMessageSvc.h"
12 #include "GaudiKernel/IService.h"
13 #include "GaudiKernel/ISvcLocator.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
41 namespace implementation_detail {
42 generate_( name, const std::string&, () )
43
44 generate_( serviceLocator, SmartIF<ISvcLocator>&, () )
45 }
46 #undef generate_
47
48 template <typename Base>
49 using add_name =
50 std::conditional_t<implementation_detail::has_name<Base>::value, Base, implementation_detail::add_name<Base>>;
51 template <typename Base>
52 using add_serviceLocator = std::conditional_t<implementation_detail::has_serviceLocator<Base>::value, Base,
53 implementation_detail::add_serviceLocator<Base>>;
54
55 template <typename Base>
56 class CommonMessaging;
57
58 class CommonMessagingBase {
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;
120 mutable MSG::Level m_level = MSG::NIL;
121
123 mutable boost::thread_specific_ptr<MsgStream> m_msgStream;
124
126 mutable SmartIF<IMessageSvc> m_msgsvc;
127 };
128
129 template <typename BASE>
130 class GAUDI_API CommonMessaging : public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase {
131 public:
132 using base_class = CommonMessaging;
133
135 using add_serviceLocator<add_name<BASE>>::add_serviceLocator;
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:
169 MSG::Level setUpMessaging() const {
170 if ( UNLIKELY( !m_commonMessagingReady ) ) { initMessaging(); }
171 return m_level;
172 }
174 MSG::Level resetMessaging() {
175 m_commonMessagingReady = false;
176 return setUpMessaging();
177 }
180 void updateMsgStreamOutputLevel( int level ) {
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_ */
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:96
detected.h
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
CommonMessagingBase::m_commonMessagingReady
bool m_commonMessagingReady
Definition: CommonMessaging.h:119
SmartIF< ISvcLocator >
CommonMessaging::create_msgStream
void create_msgStream() const override final
cold functionality
Definition: CommonMessaging.h:153
CommonMessagingBase::msgSvc
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: CommonMessaging.h:68
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:41
MsgStream.h
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:101
CommonMessagingBase::CommonMessaging
friend class CommonMessaging
Definition: CommonMessaging.h:117
CommonMessaging::initMessaging
void initMessaging() const
Initialise the messaging objects.
Definition: CommonMessaging.h:156
CommonMessaging::resetMessaging
MSG::Level resetMessaging()
Reinitialize internal states.
Definition: CommonMessaging.h:174
MSG::DEBUG
Definition: IMessageSvc.h:15
CommonMessagingBase::m_msgsvc
SmartIF< IMessageSvc > m_msgsvc
Pointer to the message service;.
Definition: CommonMessaging.h:126
CommonMessagingBase::always
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
Definition: CommonMessaging.h:89
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:104
MSG::VERBOSE
Definition: IMessageSvc.h:15
CommonMessaging::outputLevel
MSG::Level outputLevel() const
Backward compatibility function for getting the output level.
Definition: CommonMessaging.h:144
CommonMessaging::setUpMessaging
MSG::Level setUpMessaging() const
Set up local caches.
Definition: CommonMessaging.h:169
CommonMessaging::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:138
std::string
STL class.
MSG::FATAL
Definition: IMessageSvc.h:15
MSG::Level
Level
Definition: IMessageSvc.h:15
CommonMessagingBase::m_level
MSG::Level m_level
Definition: CommonMessaging.h:120
add_serviceLocator
std::conditional_t< implementation_detail::has_serviceLocator< Base >::value, Base, implementation_detail::add_serviceLocator< Base > > add_serviceLocator
Definition: CommonMessaging.h:53
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:98
CommonMessagingBase::create_msgStream
virtual void create_msgStream() const =0
cold functionality
CommonMessaging::updateMsgStreamOutputLevel
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
Definition: CommonMessaging.h:180
GaudiException.h
SmartIF.h
CommonMessagingBase::verbose
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
Definition: CommonMessaging.h:110
MSG::NIL
Definition: IMessageSvc.h:15
CommonMessaging::msgLevel
bool msgLevel(MSG::Level lvl) const
get the output level from the embedded MsgStream
Definition: CommonMessaging.h:149
CommonMessagingBase::m_msgStream
boost::thread_specific_ptr< MsgStream > m_msgStream
The predefined message stream.
Definition: CommonMessaging.h:123
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:107
IMessageSvc.h
CommonMessagingBase
Definition: CommonMessaging.h:58
CommonMessagingBase::msgStream
MsgStream & msgStream() const
Return an uninitialized MsgStream.
Definition: CommonMessaging.h:71
IService.h
LIKELY
#define LIKELY(x)
Definition: Kernel.h:95
implementation_detail
Definition: CommonMessaging.h:41
add_name
std::conditional_t< implementation_detail::has_name< Base >::value, Base, implementation_detail::add_name< Base > > add_name
Definition: CommonMessaging.h:50
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:113
implementation_detail::generate_
generate_(name, const std::string &,()) generate_(serviceLocator
CommonMessagingBase::~CommonMessagingBase
virtual ~CommonMessagingBase()=default
Virtual destructor.
CommonMessaging
Definition: CommonMessaging.h:56
MSG::ERROR
Definition: IMessageSvc.h:15
gaudirun.level
level
Definition: gaudirun.py:334
CommonMessagingBase::err
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:95
MSG::WARNING
Definition: IMessageSvc.h:15
MSG::INFO
Definition: IMessageSvc.h:15
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:71
CommonMessagingBase::msgStream
MsgStream & msgStream(const MSG::Level level) const
Predefined configurable message stream for the efficient printouts.
Definition: CommonMessaging.h:86
ISvcLocator.h
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:92
MSG::ALWAYS
Definition: IMessageSvc.h:15
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:192
IMessageSvc::setOutputLevel
virtual void setOutputLevel(int new_level)=0
Set new global output level threshold.