ATLAS Offline Software
MessageHelper.h
Go to the documentation of this file.
1 /*
2  Copyright (C) 2002-2023 CERN for the benefit of the ATLAS collaboration
3 */
4 
5 #ifndef MESSAGEHELPER_H
6 #define MESSAGEHELPER_H
7 
8 #include <atomic>
9 #include <sstream>
10 #include <string>
11 #include <vector>
12 
15 
17  public:
19  MessageHelper(const AthAlgTool& parent, unsigned int num);
20  void incrementCount(unsigned int messageNumber);
21  void printSummary(void) const;
22  void printWarning(unsigned int messageNumber) const;
23  void printWarning(unsigned int messageNumber,
24  const std::string& addition) const;
25  void setMaxNumberOfMessagesPrinted(unsigned int num);
26  void setMessage(unsigned int messageNumber, const std::string& message);
27  bool wouldPrintWarning(unsigned int messageNumber) const;
28 
29  private:
31  unsigned int
33  mutable std::vector<std::atomic<unsigned int> > m_warningCounts
35  std::vector<std::string>
38 };
40 
41 inline MessageHelper::MessageHelper(const AthAlgTool& parent, unsigned int num)
42  : m_parent(parent),
43  m_maxWarnings(3),
44  m_warningCounts(num),
45  m_warningText(num) {}
46 
47 inline void MessageHelper::incrementCount(unsigned int messageNumber) {
48  m_warningCounts[messageNumber]++;
49 }
50 
51 inline void MessageHelper::printSummary(void) const {
52  bool haveWarnings = false;
53  for (unsigned int i = 0; i < m_warningCounts.size(); ++i) {
54  if (!m_warningCounts[i])
55  continue;
56  haveWarnings = true;
57  break;
58  }
59  if (!haveWarnings)
60  return;
61 
62  std::stringstream out;
63  out << " summary of WARNING occurences (maximum shown during job: "
64  << m_maxWarnings << "):";
65  m_parent.msg(MSG::INFO) << out.str() << endmsg;
66 
67  for (unsigned int i = 0; i < m_warningCounts.size(); ++i) {
68  if (!m_warningCounts[i])
69  continue;
70  out.str("");
71  out << " " << m_warningCounts[i] << ":\t" << m_warningText[i];
72  m_parent.msg(MSG::INFO) << out.str() << endmsg;
73  }
74 }
75 
76 inline void MessageHelper::printWarning(unsigned int messageNumber) const {
77  unsigned int count = ++m_warningCounts[messageNumber];
78  if (!m_parent.msgLvl(MSG::WARNING) || count > m_maxWarnings)
79  return;
80 
81  m_parent.msg(MSG::WARNING) << m_warningText[messageNumber] << endmsg;
82  if (count == m_maxWarnings)
83  m_parent.msg(MSG::WARNING)
84  << "Limit reached. No more messages of this type will be printed."
85  << endmsg;
86 }
87 
88 inline void MessageHelper::printWarning(unsigned int messageNumber,
89  const std::string& addition) const {
90  unsigned int count = ++m_warningCounts[messageNumber];
91  if (!m_parent.msgLvl(MSG::WARNING) || count > m_maxWarnings)
92  return;
93 
94  m_parent.msg(MSG::WARNING)
95  << m_warningText[messageNumber] << addition << endmsg;
96  if (count == m_maxWarnings)
97  m_parent.msg(MSG::WARNING)
98  << "Limit reached. No more messages of this type will be printed."
99  << endmsg;
100 }
101 
103  m_maxWarnings = num;
104 }
105 
106 inline void MessageHelper::setMessage(unsigned int messageNumber,
107  const std::string& message) {
108  if (messageNumber >= m_warningCounts.size())
109  std::abort();
110  m_warningCounts[messageNumber] = 0;
111  m_warningText[messageNumber] = message;
112 }
113 
114 inline bool MessageHelper::wouldPrintWarning(unsigned int messageNumber) const {
115  if (m_parent.msgLvl(MSG::WARNING) &&
116  m_warningCounts[messageNumber] <= m_maxWarnings)
117  return true;
118  return false;
119 }
120 
121 #endif
MessageHelper
Definition: MessageHelper.h:16
MessageHelper::setMaxNumberOfMessagesPrinted
void setMaxNumberOfMessagesPrinted(unsigned int num)
Definition: MessageHelper.h:102
python.AthDsoLogger.out
out
Definition: AthDsoLogger.py:71
AthCommonMsg::msgLvl
bool msgLvl(const MSG::Level lvl) const
Definition: AthCommonMsg.h:30
ReweightUtils.message
message
Definition: ReweightUtils.py:15
XMLtoHeader.count
count
Definition: XMLtoHeader.py:85
MessageHelper::printSummary
void printSummary(void) const
Definition: MessageHelper.h:51
lumiFormat.i
int i
Definition: lumiFormat.py:92
endmsg
#define endmsg
Definition: AnalysisConfig_Ntuple.cxx:63
AthAlgTool.h
test_pyathena.parent
parent
Definition: test_pyathena.py:15
MessageHelper::m_warningText
std::vector< std::string > m_warningText
The text for the WARNINGs (the index is the error number).
Definition: MessageHelper.h:37
MessageHelper::m_parent
const AthAlgTool & m_parent
Definition: MessageHelper.h:30
MessageHelper::ATLAS_THREAD_SAFE
std::vector< std::atomic< unsigned int > > m_warningCounts ATLAS_THREAD_SAFE
The counts per error (the index is the error number)
Definition: MessageHelper.h:34
trigbs_pickEvents.num
num
Definition: trigbs_pickEvents.py:76
MessageHelper::m_maxWarnings
unsigned int m_maxWarnings
Maximum number of WARNING messages permitted.
Definition: MessageHelper.h:32
MessageHelper::wouldPrintWarning
bool wouldPrintWarning(unsigned int messageNumber) const
Definition: MessageHelper.h:114
MessageHelper::setMessage
void setMessage(unsigned int messageNumber, const std::string &message)
Definition: MessageHelper.h:106
MessageHelper::MessageHelper
MessageHelper(const AthAlgTool &parent, unsigned int num)
Pass to the constructor the number of messages.
Definition: MessageHelper.h:41
MessageHelper::printWarning
void printWarning(unsigned int messageNumber) const
Definition: MessageHelper.h:76
AthCommonMsg::msg
MsgStream & msg() const
Definition: AthCommonMsg.h:24
AthAlgTool
Definition: AthAlgTool.h:26
checker_macros.h
Define macros for attributes used to control the static checker.
MessageHelper::incrementCount
void incrementCount(unsigned int messageNumber)
Definition: MessageHelper.h:47