init_logging.cc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. // Copyright 2019 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "fuchsia_web/common/init_logging.h"
  5. #include "base/command_line.h"
  6. #include "base/logging.h"
  7. #include "base/numerics/safe_conversions.h"
  8. #include "base/strings/string_piece.h"
  9. #include "base/strings/stringprintf.h"
  10. #include "components/version_info/version_info.h"
  11. // These values must match content/public/common/content_switches.cc so that
  12. // the values will be passed to child processes in projects that Chromium's
  13. // Content layer.
  14. constexpr char kEnableLogging[] = "enable-logging";
  15. constexpr char kLogFile[] = "log-file";
  16. bool InitLoggingFromCommandLine(const base::CommandLine& command_line) {
  17. logging::LoggingSettings settings;
  18. if (command_line.GetSwitchValueASCII(kEnableLogging) == "stderr") {
  19. settings.logging_dest = logging::LOG_TO_STDERR;
  20. } else {
  21. settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
  22. }
  23. if (command_line.HasSwitch(kLogFile)) {
  24. settings.logging_dest |= logging::LOG_TO_FILE;
  25. settings.log_file_path = command_line.GetSwitchValueASCII(kLogFile).c_str();
  26. settings.delete_old = logging::DELETE_OLD_LOG_FILE;
  27. }
  28. logging::SetLogItems(true /* Process ID */, true /* Thread ID */,
  29. true /* Timestamp */, false /* Tick count */);
  30. return logging::InitLogging(settings);
  31. }
  32. bool InitLoggingFromCommandLineDefaultingToStderrForTest(
  33. base::CommandLine* command_line) {
  34. // Set logging to stderr if not specified.
  35. if (!command_line->HasSwitch(kEnableLogging)) {
  36. command_line->AppendSwitchNative(kEnableLogging, "stderr");
  37. }
  38. return InitLoggingFromCommandLine(*command_line);
  39. }
  40. void LogComponentStartWithVersion(base::StringPiece component_name) {
  41. std::string version_string = base::StringPrintf(
  42. "Starting %.*s %s", base::saturated_cast<int>(component_name.length()),
  43. component_name.data(), version_info::GetVersionNumber().c_str());
  44. #if !defined(OFFICIAL_BUILD)
  45. version_string += " (built at " + version_info::GetLastChange() + ")";
  46. #endif // !defined(OFFICIAL_BUILD)
  47. LOG(INFO) << version_string;
  48. }