Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(468)

Unified Diff: headless/lib/browser/headless_net_log.cc

Issue 2841953002: headless: Add support for net logging (Closed)
Patch Set: Created 3 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: headless/lib/browser/headless_net_log.cc
diff --git a/headless/lib/browser/headless_net_log.cc b/headless/lib/browser/headless_net_log.cc
new file mode 100644
index 0000000000000000000000000000000000000000..b7635f52fece406452e0907db2b8fa8a5d8e56c0
--- /dev/null
+++ b/headless/lib/browser/headless_net_log.cc
@@ -0,0 +1,75 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "headless/lib/browser/headless_net_log.h"
+
+#include <stdio.h>
+#include <utility>
+
+#include "base/command_line.h"
+#include "base/files/file_path.h"
+#include "base/files/scoped_file.h"
+#include "base/values.h"
+#include "build/build_config.h"
+#include "content/public/common/content_switches.h"
+#include "net/log/net_log_util.h"
+#include "net/log/write_to_file_net_log_observer.h"
+
+namespace headless {
+namespace {
+
+std::unique_ptr<base::Value> GetHeadlessConstants() {
+ std::unique_ptr<base::DictionaryValue> constants_dict =
+ net::GetNetConstants();
+
+ // Add a dictionary with client information
+ base::DictionaryValue* dict = new base::DictionaryValue();
+
+ dict->SetString("name", "headless");
+ dict->SetString(
+ "command_line",
+ base::CommandLine::ForCurrentProcess()->GetCommandLineString());
+
+ constants_dict->Set("clientInfo", dict);
+
+ return constants_dict;
+}
+
+} // namespace
+
+HeadlessNetLog::HeadlessNetLog(const base::FilePath& log_path) {
+ // TODO(mmenke): Other than a different set of constants, this code is
+ // identical to code in ChromeNetLog. Consider merging the code.
+
+ // Much like logging.h, bypass threading restrictions by using fopen
+ // directly. Have to write on a thread that's shutdown to handle events on
+ // shutdown properly, and posting events to another thread as they occur
+ // would result in an unbounded buffer size, so not much can be gained by
+ // doing this on another thread. It's only used when debugging, so
+ // performance is not a big concern.
+ base::ScopedFILE file;
+#if defined(OS_WIN)
+ file.reset(_wfopen(log_path.value().c_str(), L"w"));
+#elif defined(OS_POSIX)
+ file.reset(fopen(log_path.value().c_str(), "w"));
+#endif
+
+ if (!file) {
+ LOG(ERROR) << "Could not open file " << log_path.value()
+ << " for net logging";
+ } else {
+ std::unique_ptr<base::Value> constants(GetHeadlessConstants());
+ write_to_file_observer_.reset(new net::WriteToFileNetLogObserver());
+ write_to_file_observer_->StartObserving(this, std::move(file),
+ constants.get(), nullptr);
+ }
+}
+
+HeadlessNetLog::~HeadlessNetLog() {
+ // Remove the observer we own before we're destroyed.
+ if (write_to_file_observer_)
+ write_to_file_observer_->StopObserving(nullptr);
+}
+
+} // namespace headless

Powered by Google App Engine
This is Rietveld 408576698