net_log.cc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. // Copyright (c) 2015 GitHub, Inc.
  2. // Use of this source code is governed by the MIT license that can be
  3. // found in the LICENSE file.
  4. #include "brightray/browser/net_log.h"
  5. #include "base/command_line.h"
  6. #include "base/files/file_path.h"
  7. #include "base/values.h"
  8. #include "content/public/common/content_switches.h"
  9. #include "net/log/net_log_util.h"
  10. namespace brightray {
  11. namespace {
  12. std::unique_ptr<base::DictionaryValue> GetConstants() {
  13. std::unique_ptr<base::DictionaryValue> constants = net::GetNetConstants();
  14. // Adding client information to constants dictionary.
  15. auto* client_info = new base::DictionaryValue();
  16. client_info->SetString(
  17. "command_line",
  18. base::CommandLine::ForCurrentProcess()->GetCommandLineString());
  19. constants->Set("clientInfo", client_info);
  20. return constants;
  21. }
  22. } // namespace
  23. NetLog::NetLog() {
  24. }
  25. NetLog::~NetLog() {
  26. }
  27. void NetLog::StartLogging(net::URLRequestContext* url_request_context) {
  28. auto command_line = base::CommandLine::ForCurrentProcess();
  29. if (!command_line->HasSwitch(switches::kLogNetLog))
  30. return;
  31. base::FilePath log_path =
  32. command_line->GetSwitchValuePath(switches::kLogNetLog);
  33. #if defined(OS_WIN)
  34. log_file_.reset(_wfopen(log_path.value().c_str(), L"w"));
  35. #elif defined(OS_POSIX)
  36. log_file_.reset(fopen(log_path.value().c_str(), "w"));
  37. #endif
  38. if (!log_file_) {
  39. LOG(ERROR) << "Could not open file: " << log_path.value()
  40. << "for net logging";
  41. return;
  42. }
  43. std::unique_ptr<base::Value> constants(GetConstants());
  44. write_to_file_observer_.StartObserving(this,
  45. std::move(log_file_),
  46. constants.get(),
  47. url_request_context);
  48. }
  49. } // namespace brightray