Repository navigation
Expand file tree
/
Copy patherror_reporter.cpp
More file actions
324 lines (267 loc) · 8.22 KB
/
Copy patherror_reporter.cpp
File metadata and controls
324 lines (267 loc) · 8.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/*
This source file is part of KBEngine
For the latest info, see http://www.kbengine.org/
Copyright (c) 2008-2016 KBEngine.
KBEngine is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
KBEngine is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with KBEngine. If not, see <http://www.gnu.org/licenses/>.
*/
#include "error_reporter.h"
#ifndef CODE_INLINE
#include "error_reporter.inl"
#endif
#include "network/address.h"
#include "network/event_dispatcher.h"
#include "network/endpoint.h"
namespace KBEngine {
namespace Network
{
const uint ErrorReporter::ERROR_REPORT_MIN_PERIOD_MS = 2000; // 2 seconds
/**
* The nominal maximum time that a report count for a Network address and
* error is kept after the last raising of the error.
*/
const uint ErrorReporter::ERROR_REPORT_COUNT_MAX_LIFETIME_MS = 10000; // 10 seconds
/**
* Constructor.
*/
ErrorReporter::ErrorReporter(EventDispatcher & dispatcher) :
reportLimitTimerHandle_(),
errorsAndCounts_()
{
// report any pending exceptions every so often
reportLimitTimerHandle_ = dispatcher.addTimer(
ERROR_REPORT_MIN_PERIOD_MS * 1000, this);
}
/**
* Destructor.
*/
ErrorReporter::~ErrorReporter()
{
reportLimitTimerHandle_.cancel();
}
std::string ErrorReporter::addressErrorToString(const Address & address,
const std::string & errorString)
{
std::ostringstream out;
out << address.c_str() << ": " << errorString;
return out.str();
}
std::string ErrorReporter::addressErrorToString(
const Address & address,
const std::string & errorString,
const ErrorReportAndCount & reportAndCount,
const uint64 & now)
{
int64 deltaStamps = now - reportAndCount.lastReportStamps;
double deltaMillis = 1000 * deltaStamps / stampsPerSecondD();
char * buf = NULL;
int bufLen = 64;
int strLen = bufLen;
do
{
bufLen = strLen + 1;
delete [] buf;
buf = new char[ bufLen ];
#if KBE_PLATFORM == PLATFORM_WIN32
strLen = _snprintf(buf, bufLen, "%d reports of '%s' "
"in the last %.00fms",
reportAndCount.count,
addressErrorToString(address, errorString).c_str(),
deltaMillis);
if (strLen == -1) strLen = (bufLen - 1) * 2;
#else
strLen = snprintf(buf, bufLen, "%d reports of '%s' "
"in the last %.00fms",
reportAndCount.count,
addressErrorToString(address, errorString).c_str(),
deltaMillis);
#endif
} while (strLen >= bufLen);
std::string out(buf);
delete [] buf;
return out;
}
/**
* Report a general error with printf style format string. If repeatedly the
* resulting formatted string is reported within the minimum output window,
* they are accumulated and output after the minimum output window has passed.
*/
void ErrorReporter::reportError(
const Address & address, const char* format, ...)
{
char * buf = NULL;
int bufLen = 32;
int strLen = bufLen;
do
{
delete [] buf;
bufLen = strLen + 1;
buf = new char[ bufLen ];
va_list va;
va_start(va, format);
#if KBE_PLATFORM == PLATFORM_WIN32
strLen = _vsnprintf(buf, bufLen, format, va);
if (strLen == -1) strLen = (bufLen - 1) * 2;
#else
strLen = vsnprintf(buf, bufLen, format, va);
#endif
va_end(va);
buf[bufLen -1] = '\0';
} while (strLen >= bufLen);
std::string error(buf);
delete [] buf;
this->addReport(address, error);
}
/**
*
*/
void ErrorReporter::reportException(Reason reason,
const Address & addr,
const char* prefix)
{
NetworkException ne(reason, addr);
this->reportException(ne, prefix);
}
/**
* Output the exception if it has not occurred before, otherwise only
* output the exception if the minimum period has elapsed since the
* last outputting of this exception.
*
* @param ne the NubException
* @param prefix any prefix to add to the error message, or NULL if no prefix
*
*/
void ErrorReporter::reportException(const NetworkException & ne, const char* prefix)
{
Address offender(0, 0);
ne.getAddress(offender);
if (prefix)
{
this->reportError(offender,
"%s: Exception occurred: %s",
prefix, reasonToString(ne.reason()));
}
else
{
this->reportError(offender, "Exception occurred: %s",
reasonToString(ne.reason()));
}
}
/**
* Adds a new error message for an address to the reporter count map.
* Emits an error message if there has been no previous equivalent error
* string provider for this address.
*/
void ErrorReporter::addReport(const Address & address, const std::string & errorString)
{
AddressAndErrorString addressError(address, errorString);
ErrorsAndCounts::iterator searchIter =
errorsAndCounts_.find(addressError);
uint64 now = timestamp();
// see if we have ever reported this error
if (searchIter != errorsAndCounts_.end())
{
// this error has been reported recently..
ErrorReportAndCount & reportAndCount = searchIter->second;
reportAndCount.count++;
int64 millisSinceLastReport = 1000 *
(now - reportAndCount.lastReportStamps) /
stampsPerSecond();
reportAndCount.lastRaisedStamps = now;
if (millisSinceLastReport >= ERROR_REPORT_MIN_PERIOD_MS)
{
ERROR_MSG(fmt::format("{}\n",
addressErrorToString(address, errorString,
reportAndCount, now).c_str()));
reportAndCount.count = 0;
reportAndCount.lastReportStamps = now;
}
}
else
{
ERROR_MSG(fmt::format("{}\n",
addressErrorToString(address, errorString).c_str()));
ErrorReportAndCount reportAndCount = {
now, // lastReportStamps,
now, // lastRaisedStamps,
0, // count
};
errorsAndCounts_[ addressError ] = reportAndCount;
}
}
/**
* Output all exception's reports that have not yet been output.
*/
void ErrorReporter::reportPendingExceptions(bool reportBelowThreshold)
{
uint64 now = timestamp();
// this is set to any iterator slated for removal
ErrorsAndCounts::iterator staleIter = this->errorsAndCounts_.end();
for ( ErrorsAndCounts::iterator exceptionCountIter =
this->errorsAndCounts_.begin();
exceptionCountIter != this->errorsAndCounts_.end();
++exceptionCountIter)
{
// remove any stale mappings from the last loop's run
if (staleIter != this->errorsAndCounts_.end())
{
this->errorsAndCounts_.erase(staleIter);
staleIter = this->errorsAndCounts_.end();
}
// check this iteration's last report and see if we need to output
// anything
const AddressAndErrorString & addressError =
exceptionCountIter->first;
ErrorReportAndCount & reportAndCount = exceptionCountIter->second;
int64 millisSinceLastReport = 1000 *
(now - reportAndCount.lastReportStamps) / stampsPerSecond();
if (reportBelowThreshold ||
millisSinceLastReport >= ERROR_REPORT_MIN_PERIOD_MS)
{
if (reportAndCount.count)
{
ERROR_MSG(fmt::format("{}\n",
addressErrorToString(
addressError.first, addressError.second,
reportAndCount, now).c_str()
));
reportAndCount.count = 0;
reportAndCount.lastReportStamps = now;
}
}
// see if we can remove this mapping if it has not been raised in a
// while
uint64 sinceLastRaisedMillis = 1000 * (now - reportAndCount.lastRaisedStamps) /
stampsPerSecond();
if (sinceLastRaisedMillis > ERROR_REPORT_COUNT_MAX_LIFETIME_MS)
{
// it's hung around for too long without being raised again,
// so remove it in the next iteration
staleIter = exceptionCountIter;
}
}
// remove the last mapping if it is marked stale
if (staleIter != this->errorsAndCounts_.end())
{
this->errorsAndCounts_.erase(staleIter);
}
}
/**
* This method handles the timer event and checks to see if any delayed
* messages should be reported.
*/
void ErrorReporter::handleTimeout(TimerHandle handle, void * arg)
{
KBE_ASSERT(handle == reportLimitTimerHandle_);
this->reportPendingExceptions();
}
}
}