aboutsummaryrefslogtreecommitdiffstats
path: root/vespalib/src/vespa/vespalib/trace/slime_trace_serializer.cpp
blob: 1d18170f5551c04eae3bb6a07db572cf17b833c7 (plain) (blame)
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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include "slime_trace_serializer.h"
#include "trace.h"
#include <cassert>

using namespace vespalib::slime;

namespace vespalib {

const Memory SlimeTraceSerializer::TIMESTAMP("timestamp");
const Memory SlimeTraceSerializer::PAYLOAD("payload");
const Memory SlimeTraceSerializer::CHILDREN("children");

SlimeTraceSerializer::SlimeTraceSerializer(Cursor & cursor)
    : _cursors()
{
    _cursors.push(&cursor);
}

void
SlimeTraceSerializer::visit(const TraceNode & node)
{
    assert(!_cursors.empty());
    Cursor * current(_cursors.top());
    assert(current != NULL);
    _cursors.pop();
    addTimestamp(*current, node);
    addPayload(*current, node);
    addChildrenCursors(*current, node);
}

void
SlimeTraceSerializer::addTimestamp(Cursor & current, const TraceNode & node)
{
    current.setLong(TIMESTAMP, count_ms(node.getTimestamp().time_since_epoch()));
}

void
SlimeTraceSerializer::addPayload(Cursor & current, const TraceNode & node)
{
    if (node.hasNote()) {
        current.setString(PAYLOAD, Memory(node.getNote()));
    }
}

void
SlimeTraceSerializer::addChildrenCursors(Cursor & current, const TraceNode & node)
{
    if (node.getNumChildren() > 0) {
        addChildrenCursorsToStack(current.setArray(CHILDREN), node);
    }
}

void
SlimeTraceSerializer::addChildrenCursorsToStack(Cursor & childrenArray, const TraceNode & node)
{
    for (uint32_t childIndex(0); childIndex < node.getNumChildren(); childIndex++) {
        Cursor & child(childrenArray.addObject());
        _cursors.push(&child);
    }
}

}