aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/eventlog/event_field_output.cpp
blob: a1b4c2dafa163dbdd7a45c55ec1a2ca30129939c (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
#include "event_field_output.h"

#include <util/string/split.h>

namespace {
    TString MakeSeparators(EFieldOutputFlags flags) {
        TString res;
        res.reserve(3);

        if (flags & EFieldOutputFlag::EscapeTab) {
            res.append('\t');
        }
        if (flags & EFieldOutputFlag::EscapeNewLine) {
            res.append('\n');
        }
        if (flags & EFieldOutputFlag::EscapeBackSlash) {
            res.append('\\');
        }

        return res;
    }
}

TEventFieldOutput::TEventFieldOutput(IOutputStream& output, EFieldOutputFlags flags)
    : Output(output)
    , Flags(flags)
    , Separators(MakeSeparators(flags))
{
}

IOutputStream& TEventFieldOutput::GetOutputStream() {
    return Output;
}

EFieldOutputFlags TEventFieldOutput::GetFlags() const {
    return Flags;
}

void TEventFieldOutput::DoWrite(const void* buf, size_t len) {
    if (!Flags) {
        Output.Write(buf, len);
        return;
    }

    TStringBuf chunk{static_cast<const char*>(buf), len};

    for (const auto part : StringSplitter(chunk).SplitBySet(Separators.data())) {
        TStringBuf token = part.Token();
        TStringBuf delim = part.Delim();

        if (!token.empty()) {
            Output.Write(token);
        }
        if ("\n" == delim) {
            Output.Write(TStringBuf("\\n"));
        } else if ("\t" == delim) {
            Output.Write(TStringBuf("\\t"));
        } else if ("\\" == delim) {
            Output.Write(TStringBuf("\\\\"));
        } else {
            Y_ASSERT(delim.empty());
        }
    }
}