logging.cc 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright (c) 2021 Slack Technologies, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "shell/common/logging.h"
  5. #include <string>
  6. #include <string_view>
  7. #include "base/base_switches.h"
  8. #include "base/command_line.h"
  9. #include "base/environment.h"
  10. #include "base/files/file_path.h"
  11. #include "base/logging.h"
  12. #include "base/path_service.h"
  13. #include "base/strings/string_number_conversions.h"
  14. #include "chrome/common/chrome_paths.h"
  15. #include "content/public/common/content_switches.h"
  16. namespace logging {
  17. constexpr std::string_view kLogFileName{"ELECTRON_LOG_FILE"};
  18. constexpr std::string_view kElectronEnableLogging{"ELECTRON_ENABLE_LOGGING"};
  19. base::FilePath GetLogFileName(const base::CommandLine& command_line) {
  20. std::string filename = command_line.GetSwitchValueASCII(switches::kLogFile);
  21. if (filename.empty())
  22. base::Environment::Create()->GetVar(kLogFileName, &filename);
  23. if (!filename.empty())
  24. return base::FilePath::FromUTF8Unsafe(filename);
  25. auto log_filename = base::FilePath{FILE_PATH_LITERAL("electron_debug.log")};
  26. if (base::FilePath path; base::PathService::Get(chrome::DIR_LOGS, &path))
  27. return path.Append(log_filename);
  28. // error with path service, just use some default file somewhere
  29. return log_filename;
  30. }
  31. namespace {
  32. bool HasExplicitLogFile(const base::CommandLine& command_line) {
  33. std::string filename = command_line.GetSwitchValueASCII(switches::kLogFile);
  34. if (filename.empty())
  35. base::Environment::Create()->GetVar(kLogFileName, &filename);
  36. return !filename.empty();
  37. }
  38. LoggingDestination DetermineLoggingDestination(
  39. const base::CommandLine& command_line,
  40. bool is_preinit) {
  41. bool enable_logging = false;
  42. std::string logging_destination;
  43. if (command_line.HasSwitch(::switches::kEnableLogging)) {
  44. enable_logging = true;
  45. logging_destination =
  46. command_line.GetSwitchValueASCII(switches::kEnableLogging);
  47. } else {
  48. auto env = base::Environment::Create();
  49. if (env->HasVar(kElectronEnableLogging)) {
  50. enable_logging = true;
  51. env->GetVar(kElectronEnableLogging, &logging_destination);
  52. }
  53. }
  54. if (!enable_logging)
  55. return LOG_NONE;
  56. bool also_log_to_stderr = false;
  57. #if !defined(NDEBUG)
  58. std::string also_log_to_stderr_str;
  59. if (base::Environment::Create()->GetVar("ELECTRON_ALSO_LOG_TO_STDERR",
  60. &also_log_to_stderr_str) &&
  61. !also_log_to_stderr_str.empty())
  62. also_log_to_stderr = true;
  63. #endif
  64. // --enable-logging logs to stderr, --enable-logging=file logs to a file.
  65. // NB. this differs from Chromium, in which --enable-logging logs to a file
  66. // and --enable-logging=stderr logs to stderr, because that's how Electron
  67. // used to work, so in order to not break anyone who was depending on
  68. // --enable-logging logging to stderr, we preserve the old behavior by
  69. // default.
  70. // If --log-file or ELECTRON_LOG_FILE is specified along with
  71. // --enable-logging, return LOG_TO_FILE.
  72. // If we're in the pre-init phase, before JS has run, we want to avoid
  73. // logging to the default log file, which is inside the user data directory,
  74. // because we aren't able to accurately determine the user data directory
  75. // before JS runs. Instead, log to stderr unless there's an explicit filename
  76. // given.
  77. if (HasExplicitLogFile(command_line) ||
  78. (logging_destination == "file" && !is_preinit))
  79. return LOG_TO_FILE | (also_log_to_stderr ? LOG_TO_STDERR : 0);
  80. return LOG_TO_SYSTEM_DEBUG_LOG | LOG_TO_STDERR;
  81. }
  82. } // namespace
  83. void InitElectronLogging(const base::CommandLine& command_line,
  84. bool is_preinit) {
  85. const std::string process_type =
  86. command_line.GetSwitchValueASCII(::switches::kProcessType);
  87. LoggingDestination logging_dest =
  88. DetermineLoggingDestination(command_line, is_preinit);
  89. LogLockingState log_locking_state = LOCK_LOG_FILE;
  90. base::FilePath log_path;
  91. if (command_line.HasSwitch(::switches::kLoggingLevel) &&
  92. GetMinLogLevel() >= 0) {
  93. std::string log_level =
  94. command_line.GetSwitchValueASCII(::switches::kLoggingLevel);
  95. int level = 0;
  96. if (base::StringToInt(log_level, &level) && level >= 0 &&
  97. level < LOGGING_NUM_SEVERITIES) {
  98. SetMinLogLevel(level);
  99. } else {
  100. DLOG(WARNING) << "Bad log level: " << log_level;
  101. }
  102. }
  103. // Don't resolve the log path unless we need to. Otherwise we leave an open
  104. // ALPC handle after sandbox lockdown on Windows.
  105. if ((logging_dest & LOG_TO_FILE) != 0) {
  106. log_path = GetLogFileName(command_line);
  107. } else {
  108. log_locking_state = DONT_LOCK_LOG_FILE;
  109. }
  110. // On Windows, having non canonical forward slashes in log file name causes
  111. // problems with sandbox filters, see https://crbug.com/859676
  112. log_path = log_path.NormalizePathSeparators();
  113. LoggingSettings settings;
  114. settings.logging_dest = logging_dest;
  115. settings.log_file_path = log_path.value().c_str();
  116. settings.lock_log = log_locking_state;
  117. // If we're logging to an explicit file passed with --log-file, we don't want
  118. // to delete the log file on our second initialization.
  119. settings.delete_old =
  120. process_type.empty() && (is_preinit || !HasExplicitLogFile(command_line))
  121. ? DELETE_OLD_LOG_FILE
  122. : APPEND_TO_OLD_LOG_FILE;
  123. bool success = InitLogging(settings);
  124. if (!success) {
  125. PLOG(ERROR) << "Failed to init logging";
  126. }
  127. SetLogItems(true /* pid */, false, true /* timestamp */, false);
  128. }
  129. } // namespace logging