| The Gaudi Framework v27r1

- 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/IService.h"
11 #include "GaudiKernel/ISvcLocator.h"
12 #include "GaudiKernel/IMessageSvc.h"
13 #include "GaudiKernel/MsgStream.h"
14 #include "GaudiKernel/SmartIF.h"
15 #include "GaudiKernel/GaudiException.h"
16
17 #include <memory>
18 #include <utility>
19
24 #define generate_has_( method, args ) \
25 template <typename T, typename SFINAE = void> \
26 struct has_ ## method : std::false_type {}; \
27 template <typename T> \
28 struct has_ ## method<T, is_valid_t<decltype(std::declval<const T&>().method args)>> : std::true_type {};
29
30 #define generate_add_(method, ret, args ) \
31 template <typename Base, bool> struct add_ ## method; \
32 template <typename Base> \
33 struct add_ ## method <Base,false> : public Base { \
34 using Base::Base; \
35 }; \
36 template <typename Base> \
37 struct add_ ## method <Base, true> : public Base { \
38 using Base::Base; \
39 virtual ~add_ ## method () = default; \
40 virtual ret method args const = 0; \
41 };
42
43 namespace implementation_detail {
44 template <typename> struct void_t { typedef void type; };
45 template <typename T> using is_valid_t = typename void_t<T>::type;
46
47 generate_has_( name, () )
48 generate_add_( name, const std::string&, () )
49 generate_has_( serviceLocator, () )
50 generate_add_( serviceLocator, SmartIF<ISvcLocator>&, () )
51 }
52 #undef generate_has_
53 #undef generate_add_
54
55 template <typename Base> using add_name = implementation_detail::add_name< Base, ! implementation_detail::has_name<Base>::value >;
56 template <typename Base> using add_serviceLocator = implementation_detail::add_serviceLocator< Base, ! implementation_detail::has_serviceLocator<Base>::value >;
57
58 template <typename Base> class CommonMessaging;
59
60 class CommonMessagingBase {
61 public:
63 virtual ~CommonMessagingBase() = default;
65 virtual void create_msgSvc() const = 0;
66 virtual void create_msgStream() const = 0;
67
68
69
73 inline SmartIF<IMessageSvc>& msgSvc() const {
74 if (UNLIKELY(!m_msgsvc)) create_msgSvc();
75 return m_msgsvc;
76 }
77
78 #if defined(GAUDI_V20_COMPAT) && !defined(G21_NO_DEPRECATED)
79
83 inline SmartIF<IMessageSvc>& messageService() const {
84 return msgSvc();
85 }
86 #endif
87
89 inline MsgStream& msgStream() const {
90 if (UNLIKELY((m_createMsgStream))) create_msgStream();
91 return *m_msgStream;
92 }
93
104 inline MsgStream& msgStream(const MSG::Level level) const {
105 return msgStream() << level;
106 }
107
109 inline MsgStream& always() const { return msgStream(MSG::ALWAYS); }
110
112 inline MsgStream& fatal() const { return msgStream(MSG::FATAL); }
113
115 inline MsgStream& err() const { return msgStream(MSG::ERROR); }
116
118 inline MsgStream& error() const { return msgStream(MSG::ERROR); }
119
121 inline MsgStream& warning() const { return msgStream(MSG::WARNING); }
122
124 inline MsgStream& info() const { return msgStream(MSG::INFO); }
125
127 inline MsgStream& debug() const { return msgStream(MSG::DEBUG); }
128
130 inline MsgStream& verbose() const { return msgStream(MSG::VERBOSE); }
131
133 inline MsgStream& msg() const { return msgStream(MSG::INFO); }
134
136 inline MSG::Level msgLevel() const {
137 if (UNLIKELY(!m_msgStream)) create_msgStream();
138 return m_level;
139 }
140
142 inline MSG::Level outputLevel() const __attribute__ ((deprecated)) { return m_level; }
143
145 inline bool msgLevel(MSG::Level lvl) const { return UNLIKELY(msgLevel() <= lvl); }
146
147 private:
148 template <typename Base> friend class CommonMessaging;
149
151 mutable std::unique_ptr<MsgStream> m_msgStream;
152
153 mutable MSG::Level m_level = MSG::NIL;
155 mutable bool m_createMsgStream = true;
156
158 mutable SmartIF<IMessageSvc> m_msgsvc;
159
160
161 };
162
163 template <typename BASE>
164 class GAUDI_API CommonMessaging: public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase {
165 public:
166 using base_class = CommonMessaging;
167
169 using add_serviceLocator<add_name<BASE>>::add_serviceLocator;
170
171 private:
172 // out-of-line 'cold' functions -- put here so as to not blow up the inline 'hot' functions
173 void create_msgSvc() const override final {
174 // Get default implementation of the message service.
175 m_msgsvc = this->serviceLocator();
176 }
177 void create_msgStream() const override final {
178 auto& ms = msgSvc();
179 m_msgStream.reset(new MsgStream(ms, this->name()));
180 m_createMsgStream = (!ms.isValid() || !m_msgStream);
181 m_level = m_msgStream ? m_msgStream->level() : MSG::NIL;
182 }
183
184 protected:
187 void updateMsgStreamOutputLevel(int level) {
188 if (level != MSG::NIL && level != m_level) {
189 msgSvc()->setOutputLevel(this->name(), level);
190 if (m_msgStream) m_msgStream->setLevel(level);
191 if (UNLIKELY(MSG::Level(level) <= MSG::DEBUG))
192 debug() << "Property update for OutputLevel : new value = " << level << endmsg;
193 m_level = MSG::Level(level);
194 }
195 }
196
197 };
198
199
200 #endif /* COMMONMESSAGING_H_ */
UNLIKELY
#define UNLIKELY(x)
Definition: Kernel.h:126
MsgStream
Definition of the MsgStream class used to transmit messages.
Definition: MsgStream.h:24
SmartIF
Small smart pointer class with automatic reference counting for IInterface.
Definition: IConverter.h:14
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:133
__attribute__
#define __attribute__(x)
Definition: System.cpp:70
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:28
MsgStream.h
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:124
implementation_detail::void_t::type
void type
Definition: CommonMessaging.h:44
CommonMessagingBase::verbose
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
Definition: CommonMessaging.h:130
MSG::DEBUG
Definition: IMessageSvc.h:18
CommonMessagingBase::m_msgsvc
SmartIF< IMessageSvc > m_msgsvc
Pointer to the message service;.
Definition: CommonMessaging.h:158
std
STL namespace.
CommonMessaging::create_msgSvc
void create_msgSvc() const overridefinal
cold functionality
Definition: CommonMessaging.h:173
CommonMessagingBase::m_msgStream
std::unique_ptr< MsgStream > m_msgStream
The predefined message stream.
Definition: CommonMessaging.h:151
MSG::VERBOSE
Definition: IMessageSvc.h:17
CommonMessagingBase::err
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:115
implementation_detail::generate_has_
generate_has_(name,()) generate_add_(name
MSG::FATAL
Definition: IMessageSvc.h:22
MSG::Level
Level
Definition: IMessageSvc.h:15
CommonMessagingBase::m_level
MSG::Level m_level
Definition: CommonMessaging.h:153
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:118
generate_add_
#define generate_add_(method, ret, args)
Definition: CommonMessaging.h:30
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:121
CommonMessaging::updateMsgStreamOutputLevel
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
Definition: CommonMessaging.h:187
GaudiException.h
SmartIF.h
MSG::NIL
Definition: IMessageSvc.h:16
implementation_detail::void_t
Definition: CommonMessaging.h:44
dump_component_properties.value
list value
Definition: dump_component_properties.py:12
IMessageSvc.h
CommonMessagingBase
Definition: CommonMessaging.h:60
IService.h
add_serviceLocator
implementation_detail::add_serviceLocator< Base,!implementation_detail::has_serviceLocator< Base >::value > add_serviceLocator
Definition: CommonMessaging.h:56
implementation_detail
Definition: CommonMessaging.h:43
CommonMessaging::create_msgStream
void create_msgStream() const overridefinal
Definition: CommonMessaging.h:177
CommonMessagingBase::outputLevel
MSG::Level outputLevel() const __attribute__((deprecated))
Backward compatibility function for getting the output level.
Definition: CommonMessaging.h:142
std::unique_ptr< MsgStream >
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:127
add_name
implementation_detail::add_name< Base,!implementation_detail::has_name< Base >::value > add_name
Definition: CommonMessaging.h:55
CommonMessagingBase::msgStream
MsgStream & msgStream() const
Return an uninitialized MsgStream.
Definition: CommonMessaging.h:89
CommonMessaging
Definition: CommonMessaging.h:58
CommonMessagingBase::msgSvc
SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: CommonMessaging.h:73
MSG::ERROR
Definition: IMessageSvc.h:21
gaudirun.level
level
Definition: gaudirun.py:261
implementation_detail::is_valid_t
typename void_t< T >::type is_valid_t
Definition: CommonMessaging.h:45
MSG::WARNING
Definition: IMessageSvc.h:20
CommonMessagingBase::msgStream
MsgStream & msgStream(const MSG::Level level) const
Predefined configurable message stream for the efficient printouts.
Definition: CommonMessaging.h:104
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:112
MSG::INFO
Definition: IMessageSvc.h:19
CommonMessagingBase::msgLevel
MSG::Level msgLevel() const
get the output level from the embedded MsgStream
Definition: CommonMessaging.h:136
CommonMessagingBase::always
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
Definition: CommonMessaging.h:109
AthenaScenario.ms
tuple ms
Definition: AthenaScenario.py:72
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:107
CommonMessagingBase::m_createMsgStream
bool m_createMsgStream
Flag to trigger a new MsgStream.
Definition: CommonMessaging.h:155
ISvcLocator.h
CommonMessagingBase::msgLevel
bool msgLevel(MSG::Level lvl) const
get the output level from the embedded MsgStream
Definition: CommonMessaging.h:145
MSG::ALWAYS
Definition: IMessageSvc.h:23
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:244
ForwardSchedulerSimpleTest.msgSvc
tuple msgSvc
Definition: ForwardSchedulerSimpleTest.py:112