Coverage Report

Created: 2025-07-23 18:49

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/root/doris/be/src/common/logging.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <memory>
21
22
// GLOG defines this based on the system but doesn't check if it's already
23
// been defined.  undef it first to avoid warnings.
24
// glog MUST be included before gflags.  Instead of including them,
25
// our files should include this file instead.
26
#undef _XOPEN_SOURCE
27
// This is including a glog internal file.  We want this to expose the
28
// function to get the stack trace.
29
#include <glog/logging.h> // IWYU pragma: export
30
#undef MutexLock
31
32
// Define VLOG levels.  We want display per-row info less than per-file which
33
// is less than per-query.  For now per-connection is the same as per-query.
34
#define VLOG_CONNECTION VLOG(1)
35
#define VLOG_RPC VLOG(8)
36
#define VLOG_QUERY VLOG(1)
37
#define VLOG_FILE VLOG(2)
38
#define VLOG_ROW VLOG(10)
39
#define VLOG_PROGRESS VLOG(2)
40
#define VLOG_TRACE VLOG(10)
41
18.4E
#define VLOG_DEBUG VLOG(7)
42
0
#define VLOG_NOTICE VLOG(3)
43
0
#define VLOG_CRITICAL VLOG(1)
44
45
#define VLOG_CONNECTION_IS_ON VLOG_IS_ON(1)
46
#define VLOG_RPC_IS_ON VLOG_IS_ON(8)
47
#define VLOG_QUERY_IS_ON VLOG_IS_ON(1)
48
#define VLOG_FILE_IS_ON VLOG_IS_ON(2)
49
#define VLOG_ROW_IS_ON VLOG_IS_ON(10)
50
#define VLOG_TRACE_IS_ON VLOG_IS_ON(10)
51
#define VLOG_DEBUG_IS_ON VLOG_IS_ON(7)
52
#define VLOG_NOTICE_IS_ON VLOG_IS_ON(3)
53
#define VLOG_CRITICAL_IS_ON VLOG_IS_ON(1)
54
55
/// Define a wrapper around DCHECK for strongly typed enums that print a useful error
56
/// message on failure.
57
#define DCHECK_ENUM_EQ(a, b)                                                 \
58
    DCHECK(a == b) << "[ " #a " = " << static_cast<int>(a) << " , " #b " = " \
59
                   << static_cast<int>(b) << " ]"
