Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (e199b415)
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Modules Pages
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 #ifndef COMMONMESSAGING_H_
19 #define COMMONMESSAGING_H_
22 #include <GaudiKernel/IService.h>
24 #include <GaudiKernel/MsgStream.h>
25 #include <GaudiKernel/SmartIF.h>
26 #include <boost/thread/tss.hpp>
27 
28 #include <string>
29 #include <utility>
30 
36 
37  template <typename T>
38  concept has_name = requires( T const& t ) { t.name(); };
39 
40  template <typename T>
41  concept has_serviceLocator = requires( T const& t ) { t.serviceLocator(); };
42 
43  template <typename Base>
44  struct add_name : Base {
45  using Base::Base;
46  virtual ~add_name() = default;
47  virtual const std::string& name() const = 0;
48  };
49 
50  template <typename Base>
51  struct add_serviceLocator : Base {
52  using Base::Base;
53  virtual ~add_serviceLocator() = default;
54  virtual SmartIF<ISvcLocator>& serviceLocator() const = 0;
55  };
56 } // namespace implementation_detail
57 
58 template <typename Base>
59 using add_name = std::conditional_t<implementation_detail::has_name<Base>, Base, implementation_detail::add_name<Base>>;
60 
61 template <typename Base>
62 using add_serviceLocator = std::conditional_t<implementation_detail::has_serviceLocator<Base>, Base,
64 
65 template <typename Base>
67 
69 public:
71  virtual ~CommonMessagingBase() = default;
73  virtual void create_msgStream() const = 0;
74 
78  const SmartIF<IMessageSvc>& msgSvc() const { return m_msgsvc; }
79 
81  MsgStream& msgStream() const {
82  if ( !m_msgStream.get() ) create_msgStream();
83  return *m_msgStream;
84  }
85 
96  MsgStream& msgStream( const MSG::Level level ) const { return msgStream() << level; }
97 
99  MsgStream& always() const { return msgStream( MSG::ALWAYS ); }
100 
102  MsgStream& fatal() const { return msgStream( MSG::FATAL ); }
103 
105  MsgStream& err() const { return msgStream( MSG::ERROR ); }
106 
108  MsgStream& error() const { return msgStream( MSG::ERROR ); }
109 
111  MsgStream& warning() const { return msgStream( MSG::WARNING ); }
112 
114  MsgStream& info() const { return msgStream( MSG::INFO ); }
115 
117  MsgStream& debug() const { return msgStream( MSG::DEBUG ); }
118 
120  MsgStream& verbose() const { return msgStream( MSG::VERBOSE ); }
121 
123  MsgStream& msg() const { return msgStream( MSG::INFO ); }
124 
125 private:
126  template <typename Base>
127  friend class CommonMessaging;
128 
129  mutable bool m_commonMessagingReady = false;
131 
133  mutable boost::thread_specific_ptr<MsgStream> m_msgStream;
134 
137 };
138 
139 template <typename BASE>
140 class GAUDI_API CommonMessaging : public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase {
141 public:
143 
146 
149  if ( m_commonMessagingReady ) return m_level;
150  return setUpMessaging();
151  }
152 
154  bool msgLevel( MSG::Level lvl ) const { return msgLevel() <= lvl; }
155 
156 private:
157  // out-of-line 'cold' functions -- put here so as to not blow up the inline 'hot' functions
158  void create_msgStream() const override final { m_msgStream.reset( new MsgStream( msgSvc(), this->name() ) ); }
159 
161  void initMessaging() const {
162  if ( !m_msgsvc ) {
163  // Get default implementation of the message service.
164  m_msgsvc = this->serviceLocator();
165  }
167  m_level = MSG::Level( m_msgStream.get() ? m_msgStream->level() : MSG::NIL );
168  // if we could not get a MessageSvc, we should try again the initial set up
169  m_commonMessagingReady = m_msgsvc;
170  }
171 
172 protected:
175  if ( !m_commonMessagingReady ) { initMessaging(); }
176  return m_level;
177  }
180  m_commonMessagingReady = false;
181  return setUpMessaging();
182  }
186  setUpMessaging();
187  if ( level != MSG::NIL && level != m_level ) {
188  if ( msgSvc() ) msgSvc()->setOutputLevel( this->name(), level );
189  if ( m_msgStream.get() ) m_msgStream->setLevel( level );
190  if ( MSG::Level( level ) <= MSG::DEBUG )
191  debug() << "Property update for OutputLevel : new value = " << level << endmsg;
192  m_level = MSG::Level( level );
193  }
194  }
195 };
196 
197 #endif /* COMMONMESSAGING_H_ */
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:25
implementation_detail::add_serviceLocator::~add_serviceLocator
virtual ~add_serviceLocator()=default
CommonMessagingBase::msgStream
MsgStream & msgStream(const MSG::Level level) const
Predefined configurable message stream for the efficient printouts.
Definition: CommonMessaging.h:96
CommonMessagingBase::msgSvc
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: CommonMessaging.h:78
CommonMessagingBase::m_level
MSG::Level m_level
Definition: CommonMessaging.h:130
IService.h
std::string
STL class.
CommonMessagingBase::~CommonMessagingBase
virtual ~CommonMessagingBase()=default
Virtual destructor.
implementation_detail
Templated class to add the standard messaging functionalities.
Definition: CommonMessaging.h:35
CommonMessaging::resetMessaging
MSG::Level resetMessaging()
Reinitialize internal states.
Definition: CommonMessaging.h:179
MSG::INFO
@ INFO
Definition: IMessageSvc.h:25
CommonMessaging::create_msgStream
void create_msgStream() const override final
Definition: CommonMessaging.h:158
implementation_detail::add_name::~add_name
virtual ~add_name()=default
GaudiException.h
CommonMessagingBase::CommonMessaging
friend class CommonMessaging
Definition: CommonMessaging.h:127
CommonMessagingBase::m_msgsvc
SmartIF< IMessageSvc > m_msgsvc
Pointer to the message service;.
Definition: CommonMessaging.h:136
CommonMessagingBase
Definition: CommonMessaging.h:68
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
implementation_detail::has_name
concept has_name
Definition: CommonMessaging.h:38
implementation_detail::has_serviceLocator
concept has_serviceLocator
Definition: CommonMessaging.h:41
add_serviceLocator
std::conditional_t< implementation_detail::has_serviceLocator< Base >, Base, implementation_detail::add_serviceLocator< Base > > add_serviceLocator
Definition: CommonMessaging.h:63
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:108
CommonMessaging::initMessaging
void initMessaging() const
Initialise the messaging objects.
Definition: CommonMessaging.h:161
CommonMessagingBase::always
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
Definition: CommonMessaging.h:99
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:114
IMessageSvc.h
CommonMessaging::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:148
bug_34121.t
t
Definition: bug_34121.py:31
SmartIF.h
CommonMessaging::setUpMessaging
MSG::Level setUpMessaging() const
Set up local caches.
Definition: CommonMessaging.h:174
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:117
implementation_detail::add_name::name
virtual const std::string & name() const =0
CommonMessagingBase::err
MsgStream & err() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:105
CommonMessaging
Definition: CommonMessaging.h:66
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:111
CommonMessaging::updateMsgStreamOutputLevel
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
Definition: CommonMessaging.h:185
CommonMessagingBase::verbose
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
Definition: CommonMessaging.h:120
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:123
SmartIF< ISvcLocator >
CommonMessagingBase::create_msgStream
virtual void create_msgStream() const =0
cold functionality
endmsg
MsgStream & endmsg(MsgStream &s)
MsgStream Modifier: endmsg. Calls the output method of the MsgStream.
Definition: MsgStream.h:202
gaudirun.level
level
Definition: gaudirun.py:364
MsgStream
Definition: MsgStream.h:33
MSG::FATAL
@ FATAL
Definition: IMessageSvc.h:25
implementation_detail::add_name
Definition: CommonMessaging.h:44
CommonMessagingBase::msgStream
MsgStream & msgStream() const
Return an uninitialized MsgStream.
Definition: CommonMessaging.h:81
implementation_detail::add_serviceLocator
Definition: CommonMessaging.h:51
MSG::Level
Level
Definition: IMessageSvc.h:25
CommonMessagingBase::m_msgStream
boost::thread_specific_ptr< MsgStream > m_msgStream
The predefined message stream.
Definition: CommonMessaging.h:133
CommonMessaging::msgLevel
bool msgLevel(MSG::Level lvl) const
get the output level from the embedded MsgStream
Definition: CommonMessaging.h:154
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:25
ConditionsStallTest.name
name
Definition: ConditionsStallTest.py:77
add_name
std::conditional_t< implementation_detail::has_name< Base >, Base, implementation_detail::add_name< Base > > add_name
Definition: CommonMessaging.h:59
implementation_detail::add_serviceLocator::serviceLocator
virtual SmartIF< ISvcLocator > & serviceLocator() const =0
MSG::ALWAYS
@ ALWAYS
Definition: IMessageSvc.h:25
MSG::NIL
@ NIL
Definition: IMessageSvc.h:25
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
Gaudi::Functional::details::requires
requires(is_optional_v< OptOut >) void put(const OutHandle &out_handle
ISvcLocator.h
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:102
CommonMessagingBase::m_commonMessagingReady
bool m_commonMessagingReady
Definition: CommonMessaging.h:129
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
MsgStream.h