aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/logger/element.h
blob: 9ba27ab67e83a47c74ddb80f200a6d96da707519 (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
#pragma once

#include "priority.h"
#include "record.h"


#include <util/string/cast.h>
#include <util/stream/tempbuf.h>


class TLog;

/// @warning Better don't use directly.
class TLogElement: public TTempBufOutput {
public:
    explicit TLogElement(const TLog* parent);
    explicit TLogElement(const TLog* parent, ELogPriority priority);

    TLogElement(TLogElement&&) noexcept = default;
    TLogElement& operator=(TLogElement&&) noexcept = default;

    ~TLogElement() override;

    template <class T>
    inline TLogElement& operator<<(const T& t) {
        static_cast<IOutputStream&>(*this) << t;

        return *this;
    }

    /// @note For pretty usage: logger << TLOG_ERROR << "Error description";
    inline TLogElement& operator<<(ELogPriority priority) {
        Flush();
        Priority_ = priority;
        return *this;
    }

    template<typename T>
    TLogElement& With(const TStringBuf key, const T value) {
        Context_.emplace_back(key, ToString(value));

        return *this;
    }

    ELogPriority Priority() const noexcept {
        return Priority_;
    }

protected:
    void DoFlush() override;

protected:
    const TLog* Parent_ = nullptr;
    ELogPriority Priority_;
    TLogRecord::TMetaFlags Context_;
};