60
61
#include <fmt/format.h>
62
63
#include "util/uid_util.h"
64
65
namespace doris {
66
67
// glog doesn't allow multiple invocations of InitGoogleLogging. This method conditionally
68
// calls InitGoogleLogging only if it hasn't been called before.
69
bool init_glog(const char* basename);
70
71
// Shuts down the google logging library. Call before exit to ensure that log files are
72
// flushed. May only be called once.
73
void shutdown_logging();
74
75
void update_logging(const std::string& name, const std::string& value);
76
77
class TaggableLogger {
78
public:
79
    TaggableLogger(const char* file, int line, google::LogSeverity severity)
80
344k
            : _msg(file, line, severity) {}
81
82
    template <typename... Args>
83
91
    TaggableLogger& operator()(const std::string_view& fmt, Args&&... args) {
84
91
        if constexpr (sizeof...(args) == 0) {
85
77
            _msg.stream() << fmt;
86
77
        } else {
87
14
            _msg.stream() << fmt::format(fmt, std::forward<Args&&>(args)...);
88
14
        }
89
91
        return *this;
90
91
    }
_ZN5doris14TaggableLoggerclIJEEERS0_RKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
83
77
    TaggableLogger& operator()(const std::string_view& fmt, Args&&... args) {
84
77
        if constexpr (sizeof...(args) == 0) {
85
77
            _msg.stream() << fmt;
86
        } else {
87
            _msg.stream() << fmt::format(fmt, std::forward<Args&&>(args)...);
88
        }
89
77
        return *this;
90
77
    }
Unexecuted instantiation: _ZN5doris14TaggableLoggerclIJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEES7_S7_S7_EEERS0_RKSt17basic_string_viewIcS5_EDpOT_
_ZN5doris14TaggableLoggerclIJxEEERS0_RKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
83
11
    TaggableLogger& operator()(const std::string_view& fmt, Args&&... args) {
84
        if constexpr (sizeof...(args) == 0) {
85
            _msg.stream() << fmt;
86
11
        } else {
87
11
            _msg.stream() << fmt::format(fmt, std::forward<Args&&>(args)...);
88
11
        }
89
11
        return *this;
90
11
    }
_ZN5doris14TaggableLoggerclIJNSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEEERS0_RKSt17basic_string_viewIcS5_EDpOT_
Line
Count
Source
83
2
    TaggableLogger& operator()(const std::string_view& fmt, Args&&... args) {
84
        if constexpr (sizeof...(args) == 0) {
85
            _msg.stream() << fmt;
86
2
        } else {
87
2
            _msg.stream() << fmt::format(fmt, std::forward<Args&&>(args)...);
88
2
        }
89
2
        return *this;
90
2
    }
_ZN5doris14TaggableLoggerclIJRmEEERS0_RKSt17basic_string_viewIcSt11char_traitsIcEEDpOT_
Line
Count
Source
83
1
    TaggableLogger& operator()(const std::string_view& fmt, Args&&... args) {
84
        if constexpr (sizeof...(args) == 0) {
85
            _msg.stream() << fmt;
86
1
        } else {
87
1
            _msg.stream() << fmt::format(fmt, std::forward<Args&&>(args)...);
88
1
        }
89
1
        return *this;
90
1
    }
91
92
    template <typename V>
93
46
    TaggableLogger& tag(std::string_view key, V&& value) {
94
46
        _msg.stream() << '|' << key << '=';
95
        if constexpr (std::is_same_v<V, TUniqueId> || std::is_same_v<V, PUniqueId>) {
96
            _msg.stream() << print_id(value);
97
46
        } else {
98
46
            _msg.stream() << value;
99
46
        }
100
46
        return *this;
101
46
    }
_ZN5doris14TaggableLogger3tagIRlEERS0_St17basic_string_viewIcSt11char_traitsIcEEOT_
Line
Count
Source
93
21
    TaggableLogger& tag(std::string_view key, V&& value) {
94
21
        _msg.stream() << '|' << key << '=';
95
        if constexpr (std::is_same_v<V, TUniqueId> || std::is_same_v<V, PUniqueId>) {
96
            _msg.stream() << print_id(value);
97
21
        } else {
98
21
            _msg.stream() << value;
99
21
        }
100
21
        return *this;
101
21
    }
_ZN5doris14TaggableLogger3tagINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEEEERS0_St17basic_string_viewIcS5_EOT_
Line
Count
Source
93
25
    TaggableLogger& tag(std::string_view key, V&& value) {
94
25
        _msg.stream() << '|' << key << '=';
95
        if constexpr (std::is_same_v<V, TUniqueId> || std::is_same_v<V, PUniqueId>) {
96
            _msg.stream() << print_id(value);
97
25
        } else {
98
25
            _msg.stream() << value;
99
25
        }
100
25
        return *this;
101
25
    }
102
103
    template <typename E>
104
    TaggableLogger& error(E&& error) {
105
        _msg.stream() << "|error=" << error;
106
        return *this;
107
    }
108
109
private:
110
    google::LogMessage _msg;
111
};
112
113
// Very very important!!!!
114
// Never define LOG_DEBUG or LOG_TRACE. because the tagged logging method will
115
// always generated string and then check the log level, its performane is bad.
116
// glog's original method will first check log level if it is not satisfied,
117
// the log message is not generated.
118
34
#define LOG_INFO TaggableLogger(__FILE__, __LINE__, google::GLOG_INFO)
119
2
#define LOG_WARNING TaggableLogger(__FILE__, __LINE__, google::GLOG_WARNING)
120
#define LOG_ERROR TaggableLogger(__FILE__, __LINE__, google::GLOG_ERROR)
121
#define LOG_FATAL TaggableLogger(__FILE__, __LINE__, google::GLOG_FATAL)
122
123
// Avoid the printed log message is truncated by the glog max log size limit
124
#define LOG_LONG_STRING(severity, long_log_str)                                \
125
    do {                                                                       \
126
        constexpr size_t max_log_size = 30000 - 100;                           \
127
        size_t pos = 0;                                                        \
128
        size_t total_size = long_log_str.size();                               \
129
        size_t tmp_size = std::min(max_log_size, total_size);                  \
130
        while (pos < total_size) {                                             \
131
            tmp_size = std::min(max_log_size, total_size - pos);               \
132
            LOG(severity) << std::string(long_log_str.data() + pos, tmp_size); \
133
            pos += tmp_size;                                                   \
134
        }                                                                      \
135
    } while (0)
136
137
} // namespace doris