Coverage Report

Created: 2025-10-28 07:11

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