Class: Nanoc3::CLI::Logger

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

Overview

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

Constant Summary collapse

ACTION_COLORS =

Maps actions (‘:create`, `:update`, `:identical` and `:skip`) onto their ANSI color codes.

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



32
33
34
# File 'lib/nanoc3/cli/logger.rb', line 32

def initialize
  @level = :high
end

Instance Attribute Details

#color=(value) ⇒ Boolean (writeonly)

Returns True if color should be used, false otherwise.

Returns:

  • (Boolean)

    True if color should be used, false otherwise



30
31
32
# File 'lib/nanoc3/cli/logger.rb', line 30

def color=(value)
  @color = value
end

#levelSymbol

Returns 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).

Returns:

  • (Symbol)

    The log level



27
28
29
# File 'lib/nanoc3/cli/logger.rb', line 27

def level
  @level
end

Instance Method Details

#color?Boolean

Returns true if colors are enabled, false otherwise.

Returns:

  • (Boolean)

    true if colors are enabled, false otherwise



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

def color?
  if @color.nil?
    @color = true
    begin
      require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /mswin|mingw/
    rescue LoadError
      @color = false
    end
  end

  @color
end

#file(level, action, identifier, duration = nil) ⇒ void

This method returns an undefined value.

Logs a file-related action.

Parameters:

  • level (:high, :low)

    The importance of this action

  • action (:create, :update, :identical)

    The kind of file action

  • name (String)

    The name of the file the action was performed on



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/nanoc3/cli/logger.rb', line 59

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

#log(level, message, io = $stdout) ⇒ void

This method returns an undefined value.

Logs a message.

Parameters:

  • level (:high, :low)

    The importance of this message

  • message (String)

    The message to be logged

  • io (#puts) (defaults to: $stdout)

    The stream to which the message should be written



81
82
83
84
85
86
87
# File 'lib/nanoc3/cli/logger.rb', line 81

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

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