Class: Nanoc3::CLI::Logger
- Inherits:
-
Object
- Object
- Nanoc3::CLI::Logger
- 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
-
#color ⇒ Boolean
writeonly
True if color should be used, false otherwise.
-
#level ⇒ Symbol
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).
Instance Method Summary collapse
-
#color? ⇒ Boolean
True if colors are enabled, false otherwise.
-
#file(level, action, identifier, duration = nil) ⇒ void
Logs a file-related action.
-
#initialize ⇒ Logger
constructor
A new instance of Logger.
-
#log(level, message, io = $stdout) ⇒ void
Logs a message.
Constructor Details
#initialize ⇒ Logger
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.
30 31 32 |
# File 'lib/nanoc3/cli/logger.rb', line 30 def color=(value) @color = value end |
#level ⇒ Symbol
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).
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.
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.
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.
81 82 83 84 85 86 87 |
# File 'lib/nanoc3/cli/logger.rb', line 81 def log(level, , io=$stdout) # Don't log when logging is disabled return if @level == :off # Log when level permits it io.puts() if (@level == :low or @level == level) end |