/root/doris/be/src/common/logconfig.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | #include <ctype.h> |
19 | | #include <stdint.h> |
20 | | |
21 | | #include <cerrno> |
22 | | #include <cstdlib> |
23 | | #include <cstring> |
24 | | #include <iomanip> |
25 | | #include <iostream> |
26 | | #include <mutex> |
27 | | #include <string> |
28 | | #include <vector> |
29 | | |
30 | | #include "common/config.h" |
31 | | #include "common/logging.h" |
32 | | |
33 | | namespace doris { |
34 | | |
35 | | static bool logging_initialized = false; |
36 | | |
37 | | static std::mutex logging_mutex; |
38 | | |
39 | 3 | static bool iequals(const std::string& a, const std::string& b) { |
40 | 3 | unsigned int sz = a.size(); |
41 | 3 | if (b.size() != sz) { |
42 | 2 | return false; |
43 | 2 | } |
44 | 5 | for (unsigned int i = 0; i < sz; ++i) { |
45 | 4 | if (tolower(a[i]) != tolower(b[i])) { |
46 | 0 | return false; |
47 | 0 | } |
48 | 4 | } |
49 | 1 | return true; |
50 | 1 | } |
51 | | |
52 | | // if custom_date_time_format = false, same format as in be.log |
53 | | // The following is same as default log format. eg: |
54 | | // I20240605 15:25:15.677153 1763151 wal_manager.cpp:481] msg... |
55 | | template <bool add_runtime_logger_prefix = false, bool custom_date_time_format = false> |
56 | 0 | void custom_prefix(std::ostream& s, const google::LogMessageInfo& l, void* arg) { |
57 | 0 | if constexpr (add_runtime_logger_prefix) { |
58 | | // Add prefix "RuntimeLogger ". |
59 | 0 | s << "RuntimeLogger "; |
60 | 0 | } |
61 | 0 | s << l.severity[0]; |
62 | | |
63 | | // Add a space if custom_date_time_format. |
64 | 0 | if constexpr (custom_date_time_format) { |
65 | 0 | s << ' '; |
66 | 0 | } |
67 | |
|
68 | 0 | std::tm tm_time = {}; |
69 | 0 | tm_time.tm_year = l.time.year(); |
70 | 0 | tm_time.tm_mon = l.time.month(); |
71 | 0 | tm_time.tm_mday = l.time.day(); |
72 | 0 | tm_time.tm_hour = l.time.hour(); |
73 | 0 | tm_time.tm_min = l.time.min(); |
74 | 0 | tm_time.tm_sec = l.time.sec(); |
75 | |
|
76 | 0 | if constexpr (custom_date_time_format) { |
77 | 0 | s << std::put_time(&tm_time, config::sys_log_custom_date_time_format.c_str()); |
78 | 0 | if (!config::sys_log_custom_date_time_ms_format.empty()) { |
79 | 0 | s << fmt::format(config::sys_log_custom_date_time_ms_format, l.time.usec() / 1000); |
80 | 0 | } |
81 | 0 | } else { |
82 | 0 | s << std::put_time(&tm_time, "%Y%m%d %H:%M:%S"); |
83 | 0 | s << "." << std::setw(6) << l.time.usec(); |
84 | 0 | } |
85 | |
|
86 | 0 | s << ' '; |
87 | 0 | s << std::setfill(' ') << std::setw(5); |
88 | 0 | s << l.thread_id << std::setfill('0'); |
89 | 0 | s << ' '; |
90 | 0 | s << l.filename << ':' << l.line_number << "]"; |
91 | 0 | } Unexecuted instantiation: _ZN5doris13custom_prefixILb1ELb1EEEvRSoRKN6google14LogMessageInfoEPv Unexecuted instantiation: _ZN5doris13custom_prefixILb1ELb0EEEvRSoRKN6google14LogMessageInfoEPv Unexecuted instantiation: _ZN5doris13custom_prefixILb0ELb1EEEvRSoRKN6google14LogMessageInfoEPv |
92 | | |
93 | 1 | bool init_glog(const char* basename) { |
94 | 1 | std::lock_guard<std::mutex> logging_lock(logging_mutex); |
95 | | |
96 | 1 | if (logging_initialized) { |
97 | 0 | return true; |
98 | 0 | } |
99 | | |
100 | 1 | bool log_to_console = (getenv("DORIS_LOG_TO_STDERR") != nullptr); |
101 | 1 | if (log_to_console) { |
102 | 0 | if (config::enable_file_logger) { |
103 | 0 | FLAGS_alsologtostderr = true; |
104 | 0 | } else { |
105 | 0 | FLAGS_logtostderr = true; |
106 | 0 | } |
107 | 0 | } |
108 | | |
109 | | // don't log to stderr except fatal level |
110 | | // so fatal log can output to be.out . |
111 | 1 | FLAGS_stderrthreshold = google::FATAL; |
112 | | // set glog log dir |
113 | | // ATTN: sys_log_dir is deprecated, this is just for compatibility |
114 | 1 | std::string log_dir = config::sys_log_dir; |
115 | 1 | if (log_dir == "") { |
116 | 1 | log_dir = getenv("LOG_DIR"); |
117 | 1 | } |
118 | 1 | FLAGS_log_dir = log_dir; |
119 | | // 0 means buffer INFO only |
120 | 1 | FLAGS_logbuflevel = 0; |
121 | | // buffer log messages for at most this many seconds |
122 | 1 | FLAGS_logbufsecs = 30; |
123 | | // set roll num |
124 | 1 | FLAGS_log_filenum_quota = config::sys_log_roll_num; |
125 | | |
126 | | // set log level |
127 | 1 | std::string& loglevel = config::sys_log_level; |
128 | 1 | if (iequals(loglevel, "INFO")) { |
129 | 1 | FLAGS_minloglevel = 0; |
130 | 1 | } else if (iequals(loglevel, "WARNING")) { |
131 | 0 | FLAGS_minloglevel = 1; |
132 | 0 | } else if (iequals(loglevel, "ERROR")) { |
133 | 0 | FLAGS_minloglevel = 2; |
134 | 0 | } else if (iequals(loglevel, "FATAL")) { |
135 | 0 | FLAGS_minloglevel = 3; |
136 | 0 | } else { |
137 | 0 | std::cerr << "sys_log_level needs to be INFO, WARNING, ERROR, FATAL" << std::endl; |
138 | 0 | return false; |
139 | 0 | } |
140 | | |
141 | | // set log buffer level |
142 | | // default is 0 |
143 | 1 | std::string& logbuflevel = config::log_buffer_level; |
144 | 1 | if (iequals(logbuflevel, "-1")) { |
145 | 0 | FLAGS_logbuflevel = -1; |
146 | 1 | } else if (iequals(logbuflevel, "0")) { |
147 | 0 | FLAGS_logbuflevel = 0; |
148 | 0 | } |
149 | | |
150 | | // set log roll mode |
151 | 1 | std::string& rollmode = config::sys_log_roll_mode; |
152 | 1 | std::string sizeflag = "SIZE-MB-"; |
153 | 1 | bool ok = false; |
154 | 1 | if (rollmode.compare("TIME-DAY") == 0) { |
155 | 0 | FLAGS_log_split_method = "day"; |
156 | 0 | ok = true; |
157 | 1 | } else if (rollmode.compare("TIME-HOUR") == 0) { |
158 | 0 | FLAGS_log_split_method = "hour"; |
159 | 0 | ok = true; |
160 | 1 | } else if (rollmode.substr(0, sizeflag.length()).compare(sizeflag) == 0) { |
161 | 1 | FLAGS_log_split_method = "size"; |
162 | 1 | std::string sizestr = rollmode.substr(sizeflag.size(), rollmode.size() - sizeflag.size()); |
163 | 1 | if (sizestr.size() != 0) { |
164 | 1 | char* end = nullptr; |
165 | 1 | errno = 0; |
166 | 1 | const char* sizecstr = sizestr.c_str(); |
167 | 1 | int64_t ret64 = strtoll(sizecstr, &end, 10); |
168 | 1 | if ((errno == 0) && (end == sizecstr + strlen(sizecstr))) { |
169 | 1 | int32_t retval = static_cast<int32_t>(ret64); |
170 | 1 | if (retval == ret64) { |
171 | 1 | FLAGS_max_log_size = retval; |
172 | 1 | ok = true; |
173 | 1 | } |
174 | 1 | } |
175 | 1 | } |
176 | 1 | } else { |
177 | 0 | ok = false; |
178 | 0 | } |
179 | 1 | if (!ok) { |
180 | 0 | std::cerr << "sys_log_roll_mode needs to be TIME-DAY, TIME-HOUR, SIZE-MB-nnn" << std::endl; |
181 | 0 | return false; |
182 | 0 | } |
183 | | |
184 | | // set verbose modules. |
185 | 1 | FLAGS_v = config::sys_log_verbose_flags_v; |
186 | 1 | std::vector<std::string>& verbose_modules = config::sys_log_verbose_modules; |
187 | 1 | int32_t vlog_level = config::sys_log_verbose_level; |
188 | 1 | for (size_t i = 0; i < verbose_modules.size(); i++) { |
189 | 0 | if (verbose_modules[i].size() != 0) { |
190 | 0 | google::SetVLOGLevel(verbose_modules[i].c_str(), vlog_level); |
191 | 0 | } |
192 | 0 | } |
193 | | |
194 | 1 | if (log_to_console) { |
195 | | // Add prefix if log output to stderr |
196 | 0 | if (config::sys_log_enable_custom_date_time_format) { |
197 | 0 | google::InitGoogleLogging(basename, &custom_prefix<true, true>); |
198 | 0 | } else { |
199 | 0 | google::InitGoogleLogging(basename, &custom_prefix<true, false>); |
200 | 0 | } |
201 | 1 | } else { |
202 | 1 | if (config::sys_log_enable_custom_date_time_format) { |
203 | 0 | google::InitGoogleLogging(basename, &custom_prefix<false, true>); |
204 | 1 | } else { |
205 | 1 | google::InitGoogleLogging(basename); |
206 | 1 | } |
207 | 1 | } |
208 | | |
209 | 1 | logging_initialized = true; |
210 | | |
211 | 1 | return true; |
212 | 1 | } |
213 | | |
214 | 0 | void shutdown_logging() { |
215 | 0 | std::lock_guard<std::mutex> logging_lock(logging_mutex); |
216 | 0 | google::ShutdownGoogleLogging(); |
217 | 0 | } |
218 | | |
219 | 0 | void update_logging(const std::string& name, const std::string& value) { |
220 | 0 | if ("sys_log_level" == name) { |
221 | 0 | if (iequals(value, "INFO")) { |
222 | 0 | FLAGS_minloglevel = 0; |
223 | 0 | } else if (iequals(value, "WARNING")) { |
224 | 0 | FLAGS_minloglevel = 1; |
225 | 0 | } else if (iequals(value, "ERROR")) { |
226 | 0 | FLAGS_minloglevel = 2; |
227 | 0 | } else if (iequals(value, "FATAL")) { |
228 | 0 | FLAGS_minloglevel = 3; |
229 | 0 | } else { |
230 | 0 | LOG(WARNING) << "update sys_log_level failed, need to be INFO, WARNING, ERROR, FATAL"; |
231 | 0 | } |
232 | 0 | } |
233 | 0 | } |
234 | | |
235 | | } // namespace doris |