MCPcopy Create free account
hub / github.com/apache/brpc / InitializeLogFileHandle

Function InitializeLogFileHandle

src/butil/logging.cc:372–408  ·  view source on GitHub ↗

Called by logging functions to ensure that debug_file is initialized and can be used for writing. Returns false if the file could not be initialized. debug_file will be NULL in this case.

Source from the content-addressed store, hash-verified

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.
372bool InitializeLogFileHandle() {
373 if (log_file)
374 return true;
375
376 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 log_file_name = new PathString(GetDefaultLogFile());
380 }
381
382 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 if (log_file == NULL) {
401 fprintf(stderr, "Fail to fopen %s: %s", log_file_name->c_str(), berror());
402 return false;
403 }
404#endif
405 }
406
407 return true;
408}
409
410void CloseFile(FileHandle log) {
411#if defined(OS_WIN)

Callers 2

Log2FileFunction · 0.85
BaseInitLoggingImplFunction · 0.85

Calls 3

GetDefaultLogFileFunction · 0.85
berrorFunction · 0.85
c_strMethod · 0.45

Tested by

no test coverage detected