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
|
// Copyright 2018 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "assert.h"
#include <vespa/defaults.h>
#include <vespa/vespalib/util/backtrace.h>
#include <vespa/vespalib/util/stringfmt.h>
#include <vespa/vespalib/component/vtag.h>
#include <fstream>
#include <map>
#include <mutex>
#include <chrono>
#include <iomanip>
#include <vespa/log/log.h>
LOG_SETUP(".vespa.assert");
namespace vespalib::assert {
namespace {
std::mutex _G_lock;
std::map<std::string, size_t> _G_assertMap;
}
size_t
getNumAsserts(const char *key)
{
std::lock_guard guard(_G_lock);
return _G_assertMap[key];
}
vespalib::string
getAssertLogFileName(const char *key)
{
vespalib::string relative = make_string("var/db/vespa/tmp/%s.%s.assert", key, Vtag::currentVersion.toString().c_str());
return vespa::Defaults::underVespaHome(relative.c_str());
}
void
assertOnceOrLog(const char *expr, const char *key, size_t freq)
{
size_t count(0);
{
std::lock_guard guard(_G_lock);
count = _G_assertMap[key]++;
}
if (count) {
if ((count % freq) == 0) {
LOG(error, "assert(%s) named '%s' has failed %zu times. Stacktrace = %s",
expr, key, count+1, vespalib::getStackTrace(0).c_str());
}
} else {
std::string rememberAssert = getAssertLogFileName(key);
std::ifstream prevAssertFile(rememberAssert.c_str());
if (prevAssertFile) {
if ((count % freq) == 0) {
LOG(error, "assert(%s) named '%s' has failed %zu times. Stacktrace = %s",
expr, key, count + 1, vespalib::getStackTrace(0).c_str());
}
} else {
{
LOG(error, "assert(%s) named '%s' failed first time. Stacktrace = %s",
expr, key, vespalib::getStackTrace(0).c_str());
std::ofstream assertStream(rememberAssert.c_str());
std::chrono::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
assertStream << std::put_time(std::gmtime(&now_c), "%F %T") << " assert(" << expr
<< ") named " << key << " failed" << std::endl;
assertStream.close();
}
abort();
}
}
}
}
|