forked from aristanetworks/EosSdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracing.cpp
76 lines (61 loc) · 1.91 KB
/
tracing.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
// Copyright (c) 2014 Arista Networks, Inc. All rights reserved.
// Arista Networks, Inc. Confidential and Proprietary.
#include <cstdarg>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <eos/tracing.h>
namespace eos {
struct tracer_internal {
explicit tracer_internal(char const * name_)
: name(name_), levelEnabled(9, true) {
}
void trace(trace_level level, char const * expression, va_list args) {
char * expandedExpression = NULL;
if (vasprintf(&expandedExpression, expression, args) != -1) {
fprintf(stderr, "[%s]\t%s\n", name, expandedExpression);
free(expandedExpression);
}
}
char const * name;
std::vector<bool> levelEnabled;
};
tracer::tracer(char const * name) : tracer_(new tracer_internal(name)) {
}
tracer::~tracer() {
delete tracer_;
}
bool tracer::enabled(trace_level level) {
return tracer_->levelEnabled[level];
}
void tracer::enabled_is(trace_level level, bool value) {
tracer_->levelEnabled[level] = value;
}
void tracer::trace(trace_level level, char const * expression, ...) {
if (enabled(level)) {
va_list args;
va_start(args, expression);
tracer_->trace(level, expression, args);
va_end(args);
}
}
#define TRACE(level) void tracer::trace ## level (char const * expression, ...) { \
if (enabled(static_cast<trace_level>(level))) { \
va_list args; \
va_start(args, expression); \
tracer_->trace(static_cast<trace_level>(level), expression, args); \
va_end(args); \
} \
}
TRACE(0)
TRACE(1)
TRACE(2)
TRACE(3)
TRACE(4)
TRACE(5)
TRACE(6)
TRACE(7)
TRACE(8)
TRACE(9)
#undef TRACE
}