/src/brpc/src/butil/logging.cc
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 | | // Date: 2012-10-08 23:53:50 |
19 | | |
20 | | #include "butil/logging.h" |
21 | | |
22 | | #include <gflags/gflags.h> |
23 | | DEFINE_bool(log_as_json, false, "Print log as a valid JSON"); |
24 | | DEFINE_bool(escape_log, false, "Escape log content before printing"); |
25 | | |
26 | | #if !BRPC_WITH_GLOG |
27 | | |
28 | | #if defined(OS_WIN) |
29 | | #include <io.h> |
30 | | #include <windows.h> |
31 | | typedef HANDLE FileHandle; |
32 | | typedef HANDLE MutexHandle; |
33 | | // Windows warns on using write(). It prefers _write(). |
34 | | #define write(fd, buf, count) _write(fd, buf, static_cast<unsigned int>(count)) |
35 | | // Windows doesn't define STDERR_FILENO. Define it here. |
36 | | #define STDERR_FILENO 2 |
37 | | #elif defined(OS_MACOSX) |
38 | | #include <mach/mach.h> |
39 | | #include <mach/mach_time.h> |
40 | | #include <mach-o/dyld.h> |
41 | | #elif defined(OS_POSIX) |
42 | | #if defined(OS_NACL) || defined(OS_LINUX) |
43 | | #include <sys/time.h> // timespec doesn't seem to be in <time.h> |
44 | | #else |
45 | | #include <sys/syscall.h> |
46 | | #endif |
47 | | #include <time.h> |
48 | | #endif |
49 | | #if defined(OS_POSIX) |
50 | | #include <errno.h> |
51 | | #include <pthread.h> |
52 | | #include <stdio.h> |
53 | | #include <stdlib.h> |
54 | | #include <string.h> |
55 | | #include <unistd.h> |
56 | | #define MAX_PATH PATH_MAX |
57 | | typedef FILE* FileHandle; |
58 | | typedef pthread_mutex_t* MutexHandle; |
59 | | #endif |
60 | | |
61 | | #include <algorithm> |
62 | | #include <cstring> |
63 | | #include <ctime> |
64 | | #include <iomanip> |
65 | | #include <ostream> |
66 | | #include <string> |
67 | | |
68 | | #include "butil/file_util.h" |
69 | | #include "butil/debug/alias.h" |
70 | | #include "butil/debug/debugger.h" |
71 | | #include "butil/debug/stack_trace.h" |
72 | | #include "butil/posix/eintr_wrapper.h" |
73 | | #include "butil/strings/string_util.h" |
74 | | #include "butil/strings/stringprintf.h" |
75 | | #include "butil/strings/utf_string_conversions.h" |
76 | | #include "butil/synchronization/condition_variable.h" |
77 | | #include "butil/threading/platform_thread.h" |
78 | | #include "butil/threading/simple_thread.h" |
79 | | #include "butil/object_pool.h" |
80 | | |
81 | | #if defined(OS_POSIX) |
82 | | #include "butil/errno.h" |
83 | | #include "butil/fd_guard.h" |
84 | | #endif |
85 | | #if defined(OS_LINUX) |
86 | | #include <fcntl.h> |
87 | | #endif |
88 | | |
89 | | #if defined(OS_ANDROID) |
90 | | #include <android/log.h> |
91 | | #endif |
92 | | |
93 | | #include <map> |
94 | | #include <vector> |
95 | | #include <deque> |
96 | | #include <limits> |
97 | | #include "butil/atomicops.h" |
98 | | #include "butil/thread_local.h" |
99 | | #include "butil/scoped_lock.h" // BAIDU_SCOPED_LOCK |
100 | | #include "butil/string_splitter.h" |
101 | | #include "butil/time.h" |
102 | | #include "butil/containers/doubly_buffered_data.h" |
103 | | #include "butil/memory/singleton.h" |
104 | | #include "butil/endpoint.h" |
105 | | #ifdef BAIDU_INTERNAL |
106 | | #include "butil/comlog_sink.h" |
107 | | #endif |
108 | | |
109 | | extern "C" { |
110 | | uint64_t BAIDU_WEAK bthread_self(); |
111 | | typedef struct { |
112 | | uint32_t index; // index in KeyTable |
113 | | uint32_t version; // ABA avoidance |
114 | | } bthread_key_t; |
115 | | int BAIDU_WEAK bthread_key_create(bthread_key_t* key, |
116 | | void (*destructor)(void* data)); |
117 | | int BAIDU_WEAK bthread_setspecific(bthread_key_t key, void* data); |
118 | | void* BAIDU_WEAK bthread_getspecific(bthread_key_t key); |
119 | | } |
120 | | |
121 | | namespace logging { |
122 | | |
123 | | DEFINE_bool(crash_on_fatal_log, false, |
124 | | "Crash process when a FATAL log is printed"); |
125 | | DEFINE_bool(print_stack_on_check, true, |
126 | | "Print the stack trace when a CHECK was failed"); |
127 | | |
128 | | DEFINE_int32(v, 0, "Show all VLOG(m) messages for m <= this." |
129 | | " Overridable by --vmodule."); |
130 | | DEFINE_string(vmodule, "", "per-module verbose level." |
131 | | " Argument is a comma-separated list of MODULE_NAME=LOG_LEVEL." |
132 | | " MODULE_NAME is a glob pattern, matched against the filename base" |
133 | | " (that is, name ignoring .cpp/.h)." |
134 | | " LOG_LEVEL overrides any value given by --v."); |
135 | | |
136 | | DEFINE_bool(log_pid, false, "Log process id"); |
137 | | |
138 | | DEFINE_int32(minloglevel, 0, "Any log at or above this level will be " |
139 | | "displayed. Anything below this level will be silently ignored. " |
140 | | "0=INFO 1=NOTICE 2=WARNING 3=ERROR 4=FATAL"); |
141 | | |
142 | | DEFINE_bool(log_hostname, false, "Add host after pid in each log so" |
143 | | " that we know where logs came from when using aggregation tools" |
144 | | " like ELK."); |
145 | | |
146 | | DEFINE_bool(log_year, false, "Log year in datetime part in each log"); |
147 | | |
148 | | DEFINE_bool(log_func_name, false, "Log function name in each log"); |
149 | | |
150 | | DEFINE_bool(async_log, false, "Use async log"); |
151 | | |
152 | | DEFINE_bool(async_log_in_background_always, false, "Async log written in background always."); |
153 | | |
154 | | DEFINE_int32(max_async_log_queue_size, 100000, "Max async log size. " |
155 | | "If current log count of async log > max_async_log_size, " |
156 | | "Use sync log to protect process."); |
157 | | |
158 | | DEFINE_int32(sleep_to_flush_async_log_s, 0, |
159 | | "If the value > 0, sleep before atexit to flush async log"); |
160 | | |
161 | | namespace { |
162 | | |
163 | | LoggingDestination logging_destination = LOG_DEFAULT; |
164 | | |
165 | | // For BLOG_ERROR and above, always print to stderr. |
166 | | const int kAlwaysPrintErrorLevel = BLOG_ERROR; |
167 | | |
168 | | // Which log file to use? This is initialized by InitLogging or |
169 | | // will be lazily initialized to the default value when it is |
170 | | // first needed. |
171 | | #if defined(OS_WIN) |
172 | | typedef std::wstring PathString; |
173 | | #else |
174 | | typedef std::string PathString; |
175 | | #endif |
176 | | PathString* log_file_name = NULL; |
177 | | |
178 | | // this file is lazily opened and the handle may be NULL |
179 | | FileHandle log_file = NULL; |
180 | | |
181 | | // Should we pop up fatal debug messages in a dialog? |
182 | | bool show_error_dialogs = false; |
183 | | |
184 | | // An assert handler override specified by the client to be called instead of |
185 | | // the debug message dialog and process termination. |
186 | | LogAssertHandler log_assert_handler = NULL; |
187 | | |
188 | | // Helper functions to wrap platform differences. |
189 | | |
190 | 0 | int32_t CurrentProcessId() { |
191 | | #if defined(OS_WIN) |
192 | | return GetCurrentProcessId(); |
193 | | #elif defined(OS_POSIX) |
194 | 0 | return getpid(); |
195 | 0 | #endif |
196 | 0 | } |
197 | | |
198 | 0 | void DeleteFilePath(const PathString& log_name) { |
199 | | #if defined(OS_WIN) |
200 | | DeleteFile(log_name.c_str()); |
201 | | #elif defined (OS_NACL) |
202 | | // Do nothing; unlink() isn't supported on NaCl. |
203 | | #else |
204 | 0 | unlink(log_name.c_str()); |
205 | 0 | #endif |
206 | 0 | } |
207 | | |
208 | | #if defined(OS_LINUX) |
209 | 0 | static PathString GetProcessName() { |
210 | 0 | butil::fd_guard fd(open("/proc/self/cmdline", O_RDONLY)); |
211 | 0 | if (fd < 0) { |
212 | 0 | return "unknown"; |
213 | 0 | } |
214 | 0 | char buf[512]; |
215 | 0 | const ssize_t len = read(fd, buf, sizeof(buf) - 1); |
216 | 0 | if (len <= 0) { |
217 | 0 | return "unknown"; |
218 | 0 | } |
219 | 0 | buf[len] = '\0'; |
220 | | // Not string(buf, len) because we needs to buf to be truncated at first \0. |
221 | | // Under gdb, the first part of cmdline may include path. |
222 | 0 | return butil::FilePath(std::string(buf)).BaseName().value(); |
223 | 0 | } |
224 | | #endif |
225 | | |
226 | 0 | PathString GetDefaultLogFile() { |
227 | | #if defined(OS_WIN) |
228 | | // On Windows we use the same path as the exe. |
229 | | wchar_t module_name[MAX_PATH]; |
230 | | GetModuleFileName(NULL, module_name, MAX_PATH); |
231 | | |
232 | | PathString log_file = module_name; |
233 | | PathString::size_type last_backslash = |
234 | | log_file.rfind('\\', log_file.size()); |
235 | | if (last_backslash != PathString::npos) |
236 | | log_file.erase(last_backslash + 1); |
237 | | log_file += L"debug.log"; |
238 | | return log_file; |
239 | | #elif defined(OS_LINUX) |
240 | 0 | return GetProcessName() + ".log"; |
241 | | #elif defined(OS_POSIX) |
242 | | // On other platforms we just use the current directory. |
243 | | return PathString("debug.log"); |
244 | | #endif |
245 | 0 | } |
246 | | |
247 | | // This class acts as a wrapper for locking the logging files. |
248 | | // LoggingLock::Init() should be called from the main thread before any logging |
249 | | // is done. Then whenever logging, be sure to have a local LoggingLock |
250 | | // instance on the stack. This will ensure that the lock is unlocked upon |
251 | | // exiting the frame. |
252 | | // LoggingLocks can not be nested. |
253 | | class LoggingLock { |
254 | | public: |
255 | 0 | LoggingLock() { |
256 | 0 | LockLogging(); |
257 | 0 | } |
258 | | |
259 | 0 | ~LoggingLock() { |
260 | 0 | UnlockLogging(); |
261 | 0 | } |
262 | | |
263 | 0 | static void Init(LogLockingState lock_log, const LogChar* new_log_file) { |
264 | 0 | if (initialized) |
265 | 0 | return; |
266 | 0 | lock_log_file = lock_log; |
267 | 0 | if (lock_log_file == LOCK_LOG_FILE) { |
268 | | #if defined(OS_WIN) |
269 | | if (!log_mutex) { |
270 | | std::wstring safe_name; |
271 | | if (new_log_file) |
272 | | safe_name = new_log_file; |
273 | | else |
274 | | safe_name = GetDefaultLogFile(); |
275 | | // \ is not a legal character in mutex names so we replace \ with / |
276 | | std::replace(safe_name.begin(), safe_name.end(), '\\', '/'); |
277 | | std::wstring t(L"Global\\"); |
278 | | t.append(safe_name); |
279 | | log_mutex = ::CreateMutex(NULL, FALSE, t.c_str()); |
280 | | |
281 | | if (log_mutex == NULL) { |
282 | | #if DEBUG |
283 | | // Keep the error code for debugging |
284 | | int error = GetLastError(); // NOLINT |
285 | | butil::debug::BreakDebugger(); |
286 | | #endif |
287 | | // Return nicely without putting initialized to true. |
288 | | return; |
289 | | } |
290 | | } |
291 | | #endif |
292 | 0 | } else { |
293 | 0 | log_lock = new butil::Mutex; |
294 | 0 | } |
295 | 0 | initialized = true; |
296 | 0 | } |
297 | | |
298 | | private: |
299 | 0 | static void LockLogging() { |
300 | 0 | if (lock_log_file == LOCK_LOG_FILE) { |
301 | | #if defined(OS_WIN) |
302 | | ::WaitForSingleObject(log_mutex, INFINITE); |
303 | | // WaitForSingleObject could have returned WAIT_ABANDONED. We don't |
304 | | // abort the process here. UI tests might be crashy sometimes, |
305 | | // and aborting the test binary only makes the problem worse. |
306 | | // We also don't use LOG macros because that might lead to an infinite |
307 | | // loop. For more info see http://crbug.com/18028. |
308 | | #elif defined(OS_POSIX) |
309 | 0 | pthread_mutex_lock(&log_mutex); |
310 | 0 | #endif |
311 | 0 | } else { |
312 | | // use the lock |
313 | 0 | log_lock->lock(); |
314 | 0 | } |
315 | 0 | } |
316 | | |
317 | 0 | static void UnlockLogging() { |
318 | 0 | if (lock_log_file == LOCK_LOG_FILE) { |
319 | | #if defined(OS_WIN) |
320 | | ReleaseMutex(log_mutex); |
321 | | #elif defined(OS_POSIX) |
322 | 0 | pthread_mutex_unlock(&log_mutex); |
323 | 0 | #endif |
324 | 0 | } else { |
325 | 0 | log_lock->unlock(); |
326 | 0 | } |
327 | 0 | } |
328 | | |
329 | | // The lock is used if log file locking is false. It helps us avoid problems |
330 | | // with multiple threads writing to the log file at the same time. |
331 | | static butil::Mutex* log_lock; |
332 | | |
333 | | // When we don't use a lock, we are using a global mutex. We need to do this |
334 | | // because LockFileEx is not thread safe. |
335 | | #if defined(OS_WIN) |
336 | | static MutexHandle log_mutex; |
337 | | #elif defined(OS_POSIX) |
338 | | static pthread_mutex_t log_mutex; |
339 | | #endif |
340 | | |
341 | | static bool initialized; |
342 | | static LogLockingState lock_log_file; |
343 | | }; |
344 | | |
345 | | // static |
346 | | bool LoggingLock::initialized = false; |
347 | | // static |
348 | | butil::Mutex* LoggingLock::log_lock = NULL; |
349 | | // static |
350 | | LogLockingState LoggingLock::lock_log_file = LOCK_LOG_FILE; |
351 | | |
352 | | #if defined(OS_WIN) |
353 | | // static |
354 | | MutexHandle LoggingLock::log_mutex = NULL; |
355 | | #elif defined(OS_POSIX) |
356 | | pthread_mutex_t LoggingLock::log_mutex = PTHREAD_MUTEX_INITIALIZER; |
357 | | #endif |
358 | | |
359 | | // Called by logging functions to ensure that debug_file is initialized |
360 | | // and can be used for writing. Returns false if the file could not be |
361 | | // initialized. debug_file will be NULL in this case. |
362 | 0 | bool InitializeLogFileHandle() { |
363 | 0 | if (log_file) |
364 | 0 | return true; |
365 | | |
366 | 0 | if (!log_file_name) { |
367 | | // Nobody has called InitLogging to specify a debug log file, so here we |
368 | | // initialize the log file name to a default. |
369 | 0 | log_file_name = new PathString(GetDefaultLogFile()); |
370 | 0 | } |
371 | |
|
372 | 0 | if ((logging_destination & LOG_TO_FILE) != 0) { |
373 | | #if defined(OS_WIN) |
374 | | log_file = CreateFile(log_file_name->c_str(), GENERIC_WRITE, |
375 | | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
376 | | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
377 | | if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) { |
378 | | // try the current directory |
379 | | log_file = CreateFile(L".\\debug.log", GENERIC_WRITE, |
380 | | FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, |
381 | | OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); |
382 | | if (log_file == INVALID_HANDLE_VALUE || log_file == NULL) { |
383 | | log_file = NULL; |
384 | | return false; |
385 | | } |
386 | | } |
387 | | SetFilePointer(log_file, 0, 0, FILE_END); |
388 | | #elif defined(OS_POSIX) |
389 | 0 | log_file = fopen(log_file_name->c_str(), "a"); |
390 | 0 | if (log_file == NULL) { |
391 | 0 | fprintf(stderr, "Fail to fopen %s", log_file_name->c_str()); |
392 | 0 | return false; |
393 | 0 | } |
394 | 0 | #endif |
395 | 0 | } |
396 | | |
397 | 0 | return true; |
398 | 0 | } |
399 | | |
400 | 0 | void CloseFile(FileHandle log) { |
401 | | #if defined(OS_WIN) |
402 | | CloseHandle(log); |
403 | | #else |
404 | 0 | fclose(log); |
405 | 0 | #endif |
406 | 0 | } |
407 | | |
408 | 0 | void CloseLogFileUnlocked() { |
409 | 0 | if (!log_file) |
410 | 0 | return; |
411 | | |
412 | 0 | CloseFile(log_file); |
413 | 0 | log_file = NULL; |
414 | 0 | } |
415 | | |
416 | 0 | void Log2File(const std::string& log) { |
417 | | // We can have multiple threads and/or processes, so try to prevent them |
418 | | // from clobbering each other's writes. |
419 | | // If the client app did not call InitLogging, and the lock has not |
420 | | // been created do it now. We do this on demand, but if two threads try |
421 | | // to do this at the same time, there will be a race condition to create |
422 | | // the lock. This is why InitLogging should be called from the main |
423 | | // thread at the beginning of execution. |
424 | 0 | LoggingLock::Init(LOCK_LOG_FILE, NULL); |
425 | 0 | LoggingLock logging_lock; |
426 | 0 | if (InitializeLogFileHandle()) { |
427 | | #if defined(OS_WIN) |
428 | | SetFilePointer(log_file, 0, 0, SEEK_END); |
429 | | DWORD num_written; |
430 | | WriteFile(log_file, |
431 | | static_cast<const void*>(log.data()), |
432 | | static_cast<DWORD>(log.size()), |
433 | | &num_written, |
434 | | NULL); |
435 | | #else |
436 | 0 | fwrite(log.data(), log.size(), 1, log_file); |
437 | 0 | fflush(log_file); |
438 | 0 | #endif |
439 | 0 | } |
440 | 0 | } |
441 | | |
442 | | } // namespace |
443 | | |
444 | | struct BAIDU_CACHELINE_ALIGNMENT LogRequest { |
445 | | static LogRequest* const UNCONNECTED; |
446 | | |
447 | | LogRequest* next{NULL}; |
448 | | std::string data; |
449 | | }; |
450 | | |
451 | | LogRequest* const LogRequest::UNCONNECTED = (LogRequest*)(intptr_t)-1; |
452 | | |
453 | | class AsyncLogger : public butil::SimpleThread { |
454 | | public: |
455 | | static AsyncLogger* GetInstance(); |
456 | | |
457 | | void Log(const std::string& log); |
458 | | void Log(std::string&& log); |
459 | | void StopAndJoin(); |
460 | | |
461 | | private: |
462 | | friend struct DefaultSingletonTraits<AsyncLogger>; |
463 | | |
464 | | static LogRequest _stop_req; |
465 | | |
466 | | AsyncLogger(); |
467 | | ~AsyncLogger() override; |
468 | | |
469 | 0 | static void AtExit() { |
470 | 0 | GetInstance()->StopAndJoin(); |
471 | 0 | if (FLAGS_sleep_to_flush_async_log_s > 0) { |
472 | 0 | ::sleep(FLAGS_sleep_to_flush_async_log_s); |
473 | 0 | } |
474 | 0 | } |
475 | | |
476 | | void LogImpl(LogRequest* log_req); |
477 | | |
478 | | void Run() override; |
479 | | |
480 | | void LogTask(LogRequest* req); |
481 | | |
482 | | bool IsLogComplete(LogRequest* old_head); |
483 | | |
484 | | void DoLog(LogRequest* req); |
485 | | void DoLog(const std::string& log); |
486 | | |
487 | | butil::atomic<LogRequest*> _log_head; |
488 | | butil::Mutex _mutex; |
489 | | butil::ConditionVariable _cond; |
490 | | LogRequest* _current_log_request; |
491 | | butil::atomic<int32_t> _log_request_count; |
492 | | butil::atomic<bool> _stop; |
493 | | }; |
494 | | |
495 | 0 | AsyncLogger* AsyncLogger::GetInstance() { |
496 | 0 | return Singleton<AsyncLogger, |
497 | 0 | LeakySingletonTraits<AsyncLogger>>::get(); |
498 | 0 | } |
499 | | |
500 | | AsyncLogger::AsyncLogger() |
501 | | : butil::SimpleThread("async_log_thread") |
502 | | , _log_head(NULL) |
503 | | , _cond(&_mutex) |
504 | | , _current_log_request(NULL) |
505 | 0 | , _stop(false) { |
506 | 0 | Start(); |
507 | | // We need to stop async logger and |
508 | | // flush all async log before exit. |
509 | 0 | atexit(AtExit); |
510 | 0 | } |
511 | | |
512 | 0 | AsyncLogger::~AsyncLogger() { |
513 | 0 | StopAndJoin(); |
514 | 0 | } |
515 | | |
516 | 0 | void AsyncLogger::Log(const std::string& log) { |
517 | 0 | if (log.empty()) { |
518 | 0 | return; |
519 | 0 | } |
520 | | |
521 | 0 | bool is_full = FLAGS_max_async_log_queue_size > 0 && |
522 | 0 | _log_request_count.fetch_add(1, butil::memory_order_relaxed) > |
523 | 0 | FLAGS_max_async_log_queue_size; |
524 | 0 | if (is_full || _stop.load(butil::memory_order_relaxed)) { |
525 | | // Async logger is full or stopped, fallback to sync log. |
526 | 0 | DoLog(log); |
527 | 0 | return; |
528 | 0 | } |
529 | | |
530 | 0 | auto log_req = butil::get_object<LogRequest>(); |
531 | 0 | if (!log_req) { |
532 | | // Async log failed, fallback to sync log. |
533 | 0 | DoLog(log); |
534 | 0 | return; |
535 | 0 | } |
536 | 0 | log_req->data = log; |
537 | 0 | LogImpl(log_req); |
538 | 0 | } |
539 | | |
540 | 0 | void AsyncLogger::Log(std::string&& log) { |
541 | 0 | if (log.empty()) { |
542 | 0 | return; |
543 | 0 | } |
544 | | |
545 | 0 | bool is_full = FLAGS_max_async_log_queue_size > 0 && |
546 | 0 | _log_request_count.fetch_add(1, butil::memory_order_relaxed) > |
547 | 0 | FLAGS_max_async_log_queue_size; |
548 | 0 | if (is_full || _stop.load(butil::memory_order_relaxed)) { |
549 | | // Async logger is full or stopped, fallback to sync log. |
550 | 0 | DoLog(log); |
551 | 0 | return; |
552 | 0 | } |
553 | | |
554 | 0 | auto log_req = butil::get_object<LogRequest>(); |
555 | 0 | if (!log_req) { |
556 | | // Async log failed, fallback to sync log. |
557 | 0 | DoLog(log); |
558 | 0 | return; |
559 | 0 | } |
560 | 0 | log_req->data = std::move(log); |
561 | 0 | LogImpl(log_req); |
562 | 0 | } |
563 | | |
564 | 0 | void AsyncLogger::LogImpl(LogRequest* log_req) { |
565 | 0 | log_req->next = LogRequest::UNCONNECTED; |
566 | | // Release fence makes sure the thread getting request sees *req |
567 | 0 | LogRequest* const prev_head = |
568 | 0 | _log_head.exchange(log_req, butil::memory_order_release); |
569 | 0 | if (prev_head != NULL) { |
570 | | // Someone is logging. The async_log_thread thread may spin |
571 | | // until req->next to be non-UNCONNECTED. This process is not |
572 | | // lock-free, but the duration is so short(1~2 instructions, |
573 | | // depending on compiler) that the spin rarely occurs in practice |
574 | | // (I've not seen any spin in highly contended tests). |
575 | 0 | log_req->next = prev_head; |
576 | 0 | return; |
577 | 0 | } |
578 | | // We've got the right to write. |
579 | 0 | log_req->next = NULL; |
580 | |
|
581 | 0 | if (!FLAGS_async_log_in_background_always) { |
582 | | // Use sync log for the LogRequest |
583 | | // which has got the right to write. |
584 | 0 | DoLog(log_req); |
585 | | // Return when there's no more LogRequests. |
586 | 0 | if (IsLogComplete(log_req)) { |
587 | 0 | butil::return_object(log_req); |
588 | 0 | return; |
589 | 0 | } |
590 | 0 | } |
591 | | |
592 | 0 | BAIDU_SCOPED_LOCK(_mutex); |
593 | 0 | if (_stop.load(butil::memory_order_relaxed)) { |
594 | | // Async logger is stopped, fallback to sync log. |
595 | 0 | LogTask(log_req); |
596 | 0 | } else { |
597 | | // Wake up async logger. |
598 | 0 | _current_log_request = log_req; |
599 | 0 | _cond.Signal(); |
600 | 0 | } |
601 | 0 | } |
602 | | |
603 | 0 | void AsyncLogger::StopAndJoin() { |
604 | 0 | if (!_stop.exchange(true, butil::memory_order_relaxed)) { |
605 | 0 | BAIDU_SCOPED_LOCK(_mutex); |
606 | 0 | _cond.Signal(); |
607 | 0 | } |
608 | 0 | if (!HasBeenJoined()) { |
609 | 0 | Join(); |
610 | 0 | } |
611 | 0 | } |
612 | | |
613 | 0 | void AsyncLogger::Run() { |
614 | 0 | while (true) { |
615 | 0 | BAIDU_SCOPED_LOCK(_mutex); |
616 | 0 | while (!_stop.load(butil::memory_order_relaxed) && |
617 | 0 | !_current_log_request) { |
618 | 0 | _cond.Wait(); |
619 | 0 | } |
620 | 0 | if (_stop.load(butil::memory_order_relaxed) && |
621 | 0 | !_current_log_request) { |
622 | 0 | break; |
623 | 0 | } |
624 | | |
625 | 0 | LogTask(_current_log_request); |
626 | 0 | _current_log_request = NULL; |
627 | 0 | } |
628 | 0 | } |
629 | | |
630 | 0 | void AsyncLogger::LogTask(LogRequest* req) { |
631 | 0 | do { |
632 | | // req was logged, skip it. |
633 | 0 | if (req->next != NULL && req->data.empty()) { |
634 | 0 | LogRequest* const saved_req = req; |
635 | 0 | req = req->next; |
636 | 0 | butil::return_object(saved_req); |
637 | 0 | } |
638 | | |
639 | | // Log all requests to file. |
640 | 0 | while (req->next != NULL) { |
641 | 0 | LogRequest* const saved_req = req; |
642 | 0 | req = req->next; |
643 | 0 | if (!saved_req->data.empty()) { |
644 | 0 | DoLog(saved_req); |
645 | 0 | } |
646 | | // Release LogRequests until last request. |
647 | 0 | butil::return_object(saved_req); |
648 | 0 | } |
649 | 0 | if (!req->data.empty()) { |
650 | 0 | DoLog(req); |
651 | 0 | } |
652 | | |
653 | | // Return when there's no more LogRequests. |
654 | 0 | if (IsLogComplete(req)) { |
655 | 0 | butil::return_object(req); |
656 | 0 | return; |
657 | 0 | } |
658 | 0 | } while (true); |
659 | 0 | } |
660 | | |
661 | 0 | bool AsyncLogger::IsLogComplete(LogRequest* old_head) { |
662 | 0 | if (old_head->next) { |
663 | 0 | fprintf(stderr, "old_head->next should be NULL\n"); |
664 | 0 | } |
665 | 0 | LogRequest* new_head = old_head; |
666 | 0 | LogRequest* desired = NULL; |
667 | 0 | if (_log_head.compare_exchange_strong( |
668 | 0 | new_head, desired, butil::memory_order_acquire)) { |
669 | | // No one added new requests. |
670 | 0 | return true; |
671 | 0 | } |
672 | 0 | if (new_head == old_head) { |
673 | 0 | fprintf(stderr, "new_head should not be equal to old_head\n"); |
674 | 0 | } |
675 | | // Above acquire fence pairs release fence of exchange in Log() to make |
676 | | // sure that we see all fields of requests set. |
677 | | |
678 | | // Someone added new requests. |
679 | | // Reverse the list until old_head. |
680 | 0 | LogRequest* tail = NULL; |
681 | 0 | LogRequest* p = new_head; |
682 | 0 | do { |
683 | 0 | while (p->next == LogRequest::UNCONNECTED) { |
684 | 0 | sched_yield(); |
685 | 0 | } |
686 | 0 | LogRequest* const saved_next = p->next; |
687 | 0 | p->next = tail; |
688 | 0 | tail = p; |
689 | 0 | p = saved_next; |
690 | 0 | if (!p) { |
691 | 0 | fprintf(stderr, "p should not be NULL\n"); |
692 | 0 | } |
693 | 0 | } while (p != old_head); |
694 | | |
695 | | // Link old list with new list. |
696 | 0 | old_head->next = tail; |
697 | 0 | return false; |
698 | 0 | } |
699 | | |
700 | 0 | void AsyncLogger::DoLog(LogRequest* req) { |
701 | 0 | DoLog(req->data); |
702 | 0 | req->data.clear(); |
703 | 0 | } |
704 | | |
705 | 0 | void AsyncLogger::DoLog(const std::string& log) { |
706 | 0 | Log2File(log); |
707 | 0 | _log_request_count.fetch_sub(1); |
708 | 0 | } |
709 | | |
710 | | LoggingSettings::LoggingSettings() |
711 | | : logging_dest(LOG_DEFAULT), |
712 | | log_file(NULL), |
713 | | lock_log(LOCK_LOG_FILE), |
714 | 0 | delete_old(APPEND_TO_OLD_LOG_FILE) {} |
715 | | |
716 | 0 | bool BaseInitLoggingImpl(const LoggingSettings& settings) { |
717 | | #if defined(OS_NACL) |
718 | | // Can log only to the system debug log. |
719 | | CHECK_EQ(settings.logging_dest & ~LOG_TO_SYSTEM_DEBUG_LOG, 0); |
720 | | #endif |
721 | |
|
722 | 0 | logging_destination = settings.logging_dest; |
723 | | |
724 | | // ignore file options unless logging to file is set. |
725 | 0 | if ((logging_destination & LOG_TO_FILE) == 0) |
726 | 0 | return true; |
727 | | |
728 | 0 | LoggingLock::Init(settings.lock_log, settings.log_file); |
729 | 0 | LoggingLock logging_lock; |
730 | | |
731 | | // Calling InitLogging twice or after some log call has already opened the |
732 | | // default log file will re-initialize to the new options. |
733 | 0 | CloseLogFileUnlocked(); |
734 | |
|
735 | 0 | if (!log_file_name) |
736 | 0 | log_file_name = new PathString(); |
737 | 0 | if (settings.log_file) { |
738 | 0 | *log_file_name = settings.log_file; |
739 | 0 | } else { |
740 | 0 | *log_file_name = GetDefaultLogFile(); |
741 | 0 | } |
742 | 0 | if (settings.delete_old == DELETE_OLD_LOG_FILE) |
743 | 0 | DeleteFilePath(*log_file_name); |
744 | |
|
745 | 0 | return InitializeLogFileHandle(); |
746 | 0 | } |
747 | | |
748 | 0 | void SetMinLogLevel(int level) { |
749 | 0 | FLAGS_minloglevel = std::min(BLOG_FATAL, level); |
750 | 0 | } |
751 | | |
752 | 19.0k | int GetMinLogLevel() { |
753 | 19.0k | return FLAGS_minloglevel; |
754 | 19.0k | } |
755 | | |
756 | 0 | void SetShowErrorDialogs(bool enable_dialogs) { |
757 | 0 | show_error_dialogs = enable_dialogs; |
758 | 0 | } |
759 | | |
760 | 0 | void SetLogAssertHandler(LogAssertHandler handler) { |
761 | 0 | log_assert_handler = handler; |
762 | 0 | } |
763 | | |
764 | | const char* const log_severity_names[LOG_NUM_SEVERITIES] = { |
765 | | "INFO", "NOTICE", "WARNING", "ERROR", "FATAL" }; |
766 | | |
767 | 19.2k | static void PrintLogSeverity(std::ostream& os, int severity) { |
768 | 19.2k | if (severity < 0) { |
769 | | // Add extra space to separate from following datetime. |
770 | 0 | os << 'V' << -severity << ' '; |
771 | 19.2k | } else if (severity < LOG_NUM_SEVERITIES) { |
772 | 19.2k | os << log_severity_names[severity][0]; |
773 | 19.2k | } else { |
774 | 0 | os << 'U'; |
775 | 0 | } |
776 | 19.2k | } |
777 | | |
778 | | void PrintLogPrefix(std::ostream& os, int severity, |
779 | | const char* file, int line, |
780 | 19.2k | const char* func) { |
781 | 19.2k | PrintLogSeverity(os, severity); |
782 | 19.2k | #if defined(OS_LINUX) || defined(OS_MACOSX) |
783 | 19.2k | timeval tv; |
784 | 19.2k | gettimeofday(&tv, NULL); |
785 | 19.2k | time_t t = tv.tv_sec; |
786 | | #else |
787 | | time_t t = time(NULL); |
788 | | #endif |
789 | 19.2k | struct tm local_tm = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL}; |
790 | | #if _MSC_VER >= 1400 |
791 | | localtime_s(&local_tm, &t); |
792 | | #else |
793 | 19.2k | localtime_r(&t, &local_tm); |
794 | 19.2k | #endif |
795 | 19.2k | const char prev_fill = os.fill('0'); |
796 | 19.2k | if (FLAGS_log_year) { |
797 | 0 | os << std::setw(4) << local_tm.tm_year + 1900; |
798 | 0 | } |
799 | 19.2k | os << std::setw(2) << local_tm.tm_mon + 1 |
800 | 19.2k | << std::setw(2) << local_tm.tm_mday << ' ' |
801 | 19.2k | << std::setw(2) << local_tm.tm_hour << ':' |
802 | 19.2k | << std::setw(2) << local_tm.tm_min << ':' |
803 | 19.2k | << std::setw(2) << local_tm.tm_sec; |
804 | 19.2k | #if defined(OS_LINUX) || defined(OS_MACOSX) |
805 | 19.2k | os << '.' << std::setw(6) << tv.tv_usec; |
806 | 19.2k | #endif |
807 | 19.2k | if (FLAGS_log_pid) { |
808 | 0 | os << ' ' << std::setfill(' ') << std::setw(5) << CurrentProcessId(); |
809 | 0 | } |
810 | 19.2k | os << ' ' << std::setfill(' ') << std::setw(5) |
811 | 19.2k | << butil::PlatformThread::CurrentId() << std::setfill('0'); |
812 | 19.2k | if (FLAGS_log_hostname) { |
813 | 0 | butil::StringPiece hostname(butil::my_hostname()); |
814 | 0 | if (hostname.ends_with(".baidu.com")) { // make it shorter |
815 | 0 | hostname.remove_suffix(10); |
816 | 0 | } |
817 | 0 | os << ' ' << hostname; |
818 | 0 | } |
819 | 19.2k | os << ' ' << file << ':' << line; |
820 | 19.2k | if (func && *func != '\0') { |
821 | 0 | os << " " << func; |
822 | 0 | } |
823 | 19.2k | os << "] "; |
824 | | |
825 | 19.2k | os.fill(prev_fill); |
826 | 19.2k | } |
827 | | |
828 | | void PrintLogPrefix(std::ostream& os, int severity, |
829 | 0 | const char* file, int line) { |
830 | 0 | PrintLogPrefix(os, severity, file, line, ""); |
831 | 0 | } |
832 | | |
833 | | static void PrintLogPrefixAsJSON(std::ostream& os, int severity, |
834 | | const char* file, const char* func, |
835 | 0 | int line) { |
836 | | // severity |
837 | 0 | os << "\"L\":\""; |
838 | 0 | if (severity < 0) { |
839 | 0 | os << 'V' << -severity; |
840 | 0 | } else if (severity < LOG_NUM_SEVERITIES) { |
841 | 0 | os << log_severity_names[severity][0]; |
842 | 0 | } else { |
843 | 0 | os << 'U'; |
844 | 0 | } |
845 | | // time |
846 | 0 | os << "\",\"T\":\""; |
847 | 0 | #if defined(OS_LINUX) |
848 | 0 | timeval tv; |
849 | 0 | gettimeofday(&tv, NULL); |
850 | 0 | time_t t = tv.tv_sec; |
851 | | #else |
852 | | time_t t = time(NULL); |
853 | | #endif |
854 | 0 | struct tm local_tm = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, NULL}; |
855 | | #if _MSC_VER >= 1400 |
856 | | localtime_s(&local_tm, &t); |
857 | | #else |
858 | 0 | localtime_r(&t, &local_tm); |
859 | 0 | #endif |
860 | 0 | const char prev_fill = os.fill('0'); |
861 | 0 | if (FLAGS_log_year) { |
862 | 0 | os << std::setw(4) << local_tm.tm_year + 1900; |
863 | 0 | } |
864 | 0 | os << std::setw(2) << local_tm.tm_mon + 1 |
865 | 0 | << std::setw(2) << local_tm.tm_mday << ' ' |
866 | 0 | << std::setw(2) << local_tm.tm_hour << ':' |
867 | 0 | << std::setw(2) << local_tm.tm_min << ':' |
868 | 0 | << std::setw(2) << local_tm.tm_sec; |
869 | 0 | #if defined(OS_LINUX) |
870 | 0 | os << '.' << std::setw(6) << tv.tv_usec; |
871 | 0 | #endif |
872 | 0 | os << "\","; |
873 | 0 | os.fill(prev_fill); |
874 | |
|
875 | 0 | if (FLAGS_log_pid) { |
876 | 0 | os << "\"pid\":\"" << CurrentProcessId() << "\","; |
877 | 0 | } |
878 | 0 | os << "\"tid\":\"" << butil::PlatformThread::CurrentId() << "\","; |
879 | 0 | if (FLAGS_log_hostname) { |
880 | 0 | butil::StringPiece hostname(butil::my_hostname()); |
881 | 0 | if (hostname.ends_with(".baidu.com")) { // make it shorter |
882 | 0 | hostname.remove_suffix(10); |
883 | 0 | } |
884 | 0 | os << "\"host\":\"" << hostname << "\","; |
885 | 0 | } |
886 | 0 | os << "\"C\":\"" << file << ':' << line; |
887 | 0 | if (func && *func != '\0') { |
888 | 0 | os << " " << func; |
889 | 0 | } |
890 | 0 | os << "\""; |
891 | 0 | } |
892 | | |
893 | 0 | void EscapeJson(std::ostream& os, const butil::StringPiece& s) { |
894 | 0 | for (auto it = s.begin(); it != s.end(); it++) { |
895 | 0 | auto c = *it; |
896 | 0 | switch (c) { |
897 | 0 | case '"': os << "\\\""; break; |
898 | 0 | case '\\': os << "\\\\"; break; |
899 | 0 | case '\b': os << "\\b"; break; |
900 | 0 | case '\f': os << "\\f"; break; |
901 | 0 | case '\n': os << "\\n"; break; |
902 | 0 | case '\r': os << "\\r"; break; |
903 | 0 | case '\t': os << "\\t"; break; |
904 | 0 | default: os << c; |
905 | 0 | } |
906 | 0 | } |
907 | 0 | } |
908 | | |
909 | 19.2k | inline void OutputLog(std::ostream& os, const butil::StringPiece& s) { |
910 | 19.2k | if (FLAGS_escape_log) { |
911 | 0 | EscapeJson(os, s); |
912 | 19.2k | } else { |
913 | 19.2k | os.write(s.data(), s.length()); |
914 | 19.2k | } |
915 | 19.2k | } |
916 | | |
917 | | void PrintLog(std::ostream& os, int severity, const char* file, int line, |
918 | 19.2k | const char* func, const butil::StringPiece& content) { |
919 | 19.2k | if (!FLAGS_log_as_json) { |
920 | 19.2k | PrintLogPrefix(os, severity, file, line, func); |
921 | 19.2k | OutputLog(os, content); |
922 | 19.2k | } else { |
923 | 0 | os << '{'; |
924 | 0 | PrintLogPrefixAsJSON(os, severity, file, func, line); |
925 | 0 | bool pair_quote = false; |
926 | 0 | if (content.empty() || content[0] != '"') { |
927 | | // not a json, add a 'M' field |
928 | 0 | os << ",\"M\":\""; |
929 | 0 | pair_quote = true; |
930 | 0 | } else { |
931 | 0 | os << ','; |
932 | 0 | } |
933 | 0 | OutputLog(os, content); |
934 | 0 | if (pair_quote) { |
935 | 0 | os << '"'; |
936 | 0 | } else if (!content.empty() && content[content.size()-1] != '"') { |
937 | | // Controller may write `"M":"...` which misses the last quote |
938 | 0 | os << '"'; |
939 | 0 | } |
940 | 0 | os << '}'; |
941 | 0 | } |
942 | 19.2k | } |
943 | | |
944 | | void PrintLog(std::ostream& os, |
945 | | int severity, const char* file, int line, |
946 | 0 | const butil::StringPiece& content) { |
947 | 0 | PrintLog(os, severity, file, line, "", content); |
948 | 0 | } |
949 | | |
950 | | // A log message handler that gets notified of every log message we process. |
951 | | class DoublyBufferedLogSink : public butil::DoublyBufferedData<LogSink*> { |
952 | | public: |
953 | 5 | DoublyBufferedLogSink() {} |
954 | | static DoublyBufferedLogSink* GetInstance(); |
955 | | private: |
956 | | friend struct DefaultSingletonTraits<DoublyBufferedLogSink>; |
957 | | DISALLOW_COPY_AND_ASSIGN(DoublyBufferedLogSink); |
958 | | }; |
959 | | |
960 | 19.2k | DoublyBufferedLogSink* DoublyBufferedLogSink::GetInstance() { |
961 | 19.2k | return Singleton<DoublyBufferedLogSink, |
962 | 19.2k | LeakySingletonTraits<DoublyBufferedLogSink> >::get(); |
963 | 19.2k | } |
964 | | |
965 | | struct SetLogSinkFn { |
966 | | LogSink* new_sink; |
967 | | LogSink* old_sink; |
968 | | |
969 | 0 | bool operator()(LogSink*& ptr) { |
970 | 0 | old_sink = ptr; |
971 | 0 | ptr = new_sink; |
972 | 0 | return true; |
973 | 0 | } |
974 | | }; |
975 | | |
976 | 0 | LogSink* SetLogSink(LogSink* sink) { |
977 | 0 | SetLogSinkFn fn = { sink, NULL }; |
978 | 0 | CHECK(DoublyBufferedLogSink::GetInstance()->Modify(fn)); |
979 | 0 | return fn.old_sink; |
980 | 0 | } |
981 | | |
982 | | // MSVC doesn't like complex extern templates and DLLs. |
983 | | #if !defined(COMPILER_MSVC) |
984 | | // Explicit instantiations for commonly used comparisons. |
985 | | template std::string* MakeCheckOpString<int, int>( |
986 | | const int&, const int&, const char* names); |
987 | | template std::string* MakeCheckOpString<unsigned long, unsigned long>( |
988 | | const unsigned long&, const unsigned long&, const char* names); |
989 | | template std::string* MakeCheckOpString<unsigned long, unsigned int>( |
990 | | const unsigned long&, const unsigned int&, const char* names); |
991 | | template std::string* MakeCheckOpString<unsigned int, unsigned long>( |
992 | | const unsigned int&, const unsigned long&, const char* names); |
993 | | template std::string* MakeCheckOpString<std::string, std::string>( |
994 | | const std::string&, const std::string&, const char* name); |
995 | | #endif |
996 | | |
997 | | #if !defined(NDEBUG) |
998 | | // Displays a message box to the user with the error message in it. |
999 | | // Used for fatal messages, where we close the app simultaneously. |
1000 | | // This is for developers only; we don't use this in circumstances |
1001 | | // (like release builds) where users could see it, since users don't |
1002 | | // understand these messages anyway. |
1003 | 0 | void DisplayDebugMessageInDialog(const std::string& str) { |
1004 | 0 | if (str.empty()) |
1005 | 0 | return; |
1006 | | |
1007 | 0 | if (!show_error_dialogs) |
1008 | 0 | return; |
1009 | |
|
1010 | | #if defined(OS_WIN) |
1011 | | // For Windows programs, it's possible that the message loop is |
1012 | | // messed up on a fatal error, and creating a MessageBox will cause |
1013 | | // that message loop to be run. Instead, we try to spawn another |
1014 | | // process that displays its command line. We look for "Debug |
1015 | | // Message.exe" in the same directory as the application. If it |
1016 | | // exists, we use it, otherwise, we use a regular message box. |
1017 | | wchar_t prog_name[MAX_PATH]; |
1018 | | GetModuleFileNameW(NULL, prog_name, MAX_PATH); |
1019 | | wchar_t* backslash = wcsrchr(prog_name, '\\'); |
1020 | | if (backslash) |
1021 | | backslash[1] = 0; |
1022 | | wcscat_s(prog_name, MAX_PATH, L"debug_message.exe"); |
1023 | | |
1024 | | std::wstring cmdline = butil::UTF8ToWide(str); |
1025 | | if (cmdline.empty()) |
1026 | | return; |
1027 | | |
1028 | | STARTUPINFO startup_info; |
1029 | | memset(&startup_info, 0, sizeof(startup_info)); |
1030 | | startup_info.cb = sizeof(startup_info); |
1031 | | |
1032 | | PROCESS_INFORMATION process_info; |
1033 | | if (CreateProcessW(prog_name, &cmdline[0], NULL, NULL, false, 0, NULL, |
1034 | | NULL, &startup_info, &process_info)) { |
1035 | | WaitForSingleObject(process_info.hProcess, INFINITE); |
1036 | | CloseHandle(process_info.hThread); |
1037 | | CloseHandle(process_info.hProcess); |
1038 | | } else { |
1039 | | // debug process broken, let's just do a message box |
1040 | | MessageBoxW(NULL, &cmdline[0], L"Fatal error", |
1041 | | MB_OK | MB_ICONHAND | MB_TOPMOST); |
1042 | | } |
1043 | | #else |
1044 | | // We intentionally don't implement a dialog on other platforms. |
1045 | | // You can just look at stderr. |
1046 | 0 | #endif |
1047 | 0 | } |
1048 | | #endif // !defined(NDEBUG) |
1049 | | |
1050 | | |
1051 | | bool StringSink::OnLogMessage(int severity, const char* file, int line, |
1052 | 0 | const butil::StringPiece& content) { |
1053 | 0 | return OnLogMessage(severity, file, line, "", content); |
1054 | 0 | } |
1055 | | |
1056 | | bool StringSink::OnLogMessage(int severity, const char* file, |
1057 | | int line, const char* func, |
1058 | 0 | const butil::StringPiece& content) { |
1059 | 0 | std::ostringstream os; |
1060 | 0 | PrintLog(os, severity, file, line, func, content); |
1061 | 0 | const std::string msg = os.str(); |
1062 | 0 | { |
1063 | 0 | butil::AutoLock lock_guard(_lock); |
1064 | 0 | append(msg); |
1065 | 0 | } |
1066 | 0 | return true; |
1067 | 0 | } |
1068 | | |
1069 | 0 | CharArrayStreamBuf::~CharArrayStreamBuf() { |
1070 | 0 | free(_data); |
1071 | 0 | } |
1072 | | |
1073 | 28 | int CharArrayStreamBuf::overflow(int ch) { |
1074 | 28 | if (ch == std::streambuf::traits_type::eof()) { |
1075 | 0 | return ch; |
1076 | 0 | } |
1077 | 28 | size_t new_size = std::max(_size * 3 / 2, (size_t)64); |
1078 | 28 | char* new_data = (char*)malloc(new_size); |
1079 | 28 | if (BAIDU_UNLIKELY(new_data == NULL)) { |
1080 | 0 | setp(NULL, NULL); |
1081 | 0 | return std::streambuf::traits_type::eof(); |
1082 | 0 | } |
1083 | 28 | memcpy(new_data, _data, _size); |
1084 | 28 | free(_data); |
1085 | 28 | _data = new_data; |
1086 | 28 | const size_t old_size = _size; |
1087 | 28 | _size = new_size; |
1088 | 28 | setp(_data, _data + new_size); |
1089 | 28 | pbump(old_size); |
1090 | | // if size == 1, this function will call overflow again. |
1091 | 28 | return sputc(ch); |
1092 | 28 | } |
1093 | | |
1094 | 186 | int CharArrayStreamBuf::sync() { |
1095 | | // data are already there. |
1096 | 186 | return 0; |
1097 | 186 | } |
1098 | | |
1099 | 19.2k | void CharArrayStreamBuf::reset() { |
1100 | 19.2k | setp(_data, _data + _size); |
1101 | 19.2k | } |
1102 | | |
1103 | | LogStream& LogStream::SetPosition(const LogChar* file, int line, |
1104 | 0 | LogSeverity severity) { |
1105 | 0 | _file = file; |
1106 | 0 | _line = line; |
1107 | 0 | _severity = severity; |
1108 | 0 | return *this; |
1109 | 0 | } |
1110 | | |
1111 | | LogStream& LogStream::SetPosition(const LogChar* file, int line, |
1112 | | const LogChar* func, |
1113 | 19.2k | LogSeverity severity) { |
1114 | 19.2k | _file = file; |
1115 | 19.2k | _line = line; |
1116 | 19.2k | _func = func; |
1117 | 19.2k | _severity = severity; |
1118 | 19.2k | return *this; |
1119 | 19.2k | } |
1120 | | |
1121 | | #if defined(__GNUC__) |
1122 | | static bthread_key_t stream_bkey; |
1123 | | static pthread_key_t stream_pkey; |
1124 | | static pthread_once_t create_stream_key_once = PTHREAD_ONCE_INIT; |
1125 | 19.2k | inline bool is_bthread_linked() { return bthread_key_create != NULL; } |
1126 | 0 | static void destroy_tls_streams(void* data) { |
1127 | 0 | if (data == NULL) { |
1128 | 0 | return; |
1129 | 0 | } |
1130 | 0 | LogStream** a = (LogStream**)data; |
1131 | 0 | for (int i = 0; i <= LOG_NUM_SEVERITIES; ++i) { |
1132 | 0 | delete a[i]; |
1133 | 0 | } |
1134 | 0 | delete[] a; |
1135 | 0 | } |
1136 | 5 | static void create_stream_key_or_die() { |
1137 | 5 | if (is_bthread_linked()) { |
1138 | 5 | int rc = bthread_key_create(&stream_bkey, destroy_tls_streams); |
1139 | 5 | if (rc) { |
1140 | 0 | fprintf(stderr, "Fail to bthread_key_create"); |
1141 | 0 | exit(1); |
1142 | 0 | } |
1143 | 5 | } else { |
1144 | 0 | int rc = pthread_key_create(&stream_pkey, destroy_tls_streams); |
1145 | 0 | if (rc) { |
1146 | 0 | fprintf(stderr, "Fail to pthread_key_create"); |
1147 | 0 | exit(1); |
1148 | 0 | } |
1149 | 0 | } |
1150 | 5 | } |
1151 | 19.2k | static LogStream** get_tls_stream_array() { |
1152 | 19.2k | pthread_once(&create_stream_key_once, create_stream_key_or_die); |
1153 | 19.2k | if (is_bthread_linked()) { |
1154 | 19.2k | return (LogStream**)bthread_getspecific(stream_bkey); |
1155 | 19.2k | } else { |
1156 | 0 | return (LogStream**)pthread_getspecific(stream_pkey); |
1157 | 0 | } |
1158 | 19.2k | } |
1159 | | |
1160 | 19.2k | static LogStream** get_or_new_tls_stream_array() { |
1161 | 19.2k | LogStream** a = get_tls_stream_array(); |
1162 | 19.2k | if (a == NULL) { |
1163 | 5 | a = new LogStream*[LOG_NUM_SEVERITIES + 1]; |
1164 | 5 | memset(a, 0, sizeof(LogStream*) * (LOG_NUM_SEVERITIES + 1)); |
1165 | 5 | if (is_bthread_linked()) { |
1166 | 5 | bthread_setspecific(stream_bkey, a); |
1167 | 5 | } else { |
1168 | 0 | pthread_setspecific(stream_pkey, a); |
1169 | 0 | } |
1170 | 5 | } |
1171 | 19.2k | return a; |
1172 | 19.2k | } |
1173 | | |
1174 | | inline LogStream* CreateLogStream(const LogChar* file, |
1175 | | int line, |
1176 | | const LogChar* func, |
1177 | 19.2k | LogSeverity severity) { |
1178 | 19.2k | int slot = 0; |
1179 | 19.2k | if (severity >= 0) { |
1180 | 19.2k | DCHECK_LT(severity, LOG_NUM_SEVERITIES); |
1181 | 19.2k | slot = severity + 1; |
1182 | 19.2k | } // else vlog |
1183 | 19.2k | LogStream** stream_array = get_or_new_tls_stream_array(); |
1184 | 19.2k | LogStream* stream = stream_array[slot]; |
1185 | 19.2k | if (stream == NULL) { |
1186 | 9 | stream = new LogStream; |
1187 | 9 | stream_array[slot] = stream; |
1188 | 9 | } |
1189 | 19.2k | if (stream->empty()) { |
1190 | 19.2k | stream->SetPosition(file, line, func, severity); |
1191 | 19.2k | } |
1192 | 19.2k | return stream; |
1193 | 19.2k | } |
1194 | | |
1195 | | inline LogStream* CreateLogStream(const LogChar* file, |
1196 | | int line, |
1197 | 0 | LogSeverity severity) { |
1198 | 0 | return CreateLogStream(file, line, "", severity); |
1199 | 0 | } |
1200 | | |
1201 | 19.2k | inline void DestroyLogStream(LogStream* stream) { |
1202 | 19.2k | if (stream != NULL) { |
1203 | 19.2k | stream->Flush(); |
1204 | 19.2k | } |
1205 | 19.2k | } |
1206 | | |
1207 | | #else |
1208 | | |
1209 | | inline LogStream* CreateLogStream(const LogChar* file, int line, |
1210 | | LogSeverity severity) { |
1211 | | return CreateLogStream(file, line, "", severity); |
1212 | | } |
1213 | | |
1214 | | |
1215 | | inline LogStream* CreateLogStream(const LogChar* file, int line, |
1216 | | const LogChar* func, |
1217 | | LogSeverity severity) { |
1218 | | LogStream* stream = new LogStream; |
1219 | | stream->SetPosition(file, line, func, severity); |
1220 | | return stream; |
1221 | | } |
1222 | | |
1223 | | inline void DestroyLogStream(LogStream* stream) { |
1224 | | delete stream; |
1225 | | } |
1226 | | |
1227 | | #endif // __GNUC__ |
1228 | | |
1229 | | class DefaultLogSink : public LogSink { |
1230 | | public: |
1231 | 19.2k | static DefaultLogSink* GetInstance() { |
1232 | 19.2k | return Singleton<DefaultLogSink, |
1233 | 19.2k | LeakySingletonTraits<DefaultLogSink> >::get(); |
1234 | 19.2k | } |
1235 | | |
1236 | | bool OnLogMessage(int severity, const char* file, int line, |
1237 | 19.2k | const butil::StringPiece& content) override { |
1238 | 19.2k | return OnLogMessage(severity, file, line, "", content); |
1239 | 19.2k | } |
1240 | | |
1241 | | bool OnLogMessage(int severity, const char* file, |
1242 | | int line, const char* func, |
1243 | 19.2k | const butil::StringPiece& content) override { |
1244 | | // There's a copy here to concatenate prefix and content. Since |
1245 | | // DefaultLogSink is hardly used right now, the copy is irrelevant. |
1246 | | // A LogSink focused on performance should also be able to handle |
1247 | | // non-continuous inputs which is a must to maximize performance. |
1248 | 19.2k | std::ostringstream os; |
1249 | 19.2k | PrintLog(os, severity, file, line, func, content); |
1250 | 19.2k | os << '\n'; |
1251 | 19.2k | std::string log = os.str(); |
1252 | | |
1253 | 19.2k | if ((logging_destination & LOG_TO_SYSTEM_DEBUG_LOG) != 0) { |
1254 | 19.2k | fwrite(log.data(), log.size(), 1, stderr); |
1255 | 19.2k | fflush(stderr); |
1256 | 19.2k | } else if (severity >= kAlwaysPrintErrorLevel) { |
1257 | | // When we're only outputting to a log file, above a certain log level, we |
1258 | | // should still output to stderr so that we can better detect and diagnose |
1259 | | // problems with unit tests, especially on the buildbots. |
1260 | 0 | fwrite(log.data(), log.size(), 1, stderr); |
1261 | 0 | fflush(stderr); |
1262 | 0 | } |
1263 | | |
1264 | | // write to log file |
1265 | 19.2k | if ((logging_destination & LOG_TO_FILE) != 0) { |
1266 | 0 | if (FLAGS_async_log) { |
1267 | 0 | AsyncLogger::GetInstance()->Log(std::move(log)); |
1268 | 0 | } else { |
1269 | 0 | Log2File(log); |
1270 | 0 | } |
1271 | 0 | } |
1272 | 19.2k | return true; |
1273 | 19.2k | } |
1274 | | private: |
1275 | 5 | DefaultLogSink() = default; |
1276 | | ~DefaultLogSink() override = default; |
1277 | | friend struct DefaultSingletonTraits<DefaultLogSink>; |
1278 | | }; |
1279 | | |
1280 | 19.2k | void LogStream::FlushWithoutReset() { |
1281 | 19.2k | if (empty()) { |
1282 | | // Nothing to flush. |
1283 | 0 | return; |
1284 | 0 | } |
1285 | | |
1286 | 19.2k | #if !defined(OS_NACL) && !defined(__UCLIBC__) |
1287 | 19.2k | if (FLAGS_print_stack_on_check && _is_check && _severity == BLOG_FATAL) { |
1288 | | // Include a stack trace on a fatal. |
1289 | 186 | butil::debug::StackTrace trace; |
1290 | 186 | size_t count = 0; |
1291 | 186 | const void* const* addrs = trace.Addresses(&count); |
1292 | | |
1293 | 186 | *this << std::endl; // Newline to separate from log message. |
1294 | 186 | if (count > 3) { |
1295 | | // Remove top 3 frames which are useless to users. |
1296 | | // #2 may be ~LogStream |
1297 | | // #0 0x00000059ccae butil::debug::StackTrace::StackTrace() |
1298 | | // #1 0x0000005947c7 logging::LogStream::FlushWithoutReset() |
1299 | | // #2 0x000000594b88 logging::LogMessage::~LogMessage() |
1300 | 186 | butil::debug::StackTrace trace_stripped(addrs + 3, count - 3); |
1301 | 186 | trace_stripped.OutputToStream(this); |
1302 | 186 | } else { |
1303 | 0 | trace.OutputToStream(this); |
1304 | 0 | } |
1305 | 186 | } |
1306 | 19.2k | #endif |
1307 | | // End the data with zero because sink is likely to assume this. |
1308 | 19.2k | *this << std::ends; |
1309 | | // Move back one step because we don't want to count the zero. |
1310 | 19.2k | pbump(-1); |
1311 | | |
1312 | | // Give any logsink first dibs on the message. |
1313 | | #ifdef BAIDU_INTERNAL |
1314 | | // If the logsink fails and it's not comlog, try comlog. stderr on last try. |
1315 | | bool tried_comlog = false; |
1316 | | #endif |
1317 | 19.2k | bool tried_default = false; |
1318 | 19.2k | { |
1319 | 19.2k | DoublyBufferedLogSink::ScopedPtr ptr; |
1320 | 19.2k | if (DoublyBufferedLogSink::GetInstance()->Read(&ptr) == 0 && |
1321 | 19.2k | (*ptr) != NULL) { |
1322 | 0 | bool result = false; |
1323 | 0 | if (FLAGS_log_func_name) { |
1324 | 0 | result = (*ptr)->OnLogMessage(_severity, _file, _line, |
1325 | 0 | _func, content()); |
1326 | 0 | } else { |
1327 | 0 | result = (*ptr)->OnLogMessage(_severity, _file, |
1328 | 0 | _line, content()); |
1329 | 0 | } |
1330 | 0 | if (result) { |
1331 | 0 | goto FINISH_LOGGING; |
1332 | 0 | } |
1333 | | #ifdef BAIDU_INTERNAL |
1334 | | tried_comlog = (*ptr == ComlogSink::GetInstance()); |
1335 | | #endif |
1336 | 0 | tried_default = (*ptr == DefaultLogSink::GetInstance()); |
1337 | 0 | } |
1338 | 19.2k | } |
1339 | | |
1340 | | #ifdef BAIDU_INTERNAL |
1341 | | if (!tried_comlog) { |
1342 | | if (ComlogSink::GetInstance()->OnLogMessage( |
1343 | | _severity, _file, _line, _func, content())) { |
1344 | | goto FINISH_LOGGING; |
1345 | | } |
1346 | | } |
1347 | | #endif |
1348 | 19.2k | if (!tried_default) { |
1349 | 19.2k | if (FLAGS_log_func_name) { |
1350 | 0 | DefaultLogSink::GetInstance()->OnLogMessage( |
1351 | 0 | _severity, _file, _line, _func, content()); |
1352 | 19.2k | } else { |
1353 | 19.2k | DefaultLogSink::GetInstance()->OnLogMessage( |
1354 | 19.2k | _severity, _file, _line, content()); |
1355 | 19.2k | } |
1356 | 19.2k | } |
1357 | | |
1358 | 19.2k | FINISH_LOGGING: |
1359 | 19.2k | if (FLAGS_crash_on_fatal_log && _severity == BLOG_FATAL) { |
1360 | | // Ensure the first characters of the string are on the stack so they |
1361 | | // are contained in minidumps for diagnostic purposes. |
1362 | 0 | butil::StringPiece str = content(); |
1363 | 0 | char str_stack[1024]; |
1364 | 0 | str.copy(str_stack, arraysize(str_stack)); |
1365 | 0 | butil::debug::Alias(str_stack); |
1366 | |
|
1367 | 0 | if (log_assert_handler) { |
1368 | | // Make a copy of the string for the handler out of paranoia. |
1369 | 0 | log_assert_handler(str.as_string()); |
1370 | 0 | } else { |
1371 | | // Don't use the string with the newline, get a fresh version to send to |
1372 | | // the debug message process. We also don't display assertions to the |
1373 | | // user in release mode. The enduser can't do anything with this |
1374 | | // information, and displaying message boxes when the application is |
1375 | | // hosed can cause additional problems. |
1376 | 0 | #ifndef NDEBUG |
1377 | 0 | DisplayDebugMessageInDialog(str.as_string()); |
1378 | 0 | #endif |
1379 | | // Crash the process to generate a dump. |
1380 | 0 | butil::debug::BreakDebugger(); |
1381 | 0 | } |
1382 | 0 | } |
1383 | 19.2k | } |
1384 | | |
1385 | | LogMessage::LogMessage(const char* file, int line, LogSeverity severity) |
1386 | 0 | : LogMessage(file, line, "", severity) {} |
1387 | | |
1388 | | LogMessage::LogMessage(const char* file, int line, |
1389 | 19.2k | const char* func, LogSeverity severity) { |
1390 | 19.2k | _stream = CreateLogStream(file, line, func, severity); |
1391 | 19.2k | } |
1392 | | |
1393 | | LogMessage::LogMessage(const char* file, int line, std::string* result) |
1394 | 0 | : LogMessage(file, line, "", result) {} |
1395 | | |
1396 | | LogMessage::LogMessage(const char* file, int line, |
1397 | 0 | const char* func, std::string* result) { |
1398 | 0 | _stream = CreateLogStream(file, line, func, BLOG_FATAL); |
1399 | 0 | *_stream << "Check failed: " << *result; |
1400 | 0 | delete result; |
1401 | 0 | } |
1402 | | |
1403 | | LogMessage::LogMessage(const char* file, int line, LogSeverity severity, |
1404 | | std::string* result) |
1405 | 0 | : LogMessage(file, line, "", severity, result) {} |
1406 | | |
1407 | | LogMessage::LogMessage(const char* file, int line, const char* func, |
1408 | 0 | LogSeverity severity, std::string* result) { |
1409 | 0 | _stream = CreateLogStream(file, line, func, severity); |
1410 | 0 | *_stream << "Check failed: " << *result; |
1411 | 0 | delete result; |
1412 | 0 | } |
1413 | | |
1414 | 19.2k | LogMessage::~LogMessage() { |
1415 | 19.2k | DestroyLogStream(_stream); |
1416 | 19.2k | } |
1417 | | |
1418 | | #if defined(OS_WIN) |
1419 | | // This has already been defined in the header, but defining it again as DWORD |
1420 | | // ensures that the type used in the header is equivalent to DWORD. If not, |
1421 | | // the redefinition is a compile error. |
1422 | | typedef DWORD SystemErrorCode; |
1423 | | #endif |
1424 | | |
1425 | 19.2k | SystemErrorCode GetLastSystemErrorCode() { |
1426 | | #if defined(OS_WIN) |
1427 | | return ::GetLastError(); |
1428 | | #elif defined(OS_POSIX) |
1429 | 19.2k | return errno; |
1430 | | #else |
1431 | | #error Not implemented |
1432 | | #endif |
1433 | 19.2k | } |
1434 | | |
1435 | 19.2k | void SetLastSystemErrorCode(SystemErrorCode err) { |
1436 | | #if defined(OS_WIN) |
1437 | | ::SetLastError(err); |
1438 | | #elif defined(OS_POSIX) |
1439 | | errno = err; |
1440 | | #else |
1441 | | #error Not implemented |
1442 | | #endif |
1443 | 19.2k | } |
1444 | | |
1445 | | #if defined(OS_WIN) |
1446 | | BUTIL_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { |
1447 | | const int error_message_buffer_size = 256; |
1448 | | char msgbuf[error_message_buffer_size]; |
1449 | | DWORD flags = FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; |
1450 | | DWORD len = FormatMessageA(flags, NULL, error_code, 0, msgbuf, |
1451 | | arraysize(msgbuf), NULL); |
1452 | | if (len) { |
1453 | | // Messages returned by system end with line breaks. |
1454 | | return butil::CollapseWhitespaceASCII(msgbuf, true) + |
1455 | | butil::StringPrintf(" (0x%X)", error_code); |
1456 | | } |
1457 | | return butil::StringPrintf("Error (0x%X) while retrieving error. (0x%X)", |
1458 | | GetLastError(), error_code); |
1459 | | } |
1460 | | #elif defined(OS_POSIX) |
1461 | 0 | BUTIL_EXPORT std::string SystemErrorCodeToString(SystemErrorCode error_code) { |
1462 | 0 | return berror(error_code); |
1463 | 0 | } |
1464 | | #else |
1465 | | #error Not implemented |
1466 | | #endif |
1467 | | |
1468 | | |
1469 | | #if defined(OS_WIN) |
1470 | | Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
1471 | | int line, |
1472 | | LogSeverity severity, |
1473 | | SystemErrorCode err) |
1474 | | : Win32ErrorLogMessage(file, line, "", severity, err) { |
1475 | | } |
1476 | | |
1477 | | Win32ErrorLogMessage::Win32ErrorLogMessage(const char* file, |
1478 | | int line, |
1479 | | const char* func, |
1480 | | LogSeverity severity, |
1481 | | SystemErrorCode err) |
1482 | | : err_(err) |
1483 | | , log_message_(file, line, func, severity) { |
1484 | | } |
1485 | | |
1486 | | Win32ErrorLogMessage::~Win32ErrorLogMessage() { |
1487 | | stream() << ": " << SystemErrorCodeToString(err_); |
1488 | | // We're about to crash (CHECK). Put |err_| on the stack (by placing it in a |
1489 | | // field) and use Alias in hopes that it makes it into crash dumps. |
1490 | | DWORD last_error = err_; |
1491 | | butil::debug::Alias(&last_error); |
1492 | | } |
1493 | | #elif defined(OS_POSIX) |
1494 | | ErrnoLogMessage::ErrnoLogMessage(const char* file, |
1495 | | int line, |
1496 | | LogSeverity severity, |
1497 | | SystemErrorCode err) |
1498 | 0 | : ErrnoLogMessage(file, line, "", severity, err) {} |
1499 | | |
1500 | | ErrnoLogMessage::ErrnoLogMessage(const char* file, |
1501 | | int line, |
1502 | | const char* func, |
1503 | | LogSeverity severity, |
1504 | | SystemErrorCode err) |
1505 | | : err_(err) |
1506 | 0 | , log_message_(file, line, func, severity) {} |
1507 | | |
1508 | 0 | ErrnoLogMessage::~ErrnoLogMessage() { |
1509 | 0 | stream() << ": " << SystemErrorCodeToString(err_); |
1510 | 0 | } |
1511 | | #endif // OS_WIN |
1512 | | |
1513 | 0 | void CloseLogFile() { |
1514 | 0 | LoggingLock logging_lock; |
1515 | 0 | CloseLogFileUnlocked(); |
1516 | 0 | } |
1517 | | |
1518 | 0 | void RawLog(int level, const char* message) { |
1519 | 0 | if (level >= FLAGS_minloglevel) { |
1520 | 0 | size_t bytes_written = 0; |
1521 | 0 | const size_t message_len = strlen(message); |
1522 | 0 | int rv; |
1523 | 0 | while (bytes_written < message_len) { |
1524 | 0 | rv = HANDLE_EINTR( |
1525 | 0 | write(STDERR_FILENO, message + bytes_written, |
1526 | 0 | message_len - bytes_written)); |
1527 | 0 | if (rv < 0) { |
1528 | | // Give up, nothing we can do now. |
1529 | 0 | break; |
1530 | 0 | } |
1531 | 0 | bytes_written += rv; |
1532 | 0 | } |
1533 | |
|
1534 | 0 | if (message_len > 0 && message[message_len - 1] != '\n') { |
1535 | 0 | do { |
1536 | 0 | rv = HANDLE_EINTR(write(STDERR_FILENO, "\n", 1)); |
1537 | 0 | if (rv < 0) { |
1538 | | // Give up, nothing we can do now. |
1539 | 0 | break; |
1540 | 0 | } |
1541 | 0 | } while (rv != 1); |
1542 | 0 | } |
1543 | 0 | } |
1544 | | |
1545 | 0 | if (FLAGS_crash_on_fatal_log && level == BLOG_FATAL) |
1546 | 0 | butil::debug::BreakDebugger(); |
1547 | 0 | } |
1548 | | |
1549 | | // This was defined at the beginning of this file. |
1550 | | #undef write |
1551 | | |
1552 | | #if defined(OS_WIN) |
1553 | | std::wstring GetLogFileFullPath() { |
1554 | | if (log_file_name) |
1555 | | return *log_file_name; |
1556 | | return std::wstring(); |
1557 | | } |
1558 | | #endif |
1559 | | |
1560 | | |
1561 | | // ----------- VLOG stuff ----------------- |
1562 | | struct VLogSite; |
1563 | | struct VModuleList; |
1564 | | |
1565 | | extern const int VLOG_UNINITIALIZED = std::numeric_limits<int>::max(); |
1566 | | |
1567 | | static pthread_mutex_t vlog_site_list_mutex = PTHREAD_MUTEX_INITIALIZER; |
1568 | | static VLogSite* vlog_site_list = NULL; |
1569 | | static VModuleList* vmodule_list = NULL; |
1570 | | |
1571 | | static pthread_mutex_t reset_vmodule_and_v_mutex = PTHREAD_MUTEX_INITIALIZER; |
1572 | | |
1573 | | static const int64_t DELAY_DELETION_SEC = 10; |
1574 | | static std::deque<std::pair<VModuleList*, int64_t> >* |
1575 | | deleting_vmodule_list = NULL; |
1576 | | |
1577 | | struct VLogSite { |
1578 | | VLogSite(const char* filename, int required_v, int line_no) |
1579 | 3 | : _next(0), _v(0), _required_v(required_v), _line_no(line_no) { |
1580 | | // Remove dirname/extname. |
1581 | 3 | butil::StringPiece s(filename); |
1582 | 3 | size_t pos = s.find_last_of("./"); |
1583 | 3 | if (pos != butil::StringPiece::npos) { |
1584 | 3 | if (s[pos] == '.') { |
1585 | 3 | s.remove_suffix(s.size() - pos); |
1586 | 3 | _full_module.assign(s.data(), s.size()); |
1587 | 3 | size_t pos2 = s.find_last_of('/'); |
1588 | 3 | if (pos2 != butil::StringPiece::npos) { |
1589 | 3 | s.remove_prefix(pos2 + 1); |
1590 | 3 | } |
1591 | 3 | } else { |
1592 | 0 | _full_module.assign(s.data(), s.size()); |
1593 | 0 | s.remove_prefix(pos + 1); |
1594 | 0 | } |
1595 | 3 | } // else keep _full_module empty when it equals _module |
1596 | 3 | _module.assign(s.data(), s.size()); |
1597 | 3 | std::transform(_module.begin(), _module.end(), |
1598 | 3 | _module.begin(), ::tolower); |
1599 | 3 | if (!_full_module.empty()) { |
1600 | 3 | std::transform(_full_module.begin(), _full_module.end(), |
1601 | 3 | _full_module.begin(), ::tolower); |
1602 | 3 | } |
1603 | 3 | } |
1604 | | |
1605 | | // The consume/release fence makes the iteration outside lock see |
1606 | | // newly added VLogSite correctly. |
1607 | 0 | VLogSite* next() { return (VLogSite*)butil::subtle::Acquire_Load(&_next); } |
1608 | | const VLogSite* next() const |
1609 | 0 | { return (VLogSite*)butil::subtle::Acquire_Load(&_next); } |
1610 | | void set_next(VLogSite* next) |
1611 | 3 | { butil::subtle::Release_Store(&_next, (butil::subtle::AtomicWord)next); } |
1612 | | |
1613 | 0 | int v() const { return _v; } |
1614 | 9 | int& v() { return _v; } |
1615 | | |
1616 | 0 | int required_v() const { return _required_v; } |
1617 | 0 | int line_no() const { return _line_no; } |
1618 | | |
1619 | 0 | const std::string& module() const { return _module; } |
1620 | 0 | const std::string& full_module() const { return _full_module; } |
1621 | | |
1622 | | private: |
1623 | | // Next site in the list. NULL means no next. |
1624 | | butil::subtle::AtomicWord _next; |
1625 | | |
1626 | | // --vmodule > --v |
1627 | | int _v; |
1628 | | |
1629 | | // vlog is on iff _v >= _required_v |
1630 | | int _required_v; |
1631 | | |
1632 | | // line nubmer of the vlog. |
1633 | | int _line_no; |
1634 | | |
1635 | | // Lowered, dirname & extname removed. |
1636 | | std::string _module; |
1637 | | // Lowered, extname removed. Empty when it equals to _module. |
1638 | | std::string _full_module; |
1639 | | }; |
1640 | | |
1641 | | // Written by Jack Handy |
1642 | | // <A href="mailto:jakkhandy@hotmail.com">jakkhandy@hotmail.com</A> |
1643 | 0 | bool wildcmp(const char* wild, const char* str) { |
1644 | 0 | const char* cp = NULL; |
1645 | 0 | const char* mp = NULL; |
1646 | |
|
1647 | 0 | while (*str && *wild != '*') { |
1648 | 0 | if (*wild != *str && *wild != '?') { |
1649 | 0 | return false; |
1650 | 0 | } |
1651 | 0 | ++wild; |
1652 | 0 | ++str; |
1653 | 0 | } |
1654 | | |
1655 | 0 | while (*str) { |
1656 | 0 | if (*wild == '*') { |
1657 | 0 | if (!*++wild) { |
1658 | 0 | return true; |
1659 | 0 | } |
1660 | 0 | mp = wild; |
1661 | 0 | cp = str+1; |
1662 | 0 | } else if (*wild == *str || *wild == '?') { |
1663 | 0 | ++wild; |
1664 | 0 | ++str; |
1665 | 0 | } else { |
1666 | 0 | wild = mp; |
1667 | 0 | str = cp++; |
1668 | 0 | } |
1669 | 0 | } |
1670 | | |
1671 | 0 | while (*wild == '*') { |
1672 | 0 | ++wild; |
1673 | 0 | } |
1674 | 0 | return !*wild; |
1675 | 0 | } |
1676 | | |
1677 | | struct VModuleList { |
1678 | 0 | VModuleList() {} |
1679 | | |
1680 | 0 | int init(const char* vmodules) { |
1681 | 0 | _exact_names.clear(); |
1682 | 0 | _wild_names.clear(); |
1683 | | |
1684 | 0 | for (butil::StringSplitter sp(vmodules, ','); sp; ++sp) { |
1685 | 0 | int verbose_level = std::numeric_limits<int>::max(); |
1686 | 0 | size_t off = 0; |
1687 | 0 | for (; off < sp.length() && sp.field()[off] != '='; ++off) {} |
1688 | 0 | if (off + 1 < sp.length()) { |
1689 | 0 | verbose_level = strtol(sp.field() + off + 1, NULL, 10); |
1690 | | |
1691 | 0 | } |
1692 | 0 | const char* name_begin = sp.field(); |
1693 | 0 | const char* name_end = sp.field() + off - 1; |
1694 | 0 | for (; isspace(*name_begin) && name_begin < sp.field() + off; |
1695 | 0 | ++name_begin) {} |
1696 | 0 | for (; isspace(*name_end) && name_end >= sp.field(); --name_end) {} |
1697 | | |
1698 | 0 | if (name_begin > name_end) { // only has spaces |
1699 | 0 | continue; |
1700 | 0 | } |
1701 | 0 | std::string name(name_begin, name_end - name_begin + 1); |
1702 | 0 | std::transform(name.begin(), name.end(), name.begin(), ::tolower); |
1703 | 0 | if (name.find_first_of("*?") == std::string::npos) { |
1704 | 0 | _exact_names[name] = verbose_level; |
1705 | 0 | } else { |
1706 | 0 | _wild_names.emplace_back(name, verbose_level); |
1707 | 0 | } |
1708 | 0 | } |
1709 | | // Reverse _wild_names so that latter wild cards override former ones. |
1710 | 0 | if (!_wild_names.empty()) { |
1711 | 0 | std::reverse(_wild_names.begin(), _wild_names.end()); |
1712 | 0 | } |
1713 | 0 | return 0; |
1714 | 0 | } |
1715 | | |
1716 | | bool find_verbose_level(const std::string& module, |
1717 | 0 | const std::string& full_module, int* v) const { |
1718 | 0 | if (!_exact_names.empty()) { |
1719 | 0 | std::map<std::string, int>::const_iterator |
1720 | 0 | it = _exact_names.find(module); |
1721 | 0 | if (it != _exact_names.end()) { |
1722 | 0 | *v = it->second; |
1723 | 0 | return true; |
1724 | 0 | } |
1725 | 0 | if (!full_module.empty()) { |
1726 | 0 | it = _exact_names.find(full_module); |
1727 | 0 | if (it != _exact_names.end()) { |
1728 | 0 | *v = it->second; |
1729 | 0 | return true; |
1730 | 0 | } |
1731 | 0 | } |
1732 | 0 | } |
1733 | | |
1734 | 0 | for (size_t i = 0; i < _wild_names.size(); ++i) { |
1735 | 0 | if (wildcmp(_wild_names[i].first.c_str(), module.c_str())) { |
1736 | 0 | *v = _wild_names[i].second; |
1737 | 0 | return true; |
1738 | 0 | } |
1739 | 0 | if (!full_module.empty() && |
1740 | 0 | wildcmp(_wild_names[i].first.c_str(), full_module.c_str())) { |
1741 | 0 | *v = _wild_names[i].second; |
1742 | 0 | return true; |
1743 | 0 | } |
1744 | 0 | } |
1745 | 0 | return false; |
1746 | 0 | } |
1747 | | |
1748 | 0 | void print(std::ostream& os) const { |
1749 | 0 | os << "exact:"; |
1750 | 0 | for (std::map<std::string, int>::const_iterator |
1751 | 0 | it = _exact_names.begin(); it != _exact_names.end(); ++it) { |
1752 | 0 | os << ' ' << it->first << '=' << it->second; |
1753 | 0 | } |
1754 | 0 | os << ", wild:"; |
1755 | 0 | for (size_t i = 0; i < _wild_names.size(); ++i) { |
1756 | 0 | os << ' ' << _wild_names[i].first << '=' << _wild_names[i].second; |
1757 | 0 | } |
1758 | 0 | } |
1759 | | |
1760 | | private: |
1761 | | std::map<std::string, int> _exact_names; |
1762 | | std::vector<std::pair<std::string, int> > _wild_names; |
1763 | | }; |
1764 | | |
1765 | | // [ The idea ] |
1766 | | // Each callsite creates a VLogSite and inserts the site into singly-linked |
1767 | | // vlog_site_list. To keep the critical area small, we use optimistic |
1768 | | // locking : Assign local site w/o locking, then insert the site into |
1769 | | // global list w/ locking, if local_module_list != global_vmodule_list or |
1770 | | // local_default_v != FLAGS_v, repeat the assigment. |
1771 | | // An important property of vlog_site_list is that: It does not remove sites. |
1772 | | // When we need to iterate the list, we don't have to hold the lock. What we |
1773 | | // do is to get the head of the list inside lock and iterate the list w/o |
1774 | | // lock. If new sites is inserted during the iteration, it should see and |
1775 | | // use the updated vmodule_list and FLAGS_v, nothing will be missed. |
1776 | | |
1777 | | static int vlog_site_list_add(VLogSite* site, |
1778 | | VModuleList** expected_module_list, |
1779 | 3 | int* expected_default_v) { |
1780 | 3 | BAIDU_SCOPED_LOCK(vlog_site_list_mutex); |
1781 | 3 | if (vmodule_list != *expected_module_list) { |
1782 | 0 | *expected_module_list = vmodule_list; |
1783 | 0 | return -1; |
1784 | 0 | } |
1785 | 3 | if (*expected_default_v != FLAGS_v) { |
1786 | 0 | *expected_default_v = FLAGS_v; |
1787 | 0 | return -1; |
1788 | 0 | } |
1789 | 3 | site->set_next(vlog_site_list); |
1790 | 3 | vlog_site_list = site; |
1791 | 3 | return 0; |
1792 | 3 | } |
1793 | | |
1794 | | bool add_vlog_site(const int** v, const char* filename, int line_no, |
1795 | 3 | int required_v) { |
1796 | 3 | VLogSite* site = new (std::nothrow) VLogSite(filename, required_v, line_no); |
1797 | 3 | if (site == NULL) { |
1798 | 0 | return false; |
1799 | 0 | } |
1800 | 3 | VModuleList* module_list = vmodule_list; |
1801 | 3 | int default_v = FLAGS_v; |
1802 | 3 | do { |
1803 | 3 | site->v() = default_v; |
1804 | 3 | if (module_list) { |
1805 | 0 | module_list->find_verbose_level( |
1806 | 0 | site->module(), site->full_module(), &site->v()); |
1807 | 0 | } |
1808 | 3 | } while (vlog_site_list_add(site, &module_list, &default_v) != 0); |
1809 | 3 | *v = &site->v(); |
1810 | 3 | return site->v() >= required_v; |
1811 | 3 | } |
1812 | | |
1813 | 0 | void print_vlog_sites(VLogSitePrinter* printer) { |
1814 | 0 | VLogSite* head = NULL; |
1815 | 0 | { |
1816 | 0 | BAIDU_SCOPED_LOCK(vlog_site_list_mutex); |
1817 | 0 | head = vlog_site_list; |
1818 | 0 | } |
1819 | 0 | VLogSitePrinter::Site site; |
1820 | 0 | for (const VLogSite* p = head; p; p = p->next()) { |
1821 | 0 | site.current_verbose_level = p->v(); |
1822 | 0 | site.required_verbose_level = p->required_v(); |
1823 | 0 | site.line_no = p->line_no(); |
1824 | 0 | site.full_module = p->full_module(); |
1825 | 0 | printer->print(site); |
1826 | 0 | } |
1827 | 0 | } |
1828 | | |
1829 | | // [Thread-safe] Reset FLAGS_vmodule. |
1830 | 0 | static int on_reset_vmodule(const char* vmodule) { |
1831 | | // resetting must be serialized. |
1832 | 0 | BAIDU_SCOPED_LOCK(reset_vmodule_and_v_mutex); |
1833 | | |
1834 | 0 | VModuleList* module_list = new (std::nothrow) VModuleList; |
1835 | 0 | if (NULL == module_list) { |
1836 | 0 | LOG(FATAL) << "Fail to new VModuleList"; |
1837 | 0 | return -1; |
1838 | 0 | } |
1839 | 0 | if (module_list->init(vmodule) != 0) { |
1840 | 0 | delete module_list; |
1841 | 0 | LOG(FATAL) << "Fail to init VModuleList"; |
1842 | 0 | return -1; |
1843 | 0 | } |
1844 | | |
1845 | 0 | VModuleList* old_module_list = NULL; |
1846 | 0 | VLogSite* old_vlog_site_list = NULL; |
1847 | 0 | { |
1848 | 0 | { |
1849 | 0 | BAIDU_SCOPED_LOCK(vlog_site_list_mutex); |
1850 | 0 | old_module_list = vmodule_list; |
1851 | 0 | vmodule_list = module_list; |
1852 | 0 | old_vlog_site_list = vlog_site_list; |
1853 | 0 | } |
1854 | 0 | for (VLogSite* p = old_vlog_site_list; p; p = p->next()) { |
1855 | 0 | p->v() = FLAGS_v; |
1856 | 0 | module_list->find_verbose_level( |
1857 | 0 | p->module(), p->full_module(), &p->v()); |
1858 | 0 | } |
1859 | 0 | } |
1860 | | |
1861 | 0 | if (old_module_list) { |
1862 | | //delay the deletion. |
1863 | 0 | if (NULL == deleting_vmodule_list) { |
1864 | 0 | deleting_vmodule_list = |
1865 | 0 | new std::deque<std::pair<VModuleList*, int64_t> >; |
1866 | 0 | } |
1867 | 0 | deleting_vmodule_list->push_back( |
1868 | 0 | std::make_pair(old_module_list, |
1869 | 0 | butil::gettimeofday_us() + DELAY_DELETION_SEC * 1000000L)); |
1870 | 0 | while (!deleting_vmodule_list->empty() && |
1871 | 0 | deleting_vmodule_list->front().second <= butil::gettimeofday_us()) { |
1872 | 0 | delete deleting_vmodule_list->front().first; |
1873 | 0 | deleting_vmodule_list->pop_front(); |
1874 | 0 | } |
1875 | 0 | } |
1876 | 0 | return 0; |
1877 | 0 | } |
1878 | | |
1879 | 0 | static bool validate_vmodule(const char*, const std::string& vmodule) { |
1880 | 0 | return on_reset_vmodule(vmodule.c_str()) == 0; |
1881 | 0 | } |
1882 | | |
1883 | | const bool ALLOW_UNUSED validate_vmodule_dummy = GFLAGS_NS::RegisterFlagValidator( |
1884 | | &FLAGS_vmodule, &validate_vmodule); |
1885 | | |
1886 | | // [Thread-safe] Reset FLAGS_v. |
1887 | 0 | static void on_reset_verbose(int default_v) { |
1888 | 0 | VModuleList* cur_module_list = NULL; |
1889 | 0 | VLogSite* cur_vlog_site_list = NULL; |
1890 | 0 | { |
1891 | | // resetting must be serialized. |
1892 | 0 | BAIDU_SCOPED_LOCK(reset_vmodule_and_v_mutex); |
1893 | 0 | { |
1894 | 0 | BAIDU_SCOPED_LOCK(vlog_site_list_mutex); |
1895 | 0 | cur_module_list = vmodule_list; |
1896 | 0 | cur_vlog_site_list = vlog_site_list; |
1897 | 0 | } |
1898 | 0 | for (VLogSite* p = cur_vlog_site_list; p; p = p->next()) { |
1899 | 0 | p->v() = default_v; |
1900 | 0 | if (cur_module_list) { |
1901 | 0 | cur_module_list->find_verbose_level( |
1902 | 0 | p->module(), p->full_module(), &p->v()); |
1903 | 0 | } |
1904 | 0 | } |
1905 | 0 | } |
1906 | 0 | } |
1907 | | |
1908 | 0 | static bool validate_v(const char*, int32_t v) { |
1909 | 0 | on_reset_verbose(v); |
1910 | 0 | return true; |
1911 | 0 | } |
1912 | | |
1913 | | const bool ALLOW_UNUSED validate_v_dummy = GFLAGS_NS::RegisterFlagValidator( |
1914 | | &FLAGS_v, &validate_v); |
1915 | | |
1916 | 0 | static bool PassValidate(const char*, bool) { |
1917 | 0 | return true; |
1918 | 0 | } |
1919 | | |
1920 | | const bool ALLOW_UNUSED validate_crash_on_fatal_log = |
1921 | | GFLAGS_NS::RegisterFlagValidator(&FLAGS_crash_on_fatal_log, PassValidate); |
1922 | | |
1923 | | const bool ALLOW_UNUSED validate_print_stack_on_check = |
1924 | | GFLAGS_NS::RegisterFlagValidator(&FLAGS_print_stack_on_check, PassValidate); |
1925 | | |
1926 | 0 | static bool NonNegativeInteger(const char*, int32_t v) { |
1927 | 0 | return v >= 0; |
1928 | 0 | } |
1929 | | |
1930 | | const bool ALLOW_UNUSED validate_min_log_level = GFLAGS_NS::RegisterFlagValidator( |
1931 | | &FLAGS_minloglevel, NonNegativeInteger); |
1932 | | |
1933 | | } // namespace logging |
1934 | | |
1935 | 0 | std::ostream& operator<<(std::ostream& out, const wchar_t* wstr) { |
1936 | 0 | return out << butil::WideToUTF8(std::wstring(wstr)); |
1937 | 0 | } |
1938 | | |
1939 | | #endif // BRPC_WITH_GLOG |