Sha256: 1ed61a3849ed1e8a1657063993b18c68bb8890c48d7a870a13fbd3a6c019b369

Contents?: true

Size: 1.93 KB

Versions: 15

Compression:

Stored size: 1.93 KB

Contents

# frozen_string_literal: true

module Nanoc::CLI
  # Nanoc::CLI::Logger is a singleton class responsible for generating
  # feedback in the terminal.
  #
  # @api private
  class Logger
    # Maps actions (`:create`, `:update`, `:identical`, `:cached`, `:skip` and `:delete`)
    # onto their ANSI color codes.
    ACTION_COLORS = {
      create: "\e[32m", # green
      update: "\e[33m", # yellow
      identical: '',    # (nothing)
      cached: '',       # (nothing)
      skip: '',         # (nothing)
      delete: "\e[31m"  # red
    }.freeze

    include Singleton

    # 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).
    #
    # @return [Symbol] The log level
    attr_accessor :level

    def initialize
      @level = :high
      @mutex = Mutex.new
    end

    # Logs a file-related action.
    #
    # @param [:high, :low] level The importance of this action
    #
    # @param [:create, :update, :identical, :cached, :skip, :delete] action The kind of file action
    #
    # @param [String] name The name of the file the action was performed on
    #
    # @return [void]
    def file(level, action, name, duration = nil)
      log(
        level,
        format(
          '%s%12s%s  %s%s',
          ACTION_COLORS[action.to_sym],
          action,
          "\e[0m",
          duration.nil? ? '' : format('[%2.2fs]  ', duration),
          name,
        ),
      )
    end

    # Logs a message.
    #
    # @param [:high, :low] level The importance of this message
    #
    # @param [String] message The message to be logged
    #
    # @param [#puts] io The stream to which the message should be written
    #
    # @return [void]
    def log(level, message, io = $stdout)
      return if @level == :off
      return if @level != :low && @level != level

      @mutex.synchronize do
        io.puts(message)
      end
    end
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
nanoc-4.11.0 lib/nanoc/cli/logger.rb
nanoc-4.10.4 lib/nanoc/cli/logger.rb
nanoc-4.10.3 lib/nanoc/cli/logger.rb
nanoc-4.10.2 lib/nanoc/cli/logger.rb
nanoc-4.10.1 lib/nanoc/cli/logger.rb
nanoc-4.10.0 lib/nanoc/cli/logger.rb
nanoc-4.9.9 lib/nanoc/cli/logger.rb
nanoc-4.9.8 lib/nanoc/cli/logger.rb
nanoc-4.9.7 lib/nanoc/cli/logger.rb
nanoc-4.9.6 lib/nanoc/cli/logger.rb
nanoc-4.9.5 lib/nanoc/cli/logger.rb
nanoc-4.9.4 lib/nanoc/cli/logger.rb
nanoc-4.9.3 lib/nanoc/cli/logger.rb
nanoc-4.9.2 lib/nanoc/cli/logger.rb
nanoc-4.9.1 lib/nanoc/cli/logger.rb