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