Class: Presence::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/presence/listeners/logger.rb

Overview

Scanner listener that sends descriptions of scan events to a log. By default, it uses a Logger instance that writes to stdout. To enable more verbose output, set:

logger.log.level = ::Logger::INFO

or:

logger.log.level = ::Logger::DEBUG

To write to a file or some other outlet, pass a Ruby Logger instance to the constructor.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(log = nil) ⇒ Logger

Returns a new instance of Logger.



19
20
21
22
23
24
25
26
27
28
# File 'lib/presence/listeners/logger.rb', line 19

def initialize(log = nil)
  if log.nil?
    log = ::Logger.new($stdout)
    log.level = ::Logger::WARN
    log.formatter = proc do |severity, datetime, progname, msg|
      "#{msg}\n"
    end
  end
  self.log = log
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_sym, *arguments, &block) ⇒ Object (protected)

Always log events, even if there is no explicit handler. Generate a method at runtime so we don’t have to do method_missing every time.



73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/presence/listeners/logger.rb', line 73

def method_missing(method_sym, *arguments, &block)
  arg_names = arguments.each_with_index.map { |arg, i| "arg#{i}" }
  arg_list = arg_names.join(',')
  instance_eval <<-RUBY
    def #{method_sym}(#{arg_list})
      log_unknown_event(:#{method_sym}, #{arg_list})
    end
  RUBY
  # def method_sym(arg1, arg2, ...)
  #   log_unknown_event(:method_sym, arg1, arg2, ...)
  # end
  send(method_sym, *arguments)
end

Instance Attribute Details

#logObject

Returns the value of attribute log.



17
18
19
# File 'lib/presence/listeners/logger.rb', line 17

def log
  @log
end

Instance Method Details

#ip_scanned(ip, cmd, result) ⇒ Object



42
43
44
45
46
47
# File 'lib/presence/listeners/logger.rb', line 42

def ip_scanned(ip, cmd, result)
  log.debug " - Checked #{ip} with #{cmd}"
  result.split("\n").each do |l|
    log.debug "    #{l}"
  end
end

#listener_registered(listener, scanner) ⇒ Object



30
31
32
# File 'lib/presence/listeners/logger.rb', line 30

def listener_registered(listener, scanner)
  log.info "Registered listener: <#{listener.class}> for: #{scanner}"
end

#localhost_found(ip, mac) ⇒ Object



49
50
51
# File 'lib/presence/listeners/logger.rb', line 49

def localhost_found(ip, mac)
  log.debug " * Found localhost!"
end

#mac_found(ip, mac) ⇒ Object



53
54
55
# File 'lib/presence/listeners/logger.rb', line 53

def mac_found(ip, mac)
  log.debug " * Found #{mac} at #{ip}"
end

#mac_not_found(ip) ⇒ Object



57
58
59
# File 'lib/presence/listeners/logger.rb', line 57

def mac_not_found(ip)
  log.debug " * No MAC found at #{ip}"
end

#scan_finished(ip_prefix, range) ⇒ Object



38
39
40
# File 'lib/presence/listeners/logger.rb', line 38

def scan_finished(ip_prefix, range)
  log.info "Scan finished."
end

#scan_started(ip_prefix, range) ⇒ Object



34
35
36
# File 'lib/presence/listeners/logger.rb', line 34

def scan_started(ip_prefix, range)
  log.info "Scanning range: #{ip_prefix}.#{range.first} to #{ip_prefix}.#{range.last}"
end