| The Gaudi Framework v30r0 (c919700c)

- Main Page
- Related Pages
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

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 <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
41 namespace implementation_detail
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>
55 using add_name = typename std::conditional<implementation_detail::has_name<Base>::value, Base,
56 implementation_detail::add_name<Base>>::type;
57 template <typename Base>
58 using add_serviceLocator = typename std::conditional<implementation_detail::has_serviceLocator<Base>::value, Base,
59 implementation_detail::add_serviceLocator<Base>>::type;
60
61 template <typename Base>
62 class CommonMessaging;
63
64 class CommonMessagingBase
65 {
66 public:
68 virtual ~CommonMessagingBase() = default;
70 virtual void create_msgStream() const = 0;
71
75 inline const SmartIF<IMessageSvc>& msgSvc() const { return m_msgsvc; }
76
78 inline MsgStream& msgStream() const
79 {
80 if ( UNLIKELY( ( !m_msgStream.get() ) ) ) create_msgStream();
81 return *m_msgStream;
82 }
83
94 inline MsgStream& msgStream( const MSG::Level level ) const { return msgStream() << level; }
95
97 inline MsgStream& always() const { return msgStream( MSG::ALWAYS ); }
98
100 inline MsgStream& fatal() const { return msgStream( MSG::FATAL ); }
101
103 inline MsgStream& err() const { return msgStream( MSG::ERROR ); }
104
106 inline MsgStream& error() const { return msgStream( MSG::ERROR ); }
107
109 inline MsgStream& warning() const { return msgStream( MSG::WARNING ); }
110
112 inline MsgStream& info() const { return msgStream( MSG::INFO ); }
113
115 inline MsgStream& debug() const { return msgStream( MSG::DEBUG ); }
116
118 inline MsgStream& verbose() const { return msgStream( MSG::VERBOSE ); }
119
121 inline MsgStream& msg() const { return msgStream( MSG::INFO ); }
122
124 inline MSG::Level msgLevel() const
125 {
126 assert( m_commonMessagingReady );
127 return m_level;
128 }
129
131 inline MSG::Level outputLevel() const __attribute__( ( deprecated ) ) { return msgLevel(); }
132
134 inline bool msgLevel( MSG::Level lvl ) const { return UNLIKELY( msgLevel() <= lvl ); }
135
136 private:
137 template <typename Base>
138 friend class CommonMessaging;
139
140 bool m_commonMessagingReady = false;
141
143 mutable boost::thread_specific_ptr<MsgStream> m_msgStream;
144
145 MSG::Level m_level = MSG::NIL;
146
148 SmartIF<IMessageSvc> m_msgsvc;
149 };
150
151 template <typename BASE>
152 class GAUDI_API CommonMessaging : public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase
153 {
154 public:
155 using base_class = CommonMessaging;
156
158 using add_serviceLocator<add_name<BASE>>::add_serviceLocator;
159
160 private:
161 // out-of-line 'cold' functions -- put here so as to not blow up the inline 'hot' functions
162 void create_msgStream() const override final { m_msgStream.reset( new MsgStream( msgSvc(), this->name() ) ); }
163
164 protected:
166 MSG::Level setUpMessaging()
167 {
168 if ( !m_commonMessagingReady ) {
169 if ( !m_msgsvc ) {
170 // Get default implementation of the message service.
171 m_msgsvc = this->serviceLocator();
172 }
173 create_msgStream();
174 m_level = MSG::Level( m_msgStream.get() ? m_msgStream->level() : MSG::NIL );
175 // if we could not get a MessageSvc, we should try again the initial set up
176 m_commonMessagingReady = m_msgsvc;
177 }
178 return m_level;
179 }
181 MSG::Level resetMessaging()
182 {
183 m_commonMessagingReady = false;
184 return setUpMessaging();
185 }
188 void updateMsgStreamOutputLevel( int level )
189 {
190 setUpMessaging();
191 if ( level != MSG::NIL && level != m_level ) {
192 if ( msgSvc() ) {
193 msgSvc()->setOutputLevel( this->name(), level );
194 }
195 if ( m_msgStream.get() ) m_msgStream->setLevel( level );
196 if ( UNLIKELY( MSG::Level( level ) <= MSG::DEBUG ) )
197 debug() << "Property update for OutputLevel : new value = " << level << endmsg;
198 m_level = MSG::Level( level );
199 }
200 }
201 };
202
203 #endif /* COMMONMESSAGING_H_ */
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:128
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
CommonMessagingBase::m_commonMessagingReady
bool m_commonMessagingReady
Definition: CommonMessaging.h:140
SmartIF
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:15
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:121
__attribute__
#define __attribute__(x)
Definition: System.cpp:89
ISvcLocator
The ISvcLocator is the interface implemented by the Service Factory in the Application Manager to loc...
Definition: ISvcLocator.h:25
gaudiComponentHelp.name
name
Definition: gaudiComponentHelp.py:32
MsgStream.h
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:112
CommonMessaging::resetMessaging
MSG::Level resetMessaging()
Reinitialize internal states.
Definition: CommonMessaging.h:181
CommonMessagingBase::verbose
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
Definition: CommonMessaging.h:118
MSG::DEBUG
Definition: IMessageSvc.h:16
CommonMessagingBase::m_msgsvc
SmartIF< IMessageSvc > m_msgsvc
Pointer to the message service;.
Definition: CommonMessaging.h:148
Gaudi::Functional::void
class MergingTransformer< Out(const vector_of_const_< In > void
Definition: MergingTransformer.h:23
add_name
typename std::conditional< implementation_detail::has_name< Base >::value, Base, implementation_detail::add_name< Base >>::type add_name
Definition: CommonMessaging.h:56
MSG::VERBOSE
Definition: IMessageSvc.h:16
CommonMessagingBase::err
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:103
std::string
STL class.
MSG::FATAL
Definition: IMessageSvc.h:16
MSG::Level
Level
Definition: IMessageSvc.h:16
CommonMessagingBase::m_level
MSG::Level m_level
Definition: CommonMessaging.h:145
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:106
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:109
CommonMessaging::updateMsgStreamOutputLevel
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
Definition: CommonMessaging.h:188
GaudiException.h
SmartIF.h
MSG::NIL
Definition: IMessageSvc.h:16
implementation_detail::void_t
Definition: CommonMessaging.h:44
CommonMessagingBase::m_msgStream
boost::thread_specific_ptr< MsgStream > m_msgStream
The predefined message stream.
Definition: CommonMessaging.h:143
IMessageSvc.h
CommonMessagingBase::msgSvc
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: CommonMessaging.h:75
CommonMessagingBase
Definition: CommonMessaging.h:64
CommonMessaging::create_msgStream
void create_msgStream() const override final
cold functionality
Definition: CommonMessaging.h:162
IService.h
implementation_detail
Definition: CommonMessaging.h:41
CommonMessagingBase::outputLevel
MSG::Level outputLevel() const __attribute__((deprecated))
Backward compatibility function for getting the output level.
Definition: CommonMessaging.h:131
std::conditional
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:115
implementation_detail::generate_
generate_(name, const std::string &,()) generate_(serviceLocator
CommonMessagingBase::msgStream
MsgStream & msgStream() const
Return an uninitialized MsgStream.
Definition: CommonMessaging.h:78
implementation_detail::void_t::type
void type
Definition: CommonMessaging.h:45
CommonMessaging
Definition: CommonMessaging.h:62
MSG::ERROR
Definition: IMessageSvc.h:16
gaudirun.level
level
Definition: gaudirun.py:271
implementation_detail::is_valid_t
typename void_t< T >::type is_valid_t
Definition: CommonMessaging.h:48
MSG::WARNING
Definition: IMessageSvc.h:16
CommonMessagingBase::msgStream
MsgStream & msgStream(const MSG::Level level) const
Predefined configurable message stream for the efficient printouts.
Definition: CommonMessaging.h:94
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:100
MSG::INFO
Definition: IMessageSvc.h:16
CommonMessagingBase::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:124
CommonMessagingBase::always
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
Definition: CommonMessaging.h:97
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:110
add_serviceLocator
typename std::conditional< implementation_detail::has_serviceLocator< Base >::value, Base, implementation_detail::add_serviceLocator< Base >>::type add_serviceLocator
Definition: CommonMessaging.h:59
CommonMessaging::setUpMessaging
MSG::Level setUpMessaging()
Set up local caches.
Definition: CommonMessaging.h:166
ISvcLocator.h
CommonMessagingBase::msgLevel
bool msgLevel(MSG::Level lvl) const
get the output level from the embedded MsgStream
Definition: CommonMessaging.h:134
MSG::ALWAYS
Definition: IMessageSvc.h:16
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:209