The Gaudi Framework  master (48d4dc99)
Loading...
Searching...
No Matches
CommonMessaging.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2026 CERN for the benefit of the LHCb and ATLAS collaborations *
3* *
4* This software is distributed under the terms of the Apache version 2 licence, *
5* copied verbatim in the file "LICENSE". *
6* *
7* In applying this licence, CERN does not waive the privileges and immunities *
8* granted to it by virtue of its status as an Intergovernmental Organization *
9* or submit itself to any jurisdiction. *
10\***********************************************************************************/
11/*
12 * CommonMessaging.h
13 *
14 * Created on: Feb 20, 2009
15 * Author: Marco Clemencic
16 */
17
18#pragma once
24#include <GaudiKernel/SmartIF.h>
25#include <boost/thread/tss.hpp>
26
27#include <string>
28#include <utility>
29
33
35
36 template <typename T>
37 concept has_name = requires( T const& t ) { t.name(); };
38
39 template <typename T>
40 concept has_serviceLocator = requires( T const& t ) { t.serviceLocator(); };
41
42 template <typename Base>
43 struct add_name : Base {
44 using Base::Base;
45 virtual ~add_name() = default;
46 virtual const std::string& name() const = 0;
47 };
48
49 template <typename Base>
50 struct add_serviceLocator : Base {
51 using Base::Base;
52 virtual ~add_serviceLocator() = default;
54 };
55} // namespace implementation_detail
56
57template <typename Base>
58using add_name = std::conditional_t<implementation_detail::has_name<Base>, Base, implementation_detail::add_name<Base>>;
59
60template <typename Base>
61using add_serviceLocator = std::conditional_t<implementation_detail::has_serviceLocator<Base>, Base,
63
64template <typename Base>
65class CommonMessaging;
66
68public:
70 virtual ~CommonMessagingBase() = default;
72 virtual void create_msgStream() const = 0;
73
74 virtual MSG::Level setUpMessaging() const = 0;
75
79 const SmartIF<IMessageSvc>& msgSvc() const { return m_msgsvc; }
80
83 if ( !m_msgStream.get() ) create_msgStream();
84 return *m_msgStream;
85 }
86
97 MsgStream& msgStream( const MSG::Level level ) const { return msgStream() << level; }
98
100 MsgStream& always() const { return msgStream( MSG::ALWAYS ); }
101
103 MsgStream& fatal() const { return msgStream( MSG::FATAL ); }
104
106 MsgStream& err() const { return msgStream( MSG::ERROR ); }
107
109 MsgStream& error() const { return msgStream( MSG::ERROR ); }
110
112 MsgStream& warning() const { return msgStream( MSG::WARNING ); }
113
115 MsgStream& info() const { return msgStream( MSG::INFO ); }
116
118 MsgStream& debug() const { return msgStream( MSG::DEBUG ); }
119
121 MsgStream& verbose() const { return msgStream( MSG::VERBOSE ); }
122
124 MsgStream& msg() const { return msgStream( MSG::INFO ); }
125
128 if ( m_commonMessagingReady ) return m_level;
129 return setUpMessaging();
130 };
131
133 bool msgLevel( MSG::Level lvl ) const { return msgLevel() <= lvl; }
134
135private:
136 template <typename Base>
137 friend class CommonMessaging;
138
139 mutable bool m_commonMessagingReady = false;
141
143 mutable boost::thread_specific_ptr<MsgStream> m_msgStream;
144
147};
148
149template <typename BASE>
150class GAUDI_API CommonMessaging : public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase {
151public:
153
156
157private:
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 if ( !m_msgsvc ) {
164 // Get default implementation of the message service.
165 m_msgsvc = this->serviceLocator();
166 }
168 m_level = MSG::Level( m_msgStream.get() ? m_msgStream->level() : MSG::NIL );
169 // if we could not get a MessageSvc, we should try again the initial set up
171 }
172
173protected:
175 MSG::Level setUpMessaging() const override final {
177 return m_level;
178 }
179
185
187 void updateMsgStreamOutputLevel( int level ) {
189 if ( level != MSG::NIL && level != m_level ) {
190 if ( msgSvc() ) msgSvc()->setOutputLevel( this->name(), level );
191 if ( m_msgStream.get() ) m_msgStream->setLevel( level );
192 if ( MSG::Level( level ) <= MSG::DEBUG )
193 debug() << "Property update for OutputLevel : new value = " << level << endmsg;
194 m_level = MSG::Level( level );
195 }
196 }
197};
std::conditional_t< implementation_detail::has_name< Base >, Base, implementation_detail::add_name< Base > > add_name
std::conditional_t< implementation_detail::has_serviceLocator< Base >, Base, implementation_detail::add_serviceLocator< Base > > add_serviceLocator
#define GAUDI_API
Definition Kernel.h:49
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition MsgStream.h:198
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & msgStream(const MSG::Level level) const
Predefined configurable message stream for the efficient printouts.
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
friend class CommonMessaging
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
MsgStream & msgStream() const
Return an uninitialized MsgStream.
boost::thread_specific_ptr< MsgStream > m_msgStream
The predefined message stream.
virtual ~CommonMessagingBase()=default
Virtual destructor.
virtual void create_msgStream() const =0
cold functionality
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
bool msgLevel(MSG::Level lvl) const
get the output level from the embedded MsgStream
virtual MSG::Level setUpMessaging() const =0
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
SmartIF< IMessageSvc > m_msgsvc
Pointer to the message service;.
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
MSG::Level setUpMessaging() const override final
Set up local caches.
void initMessaging() const
Initialise the messaging objects.
MSG::Level resetMessaging()
Reinitialize internal states.
void create_msgStream() const override final
cold functionality
CommonMessaging base_class
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
virtual void setOutputLevel(int new_level)=0
Set new global output level threshold.
Definition of the MsgStream class used to transmit messages.
Definition MsgStream.h:29
Small smart pointer class with automatic reference counting for IInterface.
Definition SmartIF.h:28
@ NIL
Definition IMessageSvc.h:22
@ WARNING
Definition IMessageSvc.h:22
@ FATAL
Definition IMessageSvc.h:22
@ DEBUG
Definition IMessageSvc.h:22
@ ERROR
Definition IMessageSvc.h:22
@ ALWAYS
Definition IMessageSvc.h:22
@ INFO
Definition IMessageSvc.h:22
@ VERBOSE
Definition IMessageSvc.h:22
Templated class to add the standard messaging functionalities.
virtual const std::string & name() const =0
virtual SmartIF< ISvcLocator > & serviceLocator() const =0