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
|
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "trace.h"
#include <vespa/vespalib/trace/slime_trace_serializer.h>
#include <vespa/vespalib/trace/slime_trace_deserializer.h>
#include <vespa/vespalib/data/slime/slime.h>
#include <vespa/vespalib/data/simple_buffer.h>
using namespace vespalib;
using namespace vespalib::slime;
namespace config {
struct SystemClock : public Clock
{
vespalib::system_time currentTime() const override {
return vespalib::system_clock::now();
}
};
static SystemClock systemClock;
const Memory Trace::TRACELOG("traceLog");
const Memory Trace::TRACELEVEL("traceLevel");
Trace::Trace(const Trace & other)
: _root(other._root),
_traceLevel(other._traceLevel),
_clock(other._clock)
{
}
Trace::Trace()
: _root(),
_traceLevel(0),
_clock(systemClock)
{
}
Trace::Trace(uint32_t traceLevel)
: _traceLevel(traceLevel),
_clock(systemClock)
{
}
Trace::Trace(uint32_t traceLevel, const Clock & clock)
: _traceLevel(traceLevel),
_clock(clock)
{
}
void
Trace::deserialize(const Inspector & inspector)
{
_traceLevel = inspector[TRACELEVEL].asLong();
deserializeTraceLog(inspector[TRACELOG]);
}
void
Trace::deserializeTraceLog(const Inspector & root)
{
SlimeTraceDeserializer deserializer(root);
_root = deserializer.deserialize();
}
bool
Trace::shouldTrace(uint32_t level) const
{
return (level <= _traceLevel);
}
void
Trace::trace(uint32_t level, const vespalib::string & message)
{
if (shouldTrace(level)) {
_root.addChild(message, _clock.currentTime());
}
}
void
Trace::serialize(Cursor & cursor) const
{
cursor.setLong(TRACELEVEL, _traceLevel);
SlimeTraceSerializer serializer(cursor.setObject(TRACELOG));
_root.accept(serializer);
}
void
Trace::serializeTraceLog(Cursor & array) const
{
for (uint32_t i(0); i < _root.getNumChildren(); i++) {
SlimeTraceSerializer serializer(array.addObject());
_root.getChild(i).accept(serializer);
}
}
vespalib::string
Trace::toString() const
{
Slime slime;
serializeTraceLog(slime.setArray());
SimpleBuffer buf;
JsonFormat::encode(slime.get(), buf, false);
return buf.get().make_string();
}
} // namespace config
|