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