Module: Elected::Logging::ClassMethods

Defined in:
lib/elected/logging.rb

Instance Method Summary collapse

Instance Method Details

#debug(string) ⇒ Object



24
25
26
# File 'lib/elected/logging.rb', line 24

def debug(string)
  log string, :debug
end

#debug_var(var_name, var, meth = :inspect, level = :debug) ⇒ Object



12
13
14
15
16
17
# File 'lib/elected/logging.rb', line 12

def debug_var(var_name, var, meth = :inspect, level = :debug)
  msg   = var.nil? ? 'NIL' : var.send(meth)
  msg   = msg[0..-2] if msg[-1, 1] == "\n"
  klass = var.is_a?(Class) ? var.name : var.class.name
  Elected.logger.send level, "#{log_prefix} #{var_name} : (#{klass}:#{var.object_id}) #{msg}"
end

#error(string) ⇒ Object



36
37
38
# File 'lib/elected/logging.rb', line 36

def error(string)
  log string, :error
end

#info(string) ⇒ Object



28
29
30
# File 'lib/elected/logging.rb', line 28

def info(string)
  log string, :info
end

#log(string, type = :debug) ⇒ Object



19
20
21
22
# File 'lib/elected/logging.rb', line 19

def log(string, type = :debug)
  msg = "#{log_prefix} #{string}"
  Elected.logger.send type, msg
end

#log_prefix(length = 60) ⇒ Object



40
41
42
43
44
45
46
47
# File 'lib/elected/logging.rb', line 40

def log_prefix(length = 60)
  prefix = name
  labels = log_prefix_labels
  prefix += ".#{labels.last}" if labels
  prefix = prefix.rjust(length, ' ')[-length, length]
  prefix += ' |'
  prefix
end

#log_prefix_labelsObject



57
58
59
60
61
62
# File 'lib/elected/logging.rb', line 57

def log_prefix_labels
  caller.
    reject { |x| x.index(__FILE__) }.
    map { |x| x =~ /(.*):(.*):in `(.*)'/ ? [$1, $2, $3] : nil }.
    first
end

#run_thread_countObject



49
50
51
# File 'lib/elected/logging.rb', line 49

def run_thread_count
  Thread.list.select { |thread| thread.status == 'run' }.count
end

#thread_countObject



53
54
55
# File 'lib/elected/logging.rb', line 53

def thread_count
  Thread.list.count
end

#warn(string) ⇒ Object



32
33
34
# File 'lib/elected/logging.rb', line 32

def warn(string)
  log string, :warn
end