logging.cc 5.5 KB

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