aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/logger/global/common.h
blob: 0f5b9893c668bf4b0a296aabcbd4bc5b18cd3b8b (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
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#pragma once

#include <util/datetime/base.h>

#include <util/folder/path.h>
#include <util/generic/singleton.h>
#include <util/generic/string.h>
#include <util/generic/ptr.h>
#include <util/generic/yexception.h>
#include <util/string/printf.h>
#include <util/system/src_location.h>

#include <library/cpp/logger/log.h>

namespace NLoggingImpl {
    const size_t SingletonPriority = 500;
}

template <class T>
T* CreateDefaultLogger() {
    return nullptr;
}

namespace NLoggingImpl {
    template<class T, class TTraits>
    class TOperatorBase {
        struct TPtr {
            TPtr()
                : Instance(TTraits::CreateDefault())
            {
            }

            THolder<T> Instance;
        };

    public:
        inline static bool Usage() {
            return SingletonWithPriority<TPtr, SingletonPriority>()->Instance.Get();
        } 

        inline static T* Get() {
            return SingletonWithPriority<TPtr, SingletonPriority>()->Instance.Get();
        }

        inline static void Set(T* v) {
            SingletonWithPriority<TPtr, SingletonPriority>()->Instance.Reset(v);
        }
    };

    template<class T>
    struct TLoggerTraits {
        static T* CreateDefault() {
            return CreateDefaultLogger<T>();
        }
    };
}

template <class T>
class TLoggerOperator : public NLoggingImpl::TOperatorBase<T, NLoggingImpl::TLoggerTraits<T>>  {
public:
    inline static TLog& Log() {
        Y_ASSERT(TLoggerOperator::Usage());
        return *TLoggerOperator::Get();
    }
};

namespace NLoggingImpl {

    TString GetLocalTimeSSimple();

    // Returns correct log type to use
    TString PrepareToOpenLog(TString logType, int logLevel, bool rotation, bool startAsDaemon);

    template <class TLoggerType>
    void InitLogImpl(TString logType, const int logLevel, const bool rotation, const bool startAsDaemon) {
        TLoggerOperator<TLoggerType>::Set(new TLoggerType(PrepareToOpenLog(logType, logLevel, rotation, startAsDaemon), (ELogPriority)logLevel));
    }
}

struct TLogRecordContext {
    constexpr TLogRecordContext(const TSourceLocation& sourceLocation, TStringBuf customMessage, ELogPriority priority)
        : SourceLocation(sourceLocation)
        , CustomMessage(customMessage)
        , Priority(priority)
    {}

    TSourceLocation SourceLocation;
    TStringBuf CustomMessage;
    ELogPriority Priority;
};

template <class... R>
struct TLogRecordPreprocessor;

template <>
struct TLogRecordPreprocessor<> {
    inline static bool CheckLoggingContext(TLog& /*log*/, const TLogRecordContext& /*context*/) {
        return true;
    }

    inline static TSimpleSharedPtr<TLogElement> StartRecord(TLog& log, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) {
        if (earlier)
            return earlier;
        TSimpleSharedPtr<TLogElement> result(new TLogElement(&log));
        *result << context.Priority;
        return result;
    }
};

template <class H, class... R>
struct TLogRecordPreprocessor<H, R...> {
    inline static bool CheckLoggingContext(TLog& log, const TLogRecordContext& context) {
        return H::CheckLoggingContext(log, context) && TLogRecordPreprocessor<R...>::CheckLoggingContext(log, context);
    }

    inline static TSimpleSharedPtr<TLogElement> StartRecord(TLog& log, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier) {
        TSimpleSharedPtr<TLogElement> first = H::StartRecord(log, context, earlier);
        return TLogRecordPreprocessor<R...>::StartRecord(log, context, first);
    }
};

struct TLogFilter {
    static bool CheckLoggingContext(TLog& log, const TLogRecordContext& context);
    static TSimpleSharedPtr<TLogElement> StartRecord(TLog& log, const TLogRecordContext& context, TSimpleSharedPtr<TLogElement> earlier);
};

class TNullLog;

template <class TPreprocessor>
TSimpleSharedPtr<TLogElement> GetLoggerForce(TLog& log, const TLogRecordContext& context) {
    if (TSimpleSharedPtr<TLogElement> result = TPreprocessor::StartRecord(log, context, nullptr))
        return result;
    return new TLogElement(&TLoggerOperator<TNullLog>::Log());
}

namespace NPrivateGlobalLogger {
    struct TEatStream {
        Y_FORCE_INLINE bool operator|(const IOutputStream&) const {
            return true;
        }
    };
} 

#define LOGGER_GENERIC_LOG_CHECKED(logger, preprocessor, level, message) (*GetLoggerForce<preprocessor>(logger, TLogRecordContext(__LOCATION__, message, level)))
#define LOGGER_CHECKED_GENERIC_LOG(logger, preprocessor, level, message) \ 
    (preprocessor::CheckLoggingContext(logger, TLogRecordContext(__LOCATION__, message, level))) && NPrivateGlobalLogger::TEatStream() | (*(preprocessor::StartRecord(logger, TLogRecordContext(__LOCATION__, message, level), nullptr))) 

#define SINGLETON_GENERIC_LOG_CHECKED(type, preprocessor, level, message) LOGGER_GENERIC_LOG_CHECKED(TLoggerOperator<type>::Log(), preprocessor, level, message)
#define SINGLETON_CHECKED_GENERIC_LOG(type, preprocessor, level, message) LOGGER_CHECKED_GENERIC_LOG(TLoggerOperator<type>::Log(), preprocessor, level, message)