host_event_logger_posix.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // Included order is important, since the #define for LOG_USER in syslog.h
  6. // conflicts with the constants in base/logging.h, and this source file should
  7. // use the version in syslog.h.
  8. // clang-format off
  9. #include "base/logging.h"
  10. #include <syslog.h>
  11. // clang-format on
  12. #include <memory>
  13. #include "base/memory/ptr_util.h"
  14. #include "base/memory/weak_ptr.h"
  15. #include "base/strings/stringprintf.h"
  16. #include "net/base/ip_endpoint.h"
  17. #include "remoting/host/host_status_monitor.h"
  18. #include "remoting/host/host_status_observer.h"
  19. #include "remoting/protocol/transport.h"
  20. namespace remoting {
  21. namespace {
  22. class HostEventLoggerPosix : public HostEventLogger, public HostStatusObserver {
  23. public:
  24. HostEventLoggerPosix(scoped_refptr<HostStatusMonitor> monitor,
  25. const std::string& application_name);
  26. HostEventLoggerPosix(const HostEventLoggerPosix&) = delete;
  27. HostEventLoggerPosix& operator=(const HostEventLoggerPosix&) = delete;
  28. ~HostEventLoggerPosix() override;
  29. // HostStatusObserver implementation. These methods will be called from the
  30. // network thread.
  31. void OnClientAuthenticated(const std::string& signaling_id) override;
  32. void OnClientDisconnected(const std::string& signaling_id) override;
  33. void OnClientAccessDenied(const std::string& signaling_id) override;
  34. void OnClientRouteChange(const std::string& signaling_id,
  35. const std::string& channel_name,
  36. const protocol::TransportRoute& route) override;
  37. void OnHostStarted(const std::string& user_email) override;
  38. void OnHostShutdown() override;
  39. private:
  40. void Log(const std::string& message);
  41. scoped_refptr<HostStatusMonitor> monitor_;
  42. std::string application_name_;
  43. };
  44. } //namespace
  45. HostEventLoggerPosix::HostEventLoggerPosix(
  46. scoped_refptr<HostStatusMonitor> monitor,
  47. const std::string& application_name)
  48. : monitor_(monitor), application_name_(application_name) {
  49. openlog(application_name_.c_str(), 0, LOG_USER);
  50. monitor_->AddStatusObserver(this);
  51. }
  52. HostEventLoggerPosix::~HostEventLoggerPosix() {
  53. monitor_->RemoveStatusObserver(this);
  54. closelog();
  55. }
  56. void HostEventLoggerPosix::OnClientAuthenticated(
  57. const std::string& signaling_id) {
  58. Log("Client connected: " + signaling_id);
  59. }
  60. void HostEventLoggerPosix::OnClientDisconnected(
  61. const std::string& signaling_id) {
  62. Log("Client disconnected: " + signaling_id);
  63. }
  64. void HostEventLoggerPosix::OnClientAccessDenied(
  65. const std::string& signaling_id) {
  66. Log("Access denied for client: " + signaling_id);
  67. }
  68. void HostEventLoggerPosix::OnClientRouteChange(
  69. const std::string& signaling_id,
  70. const std::string& channel_name,
  71. const protocol::TransportRoute& route) {
  72. Log(base::StringPrintf(
  73. "Channel IP for client: %s ip='%s' host_ip='%s' channel='%s' "
  74. "connection='%s'",
  75. signaling_id.c_str(),
  76. route.remote_address.address().IsValid()
  77. ? route.remote_address.ToString().c_str()
  78. : "unknown",
  79. route.local_address.address().IsValid()
  80. ? route.local_address.ToString().c_str()
  81. : "unknown",
  82. channel_name.c_str(),
  83. protocol::TransportRoute::GetTypeString(route.type).c_str()));
  84. }
  85. void HostEventLoggerPosix::OnHostShutdown() {
  86. // TODO(rmsousa): Fix host shutdown to actually call this, and add a log line.
  87. }
  88. void HostEventLoggerPosix::OnHostStarted(const std::string& user_email) {
  89. Log("Host started for user: " + user_email);
  90. }
  91. void HostEventLoggerPosix::Log(const std::string& message) {
  92. syslog(LOG_USER | LOG_NOTICE, "%s", message.c_str());
  93. }
  94. // static
  95. std::unique_ptr<HostEventLogger> HostEventLogger::Create(
  96. scoped_refptr<HostStatusMonitor> monitor,
  97. const std::string& application_name) {
  98. return base::WrapUnique(new HostEventLoggerPosix(monitor, application_name));
  99. }
  100. } // namespace remoting