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