forked from p4lang/p4c
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog.cpp
360 lines (317 loc) · 11.2 KB
/
log.cpp
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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
/*
Copyright 2013-present Barefoot Networks, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "config.h"
#if HAVE_LIBGC
#include <gc/gc_cpp.h>
#define NOGC_ARGS (NoGC, 0, 0)
#else
#define NOGC_ARGS
#endif /* HAVE_LIBGC */
#include <cstring>
#include <fstream> // IWYU pragma: keep
#include <iomanip>
#include <iostream>
#include <memory>
#include <sstream>
#include <string>
#include <unordered_map>
#include <vector>
#include "log.h"
#ifdef MULTITHREAD
#include <mutex>
#endif // MULTITHREAD
namespace Log {
namespace Detail {
int verbosity = 0;
int maximumLogLevel = 0;
bool enableLoggingGlobally = true;
bool enableLoggingInContext = false;
// The time at which logging was initialized; used so that log messages can have
// relative rather than absolute timestamps.
static uint64_t initTime = 0;
struct LevelAndOutput {
int level = -1;
std::ostream *out = nullptr;
};
// The first level cache for fileLogLevel() - the most recent result returned.
static const char *mostRecentFile = nullptr;
static LevelAndOutput *mostRecentInfo = nullptr;
// The second level cache for fileLogLevel(), mapping filenames to log levels.
static std::unordered_map<const void *, LevelAndOutput> logLevelCache;
// All log levels manually specified by the user.
static std::vector<std::string> debugSpecs;
// Each logfile will only be opened once, and will close when we exit.
static std::unordered_map<std::string, std::unique_ptr<std::ostream>> logfiles;
static std::vector<void (*)(void)> invalidateCallbacks;
int OutputLogPrefix::ostream_xalloc = -1;
void OutputLogPrefix::setup_ostream_xalloc(std::ostream &out) {
if (ostream_xalloc < 0) {
#ifdef MULTITHREAD
static std::mutex lock;
std::lock_guard<std::mutex> acquire(lock);
if (ostream_xalloc < 0)
#endif // MULTITHREAD
ostream_xalloc = out.xalloc();
}
}
#ifdef MULTITHREAD
struct OutputLogPrefix::lock_t {
int refcnt = 1;
std::mutex theMutex;
void lock() { theMutex.lock(); }
void unlock() { theMutex.unlock(); }
static void cleanup(std::ios_base::event event, std::ios_base &out, int index) {
if (event == std::ios_base::erase_event) {
auto *p = static_cast<lock_t *>(out.pword(index));
if (p && --p->refcnt <= 0) delete p;
} else if (event == std::ios_base::copyfmt_event) {
auto *p = static_cast<lock_t *>(out.pword(index));
if (p) p->refcnt++;
}
}
};
#endif // MULTITHREAD
OutputLogPrefix::~OutputLogPrefix() {
#ifdef MULTITHREAD
if (lock) lock->unlock();
#endif // MULTITHREAD
}
void OutputLogPrefix::indent(std::ostream &out) {
setup_ostream_xalloc(out);
if (int pfx = out.iword(ostream_xalloc)) out << std::setw(pfx) << ':';
out << indent_t::getindent(out);
}
std::ostream &operator<<(std::ostream &out, const OutputLogPrefix &pfx) {
std::stringstream tmp;
#ifdef CLOCK_MONOTONIC
if (LOGGING(2)) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
uint64_t t = ts.tv_sec * 1000000000UL + ts.tv_nsec - Log::Detail::initTime;
t /= 1000000UL; // millisec
tmp << t / 1000 << '.' << std::setw(3) << std::setfill('0') << t % 1000 << ':'
<< std::setfill(' ');
}
#endif
if (LOGGING(1)) {
const char *s = strrchr(pfx.fn, '/');
const char *e = strrchr(pfx.fn, '.');
s = s ? s + 1 : pfx.fn;
if (e && e > s)
tmp.write(s, e - s);
else
tmp << s;
tmp << ':' << pfx.level << ':';
}
pfx.setup_ostream_xalloc(out);
#ifdef MULTITHREAD
if (!(pfx.lock = static_cast<OutputLogPrefix::lock_t *>(out.pword(pfx.ostream_xalloc)))) {
static std::mutex lock;
std::lock_guard<std::mutex> acquire(lock);
if (!(pfx.lock = static_cast<OutputLogPrefix::lock_t *>(out.pword(pfx.ostream_xalloc)))) {
out.pword(pfx.ostream_xalloc) = pfx.lock = new NOGC_ARGS OutputLogPrefix::lock_t;
out.register_callback(OutputLogPrefix::lock_t::cleanup, pfx.ostream_xalloc);
}
}
pfx.lock->lock();
#endif // MULTITHREAD
if (tmp.str().size() > 0) {
out.iword(OutputLogPrefix::ostream_xalloc) = tmp.str().size();
out << tmp.str();
}
out << indent_t::getindent(out);
return out;
}
std::ostream &clearPrefix(std::ostream &out) {
if (OutputLogPrefix::ostream_xalloc >= 0) out.iword(OutputLogPrefix::ostream_xalloc) = 0;
return out;
}
static bool match(const char *pattern, const char *name) {
const char *pend = pattern + strcspn(pattern, ",:");
const char *pbackup = 0;
while (1) {
while (pattern < pend && *pattern == *name) {
pattern++;
name++;
}
if (pattern == pend) {
if (!strcmp(name, ".cpp") || !strcmp(name, ".h")) return true;
return *name == 0;
}
if (*pattern == '[') {
bool negate = false;
if (pattern[1] == '^') {
negate = true;
++pattern;
}
while ((*++pattern != *name || pattern[1] == '-') && *pattern != ']' && *pattern) {
if (pattern[1] == '-' && pattern[2] != ']') {
if (*name >= pattern[0] && *name <= pattern[2]) break;
pattern += 2;
}
}
if ((*pattern == ']' || !*pattern) ^ negate) return false;
while (*pattern && *pattern++ != ']') continue;
if (pattern > pend) pend = pattern + strcspn(pattern, ",:");
name++;
continue;
}
if (!pbackup && *pattern != '*') return false;
while (*pattern == '*') {
++pattern;
pbackup = nullptr;
}
if (pattern == pend) return true;
// FIXME -- does not work for * followed by [ -- matches a literal [ instead.
while (*name && *name != *pattern) {
if (pbackup && *name == *pbackup) {
pattern = pbackup;
break;
}
name++;
}
if (!*name) return false;
pbackup = pattern;
}
}
const char *uncachedFileLogSpec(const char *file) {
if (auto *startOfFilename = strrchr(file, '/')) file = startOfFilename + 1;
for (auto &spec : debugSpecs)
for (auto *pattern = spec.c_str(); pattern; pattern = strchr(pattern, ',')) {
while (*pattern == ',') pattern++;
if (match(pattern, file))
if (auto *level = strchr(pattern, ':')) return level + 1;
}
return nullptr;
}
int uncachedFileLogLevel(const char *file) {
if (auto spec = uncachedFileLogSpec(file)) return atoi(spec);
// If there's no matching spec, compute a default from the global verbosity level,
// except for THIS file
if (!strcmp(file, __FILE__)) return 0;
return verbosity > 0 ? verbosity - 1 : 0;
}
LevelAndOutput *cachedFileLogInfo(const char *file) {
#ifdef MULTITHREAD
static std::mutex lock;
std::lock_guard<std::mutex> acquire(lock);
#endif // MULTITHREAD
// There are two layers of caching here. First, we cache the most recent
// result we returned, to minimize expensive lookups in tight loops.
if (mostRecentFile == file) return mostRecentInfo;
// Second, we look up @file in a hash table mapping from pointers to log
// info. We expect to hit in this cache virtually all the time.
mostRecentFile = file;
return mostRecentInfo = &logLevelCache[file];
}
int fileLogLevel(const char *file) {
auto *info = cachedFileLogInfo(file);
if (info->level == -1) {
// This is the slow path. We have to walk @debugSpecs to see if there are any
// specs that match @file. There's a race here in that two threads could do this
// at the same time, but they should get the same result.
info->level = uncachedFileLogLevel(file);
}
return info->level;
}
std::ostream &uncachedFileLogOutput(const char *file) {
if (auto spec = uncachedFileLogSpec(file)) {
while (isdigit(*spec)) ++spec;
if (*spec == '>') {
std::ios_base::openmode mode = std::ios_base::out;
if (*++spec == '>') {
mode |= std::ios_base::app;
++spec;
}
const char *end = strchr(spec, ',');
if (!end) end = spec + strlen(spec);
std::string logname(spec, end - spec);
if (!logfiles.count(logname)) {
// FIXME: can't emplace a unique_ptr in some versions of GCC -- need
// explicit reset call.
logfiles[logname].reset(new std::ofstream(logname, mode));
}
return *logfiles.at(logname);
}
}
return std::clog;
}
std::ostream &fileLogOutput(const char *file) {
auto *info = cachedFileLogInfo(file);
if (!info->out) {
#ifdef MULTITHREAD
static std::mutex lock;
std::lock_guard<std::mutex> acquire(lock);
#endif // MULTITHREAD
if (!info->out) {
info->out = &uncachedFileLogOutput(file);
}
}
return *info->out;
}
void invalidateCaches(int possibleNewMaxLogLevel) {
mostRecentFile = nullptr;
mostRecentInfo = nullptr;
logLevelCache.clear();
maximumLogLevel = std::max(maximumLogLevel, possibleNewMaxLogLevel);
for (auto fn : invalidateCallbacks) fn();
}
void addInvalidateCallback(void (*fn)(void)) { invalidateCallbacks.push_back(fn); }
} // namespace Detail
void addDebugSpec(const char *spec) {
#ifdef CLOCK_MONOTONIC
if (!Detail::initTime) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
Detail::initTime = ts.tv_sec * 1000000000UL + ts.tv_nsec;
}
#endif
// Validate @spec.
bool ok = false;
long maxLogLevelInSpec = 0;
for (auto *pattern = strchr(spec, ':'); pattern; pattern = strchr(pattern, ':')) {
ok = true;
long level = strtol(pattern + 1, const_cast<char **>(&pattern), 10);
if (*pattern && *pattern != ',' && *pattern != '>') {
ok = false;
break;
}
maxLogLevelInSpec = std::max(maxLogLevelInSpec, level);
}
if (!ok) {
std::cerr << "Invalid debug trace spec '" << spec << "'" << std::endl;
return;
}
#ifdef MULTITHREAD
static std::mutex lock;
std::lock_guard<std::mutex> acquire(lock);
#endif // MULTITHREAD
Detail::debugSpecs.push_back(spec);
Detail::invalidateCaches(maxLogLevelInSpec);
}
void increaseVerbosity() {
#ifdef MULTITHREAD
static std::mutex lock;
std::lock_guard<std::mutex> acquire(lock);
#endif // MULTITHREAD
#ifdef CLOCK_MONOTONIC
if (!Detail::initTime) {
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
Detail::initTime = ts.tv_sec * 1000000000UL + ts.tv_nsec;
}
#endif
Detail::verbosity++;
Detail::invalidateCaches(Detail::verbosity - 1);
}
} // namespace Log