ATLAS Offline Software
Loading...
Searching...
No Matches
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
36 std::vector<std::string>
39};
40
41inline MessageHelper::MessageHelper(const AthAlgTool& parent, unsigned int num)
42 : m_parent(parent),
44 m_warningCounts(num),
45 m_warningText(num) {}
46
47inline void MessageHelper::incrementCount(unsigned int messageNumber) {
48 m_warningCounts[messageNumber]++;
49}
50
51inline 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
76inline 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
88inline 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
102inline void MessageHelper::setMaxNumberOfMessagesPrinted(unsigned int num) {
103 m_maxWarnings = num;
104}
105
106inline 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
114inline 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
#define endmsg
Define macros for attributes used to control the static checker.
void printSummary(void) const
const AthAlgTool & m_parent
void setMaxNumberOfMessagesPrinted(unsigned int num)
void setMessage(unsigned int messageNumber, const std::string &message)
MessageHelper(const AthAlgTool &parent, unsigned int num)
Pass to the constructor the number of messages.
void printWarning(unsigned int messageNumber) const
std::vector< std::string > m_warningText
The text for the WARNINGs (the index is the error number).
std::vector< std::atomic< unsigned int > > m_warningCounts ATLAS_THREAD_SAFE
The counts per error (the index is the error number)
void incrementCount(unsigned int messageNumber)
bool wouldPrintWarning(unsigned int messageNumber) const
unsigned int m_maxWarnings
Maximum number of WARNING messages permitted.
int count(std::string s, const std::string &regx)
count how many occurances of a regx are in a string
Definition hcg.cxx:146