Loading [MathJax]/extensions/tex2jax.js
The Gaudi Framework  master (d98a2936)
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 #pragma once
21 #include <GaudiKernel/IService.h>
23 #include <GaudiKernel/MsgStream.h>
24 #include <GaudiKernel/SmartIF.h>
25 #include <boost/thread/tss.hpp>
26 
27 #include <string>
28 #include <utility>
29 
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;
53  virtual SmartIF<ISvcLocator>& serviceLocator() const = 0;
54  };
55 } // namespace implementation_detail
56 
57 template <typename Base>
58 using add_name = std::conditional_t<implementation_detail::has_name<Base>, Base, implementation_detail::add_name<Base>>;
59 
60 template <typename Base>
61 using add_serviceLocator = std::conditional_t<implementation_detail::has_serviceLocator<Base>, Base,
63 
64 template <typename Base>
66 
68 public:
70  virtual ~CommonMessagingBase() = default;
72  virtual void create_msgStream() const = 0;
73 
77  const SmartIF<IMessageSvc>& msgSvc() const { return m_msgsvc; }
78 
80  MsgStream& msgStream() const {
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 
124 private:
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 
138 template <typename BASE>
139 class GAUDI_API CommonMessaging : public add_serviceLocator<add_name<BASE>>, public CommonMessagingBase {
140 public:
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 
155 private:
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
168  m_commonMessagingReady = m_msgsvc;
169  }
170 
171 protected:
174  if ( !m_commonMessagingReady ) { initMessaging(); }
175  return m_level;
176  }
179  m_commonMessagingReady = false;
180  return setUpMessaging();
181  }
185  setUpMessaging();
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 };
MSG::DEBUG
@ DEBUG
Definition: IMessageSvc.h:22
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:95
CommonMessagingBase::msgSvc
const SmartIF< IMessageSvc > & msgSvc() const
The standard message service.
Definition: CommonMessaging.h:77
CommonMessagingBase::m_level
MSG::Level m_level
Definition: CommonMessaging.h:129
IService.h
CommonMessagingBase::~CommonMessagingBase
virtual ~CommonMessagingBase()=default
Virtual destructor.
implementation_detail
Templated class to add the standard messaging functionalities.
Definition: CommonMessaging.h:34
CommonMessaging::resetMessaging
MSG::Level resetMessaging()
Reinitialize internal states.
Definition: CommonMessaging.h:178
MSG::INFO
@ INFO
Definition: IMessageSvc.h:22
CommonMessaging::create_msgStream
void create_msgStream() const override final
Definition: CommonMessaging.h:157
implementation_detail::add_name::~add_name
virtual ~add_name()=default
GaudiException.h
CommonMessagingBase::CommonMessaging
friend class CommonMessaging
Definition: CommonMessaging.h:126
CommonMessagingBase::m_msgsvc
SmartIF< IMessageSvc > m_msgsvc
Pointer to the message service;.
Definition: CommonMessaging.h:135
CommonMessagingBase
Definition: CommonMessaging.h:67
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:22
implementation_detail::has_name
concept has_name
Definition: CommonMessaging.h:37
implementation_detail::has_serviceLocator
concept has_serviceLocator
Definition: CommonMessaging.h:40
add_serviceLocator
std::conditional_t< implementation_detail::has_serviceLocator< Base >, Base, implementation_detail::add_serviceLocator< Base > > add_serviceLocator
Definition: CommonMessaging.h:62
Gaudi::Functional::details::detail2::requires
requires requires
Definition: details.h:419
CommonMessagingBase::error
MsgStream & error() const
shortcut for the method msgStream(MSG::ERROR)
Definition: CommonMessaging.h:107
CommonMessaging::initMessaging
void initMessaging() const
Initialise the messaging objects.
Definition: CommonMessaging.h:160
CommonMessagingBase::always
MsgStream & always() const
shortcut for the method msgStream(MSG::ALWAYS)
Definition: CommonMessaging.h:98
CommonMessagingBase::info
MsgStream & info() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:113
IMessageSvc.h
CommonMessaging::msgLevel
MSG::Level msgLevel() const
get the cached level (originally extracted from the embedded MsgStream)
Definition: CommonMessaging.h:147
bug_34121.t
t
Definition: bug_34121.py:31
SmartIF.h
CommonMessaging::setUpMessaging
MSG::Level setUpMessaging() const
Set up local caches.
Definition: CommonMessaging.h:173
CommonMessagingBase::debug
MsgStream & debug() const
shortcut for the method msgStream(MSG::DEBUG)
Definition: CommonMessaging.h:116
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:104
CommonMessaging
Definition: CommonMessaging.h:65
CommonMessagingBase::warning
MsgStream & warning() const
shortcut for the method msgStream(MSG::WARNING)
Definition: CommonMessaging.h:110
CommonMessaging::updateMsgStreamOutputLevel
void updateMsgStreamOutputLevel(int level)
Update the output level of the cached MsgStream.
Definition: CommonMessaging.h:184
CommonMessagingBase::verbose
MsgStream & verbose() const
shortcut for the method msgStream(MSG::VERBOSE)
Definition: CommonMessaging.h:119
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:122
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:198
gaudirun.level
level
Definition: gaudirun.py:364
MsgStream
Definition: MsgStream.h:29
MSG::FATAL
@ FATAL
Definition: IMessageSvc.h:22
implementation_detail::add_name
Definition: CommonMessaging.h:43
CommonMessagingBase::msgStream
MsgStream & msgStream() const
Return an uninitialized MsgStream.
Definition: CommonMessaging.h:80
implementation_detail::add_serviceLocator
Definition: CommonMessaging.h:50
MSG::Level
Level
Definition: IMessageSvc.h:22
CommonMessagingBase::m_msgStream
boost::thread_specific_ptr< MsgStream > m_msgStream
The predefined message stream.
Definition: CommonMessaging.h:132
CommonMessaging::msgLevel
bool msgLevel(MSG::Level lvl) const
get the output level from the embedded MsgStream
Definition: CommonMessaging.h:153
MSG::VERBOSE
@ VERBOSE
Definition: IMessageSvc.h:22
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:58
implementation_detail::add_serviceLocator::serviceLocator
virtual SmartIF< ISvcLocator > & serviceLocator() const =0
MSG::ALWAYS
@ ALWAYS
Definition: IMessageSvc.h:22
MSG::NIL
@ NIL
Definition: IMessageSvc.h:22
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:22
ISvcLocator.h
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:101
CommonMessagingBase::m_commonMessagingReady
bool m_commonMessagingReady
Definition: CommonMessaging.h:128
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:49
MsgStream.h