aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/string_utils/tskv_format/builder.h
blob: 40689ddc85ed3c4378f6eb39020798386022863a (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
64
65
66
67
#pragma once

#include "escape.h"

#include <util/stream/str.h>

namespace NTskvFormat {
    class TLogBuilder {
    private:
        TStringStream Out;

    public:
        TLogBuilder() = default;

        TLogBuilder(TStringBuf logType, ui32 unixtime) {
            Begin(logType, unixtime);
        }

        TLogBuilder(TStringBuf logType) {
            Begin(logType);
        }

        TLogBuilder& Add(TStringBuf fieldName, TStringBuf fieldValue) {
            if (!Out.Empty()) {
                Out << '\t';
            }
            Escape(fieldName, Out.Str());
            Out << '=';
            Escape(fieldValue, Out.Str());

            return *this;
        }

        TLogBuilder& AddUnescaped(TStringBuf fieldName, TStringBuf fieldValue) {
            if (!Out.Empty()) {
                Out << '\t';
            }
            Out << fieldName << '=' << fieldValue;
            return *this;
        }

        TLogBuilder& Begin(TStringBuf logType, ui32 unixtime) {
            Out << "tskv\ttskv_format=" << logType << "\tunixtime=" << unixtime;
            return *this;
        }

        TLogBuilder& Begin(TStringBuf logType) {
            Out << "tskv\ttskv_format=" << logType;
            return *this;
        }

        TLogBuilder& End() {
            Out << '\n';
            return *this;
        }

        TLogBuilder& Clear() {
            Out.Clear();
            return *this;
        }

        TString& Str() {
            return Out.Str();
        }
    };

}