logging.cc 5.4 KB

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