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