Class: NzbGetPP::Log

Inherits:
Object
  • Object
show all
Defined in:
lib/nzbgetpp/log.rb

Constant Summary collapse

LEVELS =
{
  :debug => 0,
  :detail => 1,
  :info  => 2,
  :warning => 3,
  :error => 4,
  :fatal => 5,
}
ANSI_RED =

Map the log level (LOG_LEVEL_*) to an array containing [human_name:String, colour:String(ANSI escape sequence)]

"\033[0;31m"
ANSI_RED_INVERTED =
"\033[7;31m"
ANSI_BROWN =
"\033[0;33m"
ANSI_MAGENTA =
"\033[0;35m"
ANSI_GREEN =
"\033[0;32m"
ANSI_BOLD_WHITE =
"\033[0;37m"
ANSI_NORMAL =
"\033[0m"
HUMAN_LOG_LEVELS =
{ 
  :fatal => ["FATAL", ANSI_RED_INVERTED],
  :error => ["ERROR", ANSI_RED],
  :warning => ["WARN", ANSI_MAGENTA],
  :info => ["INFO", ANSI_GREEN],
  :detail => ["DEBUG", ANSI_NORMAL],
  :debug => ["DEBUG", ANSI_BROWN],
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level = :debug, log_fn = nil) ⇒ Log

Returns a new instance of Log.



18
19
20
21
22
23
# File 'lib/nzbgetpp/log.rb', line 18

def initialize(level = :debug,
               log_fn = nil)
  self.level = level
  self.log_fn = log_fn
  open_log_fd()
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



16
17
18
# File 'lib/nzbgetpp/log.rb', line 16

def level
  @level
end

#log_fnObject

Returns the value of attribute log_fn.



15
16
17
# File 'lib/nzbgetpp/log.rb', line 15

def log_fn
  @log_fn
end

Instance Method Details

#close_log_fdObject



32
33
34
35
36
37
# File 'lib/nzbgetpp/log.rb', line 32

def close_log_fd
  if @log_fd and not @log_fd.closed?
    @log_fd.flush()
    @log_fd.close()
  end
end

#do_write_to_log(level, message) ⇒ Object



58
59
60
61
62
63
64
# File 'lib/nzbgetpp/log.rb', line 58

def do_write_to_log(level, message)
  puts("[%s] %s" % [level.to_s.upcase, message])

  if @log_fd and not @log_fd.closed?
    log_for_humans(Time.now.iso8601(), level, message)
  end
end

#log_for_humans(iso8601_timestr, log_level, log_msg) ⇒ Object



83
84
85
86
87
88
# File 'lib/nzbgetpp/log.rb', line 83

def log_for_humans(iso8601_timestr, log_level, log_msg)
  level_str, level_colour = HUMAN_LOG_LEVELS[log_level]
  @log_fd.puts("#{ANSI_BOLD_WHITE}#{iso8601_timestr}#{ANSI_NORMAL} "\
               "#{level_colour}#{"%5.5s" % level_str}#{ANSI_NORMAL} #{log_msg}")
  @log_fd.flush()
end

#open_log_fdObject



25
26
27
28
29
30
# File 'lib/nzbgetpp/log.rb', line 25

def open_log_fd
  @log_fd.close() if @log_fd and not @log_fd.closed?
  if self.log_fn
    @log_fd = File.open(self.log_fn, "a")
  end
end

#write_to_log(level, message) ⇒ Object



50
51
52
53
54
55
56
# File 'lib/nzbgetpp/log.rb', line 50

def write_to_log(level, message)
  if LEVELS[level] >= @_enum_level
    do_write_to_log(level, message)
  else
    # Toss it
  end
end