The Gaudi Framework  master (37c0b60a)
CommonMessaging.h
Go to the documentation of this file.
1 /***********************************************************************************\
2 * (c) Copyright 1998-2024 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 <GaudiKernel/detected.h>
27 #include <boost/thread/tss.hpp>
28 
29 #include <string>
30 #include <utility>
31 
36 #define generate_( method, ret, args ) \
37  \
38  template <typename T> \
39  using _has_##method = decltype( std::declval<const T&>().method args ); \
40  \
41  template <typename T> \
42  using has_##method = Gaudi::cpp17::is_detected<_has_##method, T>; \
43  \
44  template <typename Base> \
45  struct add_##method : public Base { \
46  using Base::Base; \
47  virtual ~add_##method() = default; \
48  virtual ret method args const = 0; \
49  };
50 
52  generate_( name, const std::string&, () )
53 
54  generate_( serviceLocator, SmartIF<ISvcLocator>&, () )
55 }
56 #undef generate_
57 
58 template <typename Base>
59 using add_name =
60  std::conditional_t<implementation_detail::has_name<Base>::value, Base, implementation_detail::add_name<Base>>;
61 template <typename Base>
62 using add_serviceLocator = std::conditional_t<implementation_detail::has_serviceLocator<Base>::value, Base,
63  implementation_detail::add_serviceLocator<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;
130  mutable MSG::Level m_level = MSG::NIL;
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  }
166  create_msgStream();
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
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
IService.h
std::string
STL class.
CommonMessagingBase::~CommonMessagingBase
virtual ~CommonMessagingBase()=default
Virtual destructor.
implementation_detail
Definition: CommonMessaging.h:51
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
GaudiException.h
CommonMessagingBase::m_msgsvc
SmartIF< IMessageSvc > m_msgsvc
Pointer to the message service;.
Definition: CommonMessaging.h:136
ISvcLocator
Definition: ISvcLocator.h:46
CommonMessagingBase
Definition: CommonMessaging.h:68
MSG::WARNING
@ WARNING
Definition: IMessageSvc.h:25
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
AvalancheSchedulerErrorTest.msgSvc
msgSvc
Definition: AvalancheSchedulerErrorTest.py:80
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
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
add_serviceLocator
std::conditional_t< implementation_detail::has_serviceLocator< Base >::value, Base, implementation_detail::add_serviceLocator< Base > > add_serviceLocator
Definition: CommonMessaging.h:63
CommonMessagingBase::msg
MsgStream & msg() const
shortcut for the method msgStream(MSG::INFO)
Definition: CommonMessaging.h:123
SmartIF
Definition: IConverter.h:25
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
CommonMessagingBase::msgStream
MsgStream & msgStream() const
Return an uninitialized MsgStream.
Definition: CommonMessaging.h:81
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
MSG::ALWAYS
@ ALWAYS
Definition: IMessageSvc.h:25
implementation_detail::generate_
generate_(name, const std::string &,()) generate_(serviceLocator
MSG::NIL
@ NIL
Definition: IMessageSvc.h:25
MSG::ERROR
@ ERROR
Definition: IMessageSvc.h:25
detected.h
add_name
std::conditional_t< implementation_detail::has_name< Base >::value, Base, implementation_detail::add_name< Base > > add_name
Definition: CommonMessaging.h:60
ISvcLocator.h
CommonMessagingBase::fatal
MsgStream & fatal() const
shortcut for the method msgStream(MSG::FATAL)
Definition: CommonMessaging.h:102
GAUDI_API
#define GAUDI_API
Definition: Kernel.h:81
MsgStream.h