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

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

- (Logger) initialize

A new instance of Logger



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/nanoc3/cli/logger.rb', line 33

def initialize
  @level = :high
  @color = $stdout.tty?

  # Try enabling color support on Windows
  begin
    require 'Win32/Console/ANSI' if RUBY_PLATFORM =~/mswin|mingw/
  rescue LoadError
    @color = false
  end
end

Instance Attribute Details

- (Boolean) color Also known as: color?

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
  @color
end

- (Symbol) level

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

- (void) file(level, action, identifier, duration = nil)

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



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nanoc3/cli/logger.rb', line 54

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

- (void) log(level, message, io = $stdout)

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



76
77
78
79
80
81
82
# File 'lib/nanoc3/cli/logger.rb', line 76

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