ATLAS Offline Software
getMessageSvc.cxx
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2025 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  IMessageSvc* svc = nullptr;
30  try {
31  svc = msgSvc.get();
32  if (svc) {
33  svc->addRef(); // even if cached, maintain correct ref-count
34  return svc;
35  }
36 
37  const bool warn = !(quiet || Athena::getMessageSvcQuiet);
38  if ( ((opt==Athena::Options::Lazy && !Gaudi::svcLocator()->existsService("MessageSvc")) ||
39  msgSvc.retrieve().isFailure()) && warn ) {
40  std::cerr << "Athena::getMessageSvc: WARNING MessageSvc not found, will use std::cout" << std::endl;
41  }
42  svc = msgSvc.get();
43  }
44  catch (const GaudiException& e)
45  {
46  std::cerr << "Athena::getMessageSvc: WARNING Got Gaudi exception " << e
47  << "; will use std::out" << std::endl;
48  return nullptr;
49  }
50 
51 
52  return svc;
53 }
54 
55 
56 void Athena::reportMessage (IMessageSvc* ims, const std::string &source, int type, const std::string &message) {
57  if (ims) ims->reportMessage(source, type, message);
58 }
59 
60 int Athena::outputLevel(const IMessageSvc* ims, const std::string &source) {
61  if (ims) return ims->outputLevel(source);
62  else return MSG::INFO;
63 }
64 
65 void Athena::setOutputLevel(IMessageSvc* ims, const std::string &source, int level) {
66  if(ims) ims->setOutputLevel(source, level);
67 }
68 
69 IMessageSvcHolder::IMessageSvcHolder(IMessageSvc *ims) : m_ims(ims) {
70  assert(m_ims);
71  m_ims->addRef(); //take ownership till we go out of scope
72 }
73 
75  m_ims(rhs.m_ims)
76 {
77  if (m_ims) m_ims->addRef(); //take ownership till we go out of scope
78 }
79 
82  if (this != & rhs && m_ims != rhs.m_ims) {
83  if (m_ims) m_ims->release(); //give up our IMessageSvc*
84  m_ims = rhs.m_ims;
85  if (m_ims) m_ims->addRef(); //take ownership till we go out of scope
86  }
87  return *this;
88 }
89 
91  m_ims(0)
92 {
94 }
95 
97  if (m_ims) m_ims->release();
98 }
99 
100 IMessageSvc*
102  if (!m_ims) m_ims = getMessageSvc();
103  return m_ims;
104 }
Athena::IMessageSvcHolder::m_ims
IMessageSvc * m_ims
Definition: getMessageSvc.h:89
Athena::Options::Lazy
@ Lazy
Definition: getMessageSvc.h:23
AllowedVariables::e
e
Definition: AsgElectronSelectorTool.cxx:37
Athena::IMessageSvcHolder::~IMessageSvcHolder
~IMessageSvcHolder()
releases the IMessageSvc
Definition: getMessageSvc.cxx:96
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:81
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
make_coralServer_rep.opt
opt
Definition: make_coralServer_rep.py:19
quiet
bool quiet
Definition: TrigGlobEffCorrValidation.cxx:190
Athena::outputLevel
int outputLevel(const IMessageSvc *ims, const std::string &source)
Definition: getMessageSvc.cxx:60
ReweightUtils.message
message
Definition: ReweightUtils.py:15
python.CaloAddPedShiftConfig.type
type
Definition: CaloAddPedShiftConfig.py:42
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:101
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:36
Athena
Some weak symbol referencing magic...
Definition: AthLegacySequence.h:21
Handler::svc
AthROOTErrorHandlerSvc * svc
Definition: AthROOTErrorHandlerSvc.cxx:10
Athena::setOutputLevel
void setOutputLevel(IMessageSvc *ims, const std::string &source, int level)
Definition: getMessageSvc.cxx:65
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.Constants.INFO
int INFO
Definition: Control/AthenaCommon/python/Constants.py:15
Athena::Options::Eager
@ Eager
Definition: getMessageSvc.h:22
copySelective.source
string source
Definition: copySelective.py:31
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:56