scoped_logging_settings.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright 2020 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. #ifndef BASE_TEST_SCOPED_LOGGING_SETTINGS_H_
  5. #define BASE_TEST_SCOPED_LOGGING_SETTINGS_H_
  6. #include <memory>
  7. #include "base/base_export.h"
  8. #include "base/files/file_path.h"
  9. #include "base/logging.h"
  10. #include "base/memory/raw_ptr.h"
  11. #include "build/chromeos_buildflags.h"
  12. namespace logging {
  13. class VlogInfo;
  14. // Saves the current logging settings and restores them when destroyed.
  15. // This is used by logging tests to avoid affecting later tests that
  16. // may run afterward, in the same process.
  17. // Note that this helper cannot be used when an un-named log-file is configured
  18. // via |LoggingSettings::log_file|.
  19. class BASE_EXPORT ScopedLoggingSettings {
  20. public:
  21. ScopedLoggingSettings();
  22. ~ScopedLoggingSettings();
  23. ScopedLoggingSettings(const ScopedLoggingSettings&) = delete;
  24. ScopedLoggingSettings& operator=(const ScopedLoggingSettings&) = delete;
  25. #if BUILDFLAG(IS_CHROMEOS)
  26. void SetLogFormat(LogFormat) const;
  27. #endif
  28. private:
  29. // Please keep the following fields in the same order as the corresponding
  30. // globals in //base/logging.cc
  31. const int min_log_level_;
  32. const uint32_t logging_destination_;
  33. #if BUILDFLAG(IS_CHROMEOS)
  34. const LogFormat log_format_;
  35. #endif // BUILDFLAG(IS_CHROMEOS)
  36. std::unique_ptr<base::FilePath::StringType> log_file_name_;
  37. const bool enable_process_id_;
  38. const bool enable_thread_id_;
  39. const bool enable_timestamp_;
  40. const bool enable_tickcount_;
  41. const char* const log_prefix_;
  42. const LogMessageHandlerFunction message_handler_;
  43. };
  44. // Replaces the existing VLOG config with a new one based on it
  45. // but with extra modules enabled.
  46. //
  47. // *** Using this leaks memory ***
  48. //
  49. // For thread safety, we cannot delete the VlogInfo object created by this.
  50. //
  51. // This is intended for use in testing only, e.g. in the setup of a test, enable
  52. // vlogging for modules that are of interest. This can help debug a flaky test
  53. // which cannot be reproduced locally while avoiding log-spam from the rest of
  54. // the code.
  55. //
  56. // This follows the same pattern as ScopedFeatureList, with init separate from
  57. // construction to allow easy use in test classes.
  58. //
  59. // Using this on multiple threads requires coordination, ScopedVmoduleSwitches
  60. // must be destroyed in reverse creation order.
  61. class BASE_EXPORT ScopedVmoduleSwitches {
  62. public:
  63. explicit ScopedVmoduleSwitches();
  64. // Specify which modules and levels to enable. This uses the same syntax as
  65. // the commandline flag, e.g. "file=1,dir/other_file=2".
  66. void InitWithSwitches(const std::string& vmodule_switch);
  67. ~ScopedVmoduleSwitches();
  68. private:
  69. #if BUILDFLAG(USE_RUNTIME_VLOG)
  70. // Creates a new instance of |VlogInfo| adding |vmodule_switch|.
  71. VlogInfo* CreateVlogInfoWithSwitches(const std::string& vmodule_switch);
  72. raw_ptr<VlogInfo> scoped_vlog_info_ = nullptr;
  73. raw_ptr<VlogInfo> previous_vlog_info_ = nullptr;
  74. #endif // BUILDFLAG(USE_RUNTIME_VLOG)
  75. };
  76. } // namespace logging
  77. #endif // BASE_TEST_SCOPED_LOGGING_SETTINGS_H_