ATLAS Offline Software
getMessageSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2022 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #include <iostream>
6 #include "GaudiKernel/Bootstrap.h"
7 #include "GaudiKernel/IMessageSvc.h"
8 #include "GaudiKernel/ISvcLocator.h"
9 #include "GaudiKernel/ServiceHandle.h"
11 
13 
14 using namespace Athena;
15 
18 std::atomic<bool> Athena::getMessageSvcQuiet;
19 
20 IMessageSvc* Athena::getMessageSvc( bool quiet ) { return getMessageSvc( Options::Lazy, quiet ); }
22 
23  // We cache the MessageSvc, but only once it has been found. This ensures that an
24  // early call to this method (before MessageSvc is available) does not prevent
25  // from finding it in subsequent calls. The limited use of ServiceHandle for this
26  // purpose should be thread-safe:
27  static ServiceHandle<IMessageSvc> msgSvc ATLAS_THREAD_SAFE ("MessageSvc", "getMessageSvc");
28 
29  if (msgSvc.get()) {
30  msgSvc->addRef(); // even if cached, maintain correct ref-count
31  }
32  else {
33  const bool warn = !(quiet || Athena::getMessageSvcQuiet);
34  if ( ((opt==Athena::Options::Lazy && !Gaudi::svcLocator()->existsService("MessageSvc")) ||
35  msgSvc.retrieve().isFailure()) && warn ) {
36  std::cerr << "Athena::getMessageSvc: WARNING MessageSvc not found, will use std::cout" << std::endl;
37  }
38  }
39 
40  return msgSvc.get();
41 }
42 
43 void Athena::reportMessage (IMessageSvc* ims, const std::string &source, int type, const std::string &message) {
44  if (ims) ims->reportMessage(source, type, message);
45 }
46 
47 int Athena::outputLevel(const IMessageSvc* ims, const std::string &source) {
48  if (ims) return ims->outputLevel(source);
49  else return MSG::INFO;
50 }
51 
52 void Athena::setOutputLevel(IMessageSvc* ims, const std::string &source, int level) {
53  if(ims) ims->setOutputLevel(source, level);
54 }
55 
56 IMessageSvcHolder::IMessageSvcHolder(IMessageSvc *ims) : m_ims(ims) {
57  assert(m_ims);
58  m_ims->addRef(); //take ownership till we go out of scope
59 }
60 
62  m_ims(rhs.m_ims)
63 {
64  if (m_ims) m_ims->addRef(); //take ownership till we go out of scope
65 }
66 
69  if (this != & rhs && m_ims != rhs.m_ims) {
70  if (m_ims) m_ims->release(); //give up our IMessageSvc*
71  m_ims = rhs.m_ims;
72  if (m_ims) m_ims->addRef(); //take ownership till we go out of scope
73  }
74  return *this;
75 }
76 
78  m_ims(0)
79 {
81 }
82 
84  if (m_ims) m_ims->release();
85 }
86 
87 IMessageSvc*
89  if (!m_ims) m_ims = getMessageSvc();
90  return m_ims;
91 }
Athena::IMessageSvcHolder::m_ims
IMessageSvc * m_ims
Definition: getMessageSvc.h:89
Athena::Options::Lazy
@ Lazy
Definition: getMessageSvc.h:23
Athena::IMessageSvcHolder::~IMessageSvcHolder
~IMessageSvcHolder()
releases the IMessageSvc
Definition: getMessageSvc.cxx:83
Athena::IMessageSvcHolder
get a IMessageSvc* on 1st use (if not set) and release it on ~
Definition: getMessageSvc.h:60
Athena::IMessageSvcHolder::operator=
IMessageSvcHolder & operator=(const IMessageSvcHolder &)
Definition: getMessageSvc.cxx:68
getMessageSvc.h
singleton-like access to IMessageSvc via open function and helper
Athena::getMessageSvcQuiet
std::atomic< bool > getMessageSvcQuiet
Set this to force off the warning messages from getMessageSvc (in unit tests, for example).
Definition: getMessageSvc.cxx:18
quiet
bool quiet
Definition: TrigGlobEffCorrValidation.cxx:170
Athena::outputLevel
int outputLevel(const IMessageSvc *ims, const std::string &source)
Definition: getMessageSvc.cxx:47
ReweightUtils.message
message
Definition: ReweightUtils.py:15
Athena::getMessageSvc
IMessageSvc * getMessageSvc(bool quiet=false)
Definition: getMessageSvc.cxx:20
Athena::IMessageSvcHolder::get
IMessageSvc * get()
upon first access sets m_ims as needed
Definition: getMessageSvc.cxx:88
python.iconfTool.models.loaders.level
level
Definition: loaders.py:20
Athena::Options::CreateOptions
CreateOptions
Definition: getMessageSvc.h:21
StdJOSetup.msgSvc
msgSvc
Provide convenience handles for various services.
Definition: StdJOSetup.py:35
Athena
Some weak symbol referencing magic...
Definition: AthLegacySequence.h:21
Athena::setOutputLevel
void setOutputLevel(IMessageSvc *ims, const std::string &source, int level)
Definition: getMessageSvc.cxx:52
pmontree.opt
opt
Definition: pmontree.py:16
ReadBadBitsFromCool.warn
warn
Definition: ReadBadBitsFromCool.py:43
Athena::IMessageSvcHolder::IMessageSvcHolder
IMessageSvcHolder()
will take the IMessageSvc* lazily from getMessageSvc() in get() Slow, but useful for classes without ...
Definition: getMessageSvc.h:64
python.CaloScaleNoiseConfig.type
type
Definition: CaloScaleNoiseConfig.py:78
Athena::Options::Eager
@ Eager
Definition: getMessageSvc.h:22
ATLAS_THREAD_SAFE
#define ATLAS_THREAD_SAFE
Definition: checker_macros.h:211
checker_macros.h
Define macros for attributes used to control the static checker.
ServiceHandle< IMessageSvc >
Athena::reportMessage
void reportMessage(IMessageSvc *ims, const std::string &source, int type, const std::string &message)
Wrappers for some of the IMessageSvc methods These can be used from libraries without explicit Gaudi ...
Definition: getMessageSvc.cxx:43