Module: BetterCap::Logger

Defined in:
lib/bettercap/logger.rb

Overview

Class responsible for console and file logging.

Constant Summary collapse

@@ctx =
nil
@@queue =
Queue.new
@@debug =
false
@@timestamp =
false
@@silent =
false
@@logfile =
nil
@@thread =
nil

Class Method Summary collapse

Class Method Details

.debug(message) ⇒ Object

Log a debug message.



65
66
67
68
69
# File 'lib/bettercap/logger.rb', line 65

def debug(message)
  if @@debug and not @@silent
    @@queue.push formatted_message(message, 'D').light_black
  end
end

.error(message) ⇒ Object

Log an error message.



50
51
52
# File 'lib/bettercap/logger.rb', line 50

def error(message)
  @@queue.push formatted_message(message, 'E').red
end

.exception(e) ⇒ Object

Log the exception e, if this is a beta version, log it as a warning, otherwise as a debug message.



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/bettercap/logger.rb', line 37

def exception(e)
  msg = "Exception : #{e.class}\n" +
        "Message   : #{e.message}\n" +
        "Backtrace :\n\n    #{e.backtrace.join("\n    ")}\n"

  if BetterCap::VERSION.end_with?('b')
    self.warn(msg)
  else
    self.debug(msg)
  end
end

.info(message) ⇒ Object

Log an information message.



55
56
57
# File 'lib/bettercap/logger.rb', line 55

def info(message)
  @@queue.push( formatted_message(message, 'I') ) unless @@silent
end

.init(ctx) ⇒ Object

Initialize the logging system.



26
27
28
29
30
31
32
33
# File 'lib/bettercap/logger.rb', line 26

def init( ctx )
  @@debug     = ctx.options.core.debug
  @@logfile   = ctx.options.core.logfile
  @@silent    = ctx.options.core.silent
  @@timestamp = ctx.options.core.log_timestamp
  @@ctx       = ctx
  @@thread    = Thread.new { worker }
end

.raw(message) ⇒ Object

Log a message as it is.



72
73
74
# File 'lib/bettercap/logger.rb', line 72

def raw(message)
  @@queue.push( formatted_message( message, nil ) )
end

.wait!Object

Wait for the messages queue to be empty.



77
78
79
80
81
82
83
84
85
# File 'lib/bettercap/logger.rb', line 77

def wait!
  while not @@queue.empty?
    if @@thread.nil?
      emit @@queue.pop
    else
      sleep 0.3
    end
  end
end

.warn(message) ⇒ Object

Log a warning message.



60
61
62
# File 'lib/bettercap/logger.rb', line 60

def warn(message)
  @@queue.push formatted_message(message, 'W').yellow
end