File: test_net_log_manager.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (107 lines) | stat: -rw-r--r-- 3,607 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "net/test/test_net_log_manager.h"

#include "base/command_line.h"
#include "base/files/file.h"
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "net/log/file_net_log_observer.h"
#include "net/log/net_log.h"
#include "net/log/net_log_util.h"

namespace net {

// A simple NetLog::ThreadSafeObserver that dumps NetLog entries to LOG.
class TestNetLogManager::LogNetLogObserver : public NetLog::ThreadSafeObserver {
 public:
  LogNetLogObserver(NetLog* net_log, NetLogCaptureMode capture_mode)
      : net_log_(net_log) {
    net_log_->AddObserver(this, capture_mode);
  }

  LogNetLogObserver(const LogNetLogObserver&) = delete;
  LogNetLogObserver& operator=(const LogNetLogObserver&) = delete;

  ~LogNetLogObserver() override { net_log_->RemoveObserver(this); }

  void OnAddEntry(const NetLogEntry& entry) override {
    LOG(ERROR) << "NetLog: id=" << entry.source.id
               << " source=" << NetLog::SourceTypeToString(entry.source.type)
               << "\n"
               << "event=" << NetLogEventTypeToString(entry.type)
               << " phase=" << NetLog::EventPhaseToString(entry.phase) << "\n"
               << entry.params.DebugString();
  }

 private:
  const raw_ptr<NetLog> net_log_;
};

TestNetLogManager::TestNetLogManager(NetLog* net_log,
                                     NetLogCaptureMode capture_mode) {
  if (base::CommandLine::ForCurrentProcess()->HasSwitch(kLogNetLogSwitch)) {
    Start(net_log, capture_mode);
  }
}

TestNetLogManager::~TestNetLogManager() {
  if (file_net_log_observer_) {
    base::RunLoop run_loop;
    file_net_log_observer_->StopObserving(/*polled_data=*/nullptr,
                                          run_loop.QuitClosure());
    run_loop.Run();
  }
}

void TestNetLogManager::ForceStart() {
  if (log_net_log_observer_ || file_net_log_observer_) {
    return;
  }
  Start(NetLog::Get(), NetLogCaptureMode::kEverything);
}

void TestNetLogManager::Start(NetLog* net_log, NetLogCaptureMode capture_mode) {
  const base::CommandLine* command_line =
      base::CommandLine::ForCurrentProcess();

  base::FilePath log_file_path =
      command_line->GetSwitchValuePath(kLogNetLogSwitch);
  if (log_file_path.empty()) {
    log_net_log_observer_ =
        std::make_unique<LogNetLogObserver>(net_log, capture_mode);
    return;
  }

  base::File file = base::File(
      log_file_path, base::File::FLAG_CREATE_ALWAYS | base::File::FLAG_WRITE);
  if (!file.IsValid()) {
    return;
  }

  auto constants = std::make_unique<base::Value::Dict>(GetNetConstants());
  base::Value::Dict client_info;
  client_info.Set("name", "net_unittests");
  base::CommandLine::StringType command_line_string =
      command_line->GetCommandLineString();
#if BUILDFLAG(IS_WIN)
  client_info.Set("command_line", base::WideToUTF8(command_line_string));
#else
  client_info.Set("command_line", command_line_string);
#endif
  constants->Set("clientInfo", std::move(client_info));

  file_net_log_observer_ = FileNetLogObserver::CreateUnboundedPreExisting(
      std::move(file), capture_mode, std::move(constants));
  // Try to write events to file as soon as they are added. This will record
  // events as much as possible even when a test fails with a crash.
  file_net_log_observer_->set_num_write_queue_events(1);
  file_net_log_observer_->StartObserving(net_log);
}

}  // namespace net