Class: Nanoc2::CLI::Logger

Inherits:
Object
  • Object
show all
Includes:
Singleton
Defined in:
lib/nanoc2/cli/logger.rb

Overview

Nanoc2::CLI::Logger is a singleton class responsible for generating feedback in the terminal.

Constant Summary collapse

ACTION_COLORS =
{
  :create         => "\e[1m" + "\e[32m", # bold + green
  :update         => "\e[1m" + "\e[33m", # bold + yellow
  :identical      => "\e[1m",            # bold
  :skip           => "\e[1m",            # bold
  :'not written'  => "\e[1m"             # bold
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

:nodoc:



28
29
30
31
# File 'lib/nanoc2/cli/logger.rb', line 28

def initialize # :nodoc:
  @level = :high
  @color = true
end

Instance Attribute Details

#colorObject Also known as: color?

Whether to use color in log messages or not



25
26
27
# File 'lib/nanoc2/cli/logger.rb', line 25

def color
  @color
end

#levelObject

The log level, which can be :high, :low or :off (which will log all messages, only high-priority messages, or no messages at all, respectively).



22
23
24
# File 'lib/nanoc2/cli/logger.rb', line 22

def level
  @level
end

Instance Method Details

#file(level, action, path, duration = nil) ⇒ Object

Logs a file-related action.

level

The importance of this action. Can be :high or :low.

action

The kind of file action. Can be :create, :update or :identical.

path

The path to the file the action was performed on.



41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nanoc2/cli/logger.rb', line 41

def file(level, action, path, duration=nil)
  log(
    level,
    '%s%12s%s  %s%s' % [
      color? ? ACTION_COLORS[action.to_sym] : '',
      action,
      color? ? "\e[0m" : '',
      duration.nil? ? '' : "[%2.2fs]  " % [ duration ],
      path
    ]
  )
end

#log(level, s, io = $stdout) ⇒ Object

Logs a message.

level

The importance of this message. Can be :high or :low.

s

The message to be logged.

io

The IO instance to which the message will be written. Defaults to standard output.



62
63
64
65
66
67
68
# File 'lib/nanoc2/cli/logger.rb', line 62

def log(level, s, io=$stdout)
  # Don't log when logging is disabled
  return if @level == :off

  # Log when level permits it
  io.puts(s) if (@level == :low or @level == level)
end