The Gaudi Framework  master (ff829712)
Loading...
Searching...
No Matches
CommonMessaging.h
Go to the documentation of this file.
1/***********************************************************************************\
2* (c) Copyright 1998-2025 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
77 const SmartIF<IMessageSvc>& msgSvc() const { return m_msgsvc; }
78
81 if ( !m_msgStream.get() ) create_msgStream();
82 return *m_msgStream;
83 }
84
95 MsgStream& msgStream( const MSG::Level level ) const { return msgStream() << level; }
96
98 MsgStream& always() const { return msgStream( MSG::ALWAYS ); }
99
101 MsgStream& fatal() const { return msgStream( MSG::FATAL ); }
102
104 MsgStream& err() const { return msgStream( MSG::ERROR ); }
105
107 MsgStream& error() const { return msgStream( MSG::ERROR ); }
108
110 MsgStream& warning() const { return msgStream( MSG::WARNING ); }
111
113 MsgStream& info() const { return msgStream( MSG::INFO ); }
114
116 MsgStream& debug() const { return msgStream( MSG::DEBUG ); }
117
119 MsgStream& verbose() const { return msgStream( MSG::VERBOSE ); }
120
122 MsgStream& msg() const { return msgStream( MSG::INFO ); }
123
124private:
125 template <typename Base>
126 friend class CommonMessaging;
127
128 mutable bool m_commonMessagingReady = false;
130
132 mutable boost::thread_specific_ptr<MsgStream> m_msgStream;
133
136};
137
138template <typename BASE>
139class GAUDI_API CommonMessaging : public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase {
140public:
142
145
148 if ( m_commonMessagingReady ) return m_level;
149 return setUpMessaging();
150 }
151
153 bool msgLevel( MSG::Level lvl ) const { return msgLevel() <= lvl; }
154
155private:
156 // out-of-line 'cold' functions -- put here so as to not blow up the inline 'hot' functions
157 void create_msgStream() const override final { m_msgStream.reset( new MsgStream( msgSvc(), this->name() ) ); }
158
160 void initMessaging() const {
161 if ( !m_msgsvc ) {
162 // Get default implementation of the message service.
163 m_msgsvc = this->serviceLocator();
164 }
166 m_level = MSG::Level( m_msgStream.get() ? m_msgStream->level() : MSG::NIL );
167 // if we could not get a MessageSvc, we should try again the initial set up
169 }
170
171protected:
175 return m_level;
176 }
177
182
184 void updateMsgStreamOutputLevel( int level ) {
186 if ( level != MSG::NIL && level != m_level ) {
187 if ( msgSvc() ) msgSvc()->setOutputLevel( this->name(), level );
188 if ( m_msgStream.get() ) m_msgStream->setLevel( level );
189 if ( MSG::Level( level ) <= MSG::DEBUG )
190 debug() << "Property update for OutputLevel : new value = " << level << endmsg;
191 m_level = MSG::Level( level );
192 }
193 }
194};
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)
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)
bool msgLevel(MSG::Level lvl) const
get the output level from the embedded MsgStream
void initMessaging() const
Initialise the messaging objects.
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
MSG::Level setUpMessaging() const
Set up local caches.
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