host_event_logger_win.cc 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "remoting/host/host_event_logger.h"
  5. #include <stddef.h>
  6. #include <windows.h>
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/logging.h"
  11. #include "base/memory/weak_ptr.h"
  12. #include "base/strings/utf_string_conversions.h"
  13. #include "net/base/ip_endpoint.h"
  14. #include "remoting/host/host_status_monitor.h"
  15. #include "remoting/host/host_status_observer.h"
  16. #include "remoting/host/win/remoting_host_messages.h"
  17. #include "remoting/host/win/windows_event_logger.h"
  18. #include "remoting/protocol/transport.h"
  19. namespace remoting {
  20. namespace {
  21. class HostEventLoggerWin : public HostEventLogger, public HostStatusObserver {
  22. public:
  23. HostEventLoggerWin(scoped_refptr<HostStatusMonitor> monitor,
  24. const std::string& application_name);
  25. HostEventLoggerWin(const HostEventLoggerWin&) = delete;
  26. HostEventLoggerWin& operator=(const HostEventLoggerWin&) = delete;
  27. ~HostEventLoggerWin() override;
  28. // HostStatusObserver implementation. These methods will be called from the
  29. // network thread.
  30. void OnClientAuthenticated(const std::string& signaling_id) override;
  31. void OnClientDisconnected(const std::string& signaling_id) override;
  32. void OnClientAccessDenied(const std::string& signaling_id) override;
  33. void OnClientRouteChange(const std::string& signaling_id,
  34. const std::string& channel_name,
  35. const protocol::TransportRoute& route) override;
  36. void OnHostStarted(const std::string& user_email) override;
  37. void OnHostShutdown() override;
  38. private:
  39. void LogString(WORD type, DWORD event_id, const std::string& string);
  40. void Log(WORD type, DWORD event_id, const std::vector<std::string>& strings);
  41. scoped_refptr<HostStatusMonitor> monitor_;
  42. WindowsEventLogger event_logger_;
  43. };
  44. } // namespace
  45. HostEventLoggerWin::HostEventLoggerWin(scoped_refptr<HostStatusMonitor> monitor,
  46. const std::string& application_name)
  47. : monitor_(monitor), event_logger_(application_name) {
  48. if (event_logger_.IsRegistered()) {
  49. monitor_->AddStatusObserver(this);
  50. } else {
  51. PLOG(ERROR) << "Failed to register the event source: " << application_name;
  52. }
  53. }
  54. HostEventLoggerWin::~HostEventLoggerWin() {
  55. if (event_logger_.IsRegistered()) {
  56. monitor_->RemoveStatusObserver(this);
  57. }
  58. }
  59. void HostEventLoggerWin::OnClientAuthenticated(
  60. const std::string& signaling_id) {
  61. LogString(EVENTLOG_INFORMATION_TYPE, MSG_HOST_CLIENT_CONNECTED, signaling_id);
  62. }
  63. void HostEventLoggerWin::OnClientDisconnected(const std::string& signaling_id) {
  64. LogString(EVENTLOG_INFORMATION_TYPE, MSG_HOST_CLIENT_DISCONNECTED,
  65. signaling_id);
  66. }
  67. void HostEventLoggerWin::OnClientAccessDenied(const std::string& signaling_id) {
  68. LogString(EVENTLOG_ERROR_TYPE, MSG_HOST_CLIENT_ACCESS_DENIED, signaling_id);
  69. }
  70. void HostEventLoggerWin::OnClientRouteChange(
  71. const std::string& signaling_id,
  72. const std::string& channel_name,
  73. const protocol::TransportRoute& route) {
  74. std::vector<std::string> strings(5);
  75. strings[0] = signaling_id;
  76. strings[1] = route.remote_address.address().IsValid()
  77. ? route.remote_address.ToString()
  78. : "unknown";
  79. strings[2] = route.local_address.address().IsValid()
  80. ? route.local_address.ToString()
  81. : "unknown";
  82. strings[3] = channel_name;
  83. strings[4] = protocol::TransportRoute::GetTypeString(route.type);
  84. Log(EVENTLOG_INFORMATION_TYPE, MSG_HOST_CLIENT_ROUTING_CHANGED, strings);
  85. }
  86. void HostEventLoggerWin::OnHostShutdown() {
  87. // TODO(rmsousa): Fix host shutdown to actually call this, and add a log line.
  88. }
  89. void HostEventLoggerWin::OnHostStarted(const std::string& user_email) {
  90. LogString(EVENTLOG_INFORMATION_TYPE, MSG_HOST_STARTED, user_email);
  91. }
  92. void HostEventLoggerWin::Log(WORD type,
  93. DWORD event_id,
  94. const std::vector<std::string>& strings) {
  95. if (!event_logger_.Log(type, event_id, strings)) {
  96. PLOG(ERROR) << "Failed to write an event to the event log";
  97. }
  98. }
  99. void HostEventLoggerWin::LogString(WORD type,
  100. DWORD event_id,
  101. const std::string& string) {
  102. std::vector<std::string> strings;
  103. strings.push_back(string);
  104. Log(type, event_id, strings);
  105. }
  106. // static
  107. std::unique_ptr<HostEventLogger> HostEventLogger::Create(
  108. scoped_refptr<HostStatusMonitor> monitor,
  109. const std::string& application_name) {
  110. return std::make_unique<HostEventLoggerWin>(monitor, application_name);
  111. }
  112. } // namespace remoting