Home | History | Annotate | Download | only in host
      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 
      5 #include "remoting/host/host_event_logger.h"
      6 
      7 #include "base/memory/scoped_ptr.h"
      8 #include "base/memory/weak_ptr.h"
      9 #include "base/strings/stringprintf.h"
     10 #include "net/base/ip_endpoint.h"
     11 #include "remoting/host/host_status_monitor.h"
     12 #include "remoting/host/host_status_observer.h"
     13 #include "remoting/protocol/transport.h"
     14 
     15 // Included here, since the #define for LOG_USER in syslog.h conflicts with the
     16 // constants in base/logging.h, and this source file should use the version in
     17 // syslog.h.
     18 #include <syslog.h>
     19 
     20 namespace remoting {
     21 
     22 namespace {
     23 
     24 class HostEventLoggerPosix : public HostEventLogger, public HostStatusObserver {
     25  public:
     26   HostEventLoggerPosix(base::WeakPtr<HostStatusMonitor> monitor,
     27                        const std::string& application_name);
     28 
     29   virtual ~HostEventLoggerPosix();
     30 
     31   // HostStatusObserver implementation.  These methods will be called from the
     32   // network thread.
     33   virtual void OnClientAuthenticated(const std::string& jid) OVERRIDE;
     34   virtual void OnClientDisconnected(const std::string& jid) OVERRIDE;
     35   virtual void OnAccessDenied(const std::string& jid) OVERRIDE;
     36   virtual void OnClientRouteChange(
     37       const std::string& jid,
     38       const std::string& channel_name,
     39       const protocol::TransportRoute& route) OVERRIDE;
     40   virtual void OnStart(const std::string& xmpp_login) OVERRIDE;
     41   virtual void OnShutdown() OVERRIDE;
     42 
     43  private:
     44   void Log(const std::string& message);
     45 
     46   base::WeakPtr<HostStatusMonitor> monitor_;
     47   std::string application_name_;
     48 
     49   DISALLOW_COPY_AND_ASSIGN(HostEventLoggerPosix);
     50 };
     51 
     52 } //namespace
     53 
     54 HostEventLoggerPosix::HostEventLoggerPosix(
     55     base::WeakPtr<HostStatusMonitor> monitor,
     56     const std::string& application_name)
     57     : monitor_(monitor),
     58       application_name_(application_name) {
     59   openlog(application_name_.c_str(), 0, LOG_USER);
     60   monitor_->AddStatusObserver(this);
     61 }
     62 
     63 HostEventLoggerPosix::~HostEventLoggerPosix() {
     64   if (monitor_.get())
     65     monitor_->RemoveStatusObserver(this);
     66   closelog();
     67 }
     68 
     69 void HostEventLoggerPosix::OnClientAuthenticated(const std::string& jid) {
     70   Log("Client connected: " + jid);
     71 }
     72 
     73 void HostEventLoggerPosix::OnClientDisconnected(const std::string& jid) {
     74   Log("Client disconnected: " + jid);
     75 }
     76 
     77 void HostEventLoggerPosix::OnAccessDenied(const std::string& jid) {
     78   Log("Access denied for client: " + jid);
     79 }
     80 
     81 void HostEventLoggerPosix::OnClientRouteChange(
     82     const std::string& jid,
     83     const std::string& channel_name,
     84     const protocol::TransportRoute& route) {
     85   Log(base::StringPrintf(
     86       "Channel IP for client: %s ip='%s' host_ip='%s' channel='%s' "
     87       "connection='%s'",
     88       jid.c_str(), route.remote_address.ToString().c_str(),
     89       route.local_address.ToString().c_str(), channel_name.c_str(),
     90       protocol::TransportRoute::GetTypeString(route.type).c_str()));
     91 }
     92 
     93 void HostEventLoggerPosix::OnShutdown() {
     94   // TODO(rmsousa): Fix host shutdown to actually call this, and add a log line.
     95 }
     96 
     97 void HostEventLoggerPosix::OnStart(const std::string& xmpp_login) {
     98   Log("Host started for user: " + xmpp_login);
     99 }
    100 
    101 void HostEventLoggerPosix::Log(const std::string& message) {
    102   syslog(LOG_USER | LOG_NOTICE, "%s", message.c_str());
    103 }
    104 
    105 // static
    106 scoped_ptr<HostEventLogger> HostEventLogger::Create(
    107     base::WeakPtr<HostStatusMonitor> monitor,
    108     const std::string& application_name) {
    109   return scoped_ptr<HostEventLogger>(
    110       new HostEventLoggerPosix(monitor, application_name));
    111 }
    112 
    113 }  // namespace remoting
    114