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
@@silent =
false
@@logfile =
nil
@@thread =
nil

Class Method Summary collapse

Class Method Details

.debug(message) ⇒ Object

Log a debug message.


52
53
54
55
56
# File 'lib/bettercap/logger.rb', line 52

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.


37
38
39
# File 'lib/bettercap/logger.rb', line 37

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

.info(message) ⇒ Object

Log an information message.


42
43
44
# File 'lib/bettercap/logger.rb', line 42

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

.init(debug, logfile, silent) ⇒ Object

Initialize the logging system. If debug is true, debug logging will be enabled. If logfile is not nil, every message will be saved to that file. If silent is true, all messages will be suppressed if they're not errors or warnings.


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

def init( debug, logfile, silent )
  @@debug   = debug
  @@logfile = logfile
  @@thread  = Thread.new { worker }
  @@silent  = silent
  @@ctx     = Context.get
end

.raw(message) ⇒ Object

Log a message as it is.


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

def raw(message)
  @@queue.push( message )
end

.wait!Object

Wait for the messages queue to be empty.


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

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.


47
48
49
# File 'lib/bettercap/logger.rb', line 47

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