Sha256: 87c7e7f962266487ccd831a8da798ca63de1ce440bc3b178202663267632bfcc

Contents?: true

Size: 778 Bytes

Versions: 11

Compression:

Stored size: 778 Bytes

Contents

require "benchmark"

module MiniMagick
  ##
  # Responsible for logging commands to stdout (activated when
  # `MiniMagick.debug` is set to `true`). Implements a simplified Logger
  # interface.
  #
  # @private
  #
  class Logger

    attr_accessor :format

    def initialize(io)
      @io     = io
      @format = "[%<duration>.2fs] %<command>s"
    end

    def debug(command, &action)
      benchmark(action) do |duration|
        output(duration: duration, command: command) if MiniMagick.debug
      end
    end

    private

    def output(data)
      printf @io, "#{format}\n", data
    end

    def benchmark(action)
      return_value = nil
      duration = Benchmark.realtime { return_value = action.call }
      yield duration
      return_value
    end

  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
mini_magick-4.2.3 lib/mini_magick/logger.rb
mini_magick-4.2.1 lib/mini_magick/logger.rb
mini_magick-4.2.0 lib/mini_magick/logger.rb
mini_magick-4.1.1 lib/mini_magick/logger.rb
mini_magick-4.1.0 lib/mini_magick/logger.rb
mini_magick-4.0.4 lib/mini_magick/logger.rb
mini_magick-4.0.3 lib/mini_magick/logger.rb
mini_magick-4.0.2 lib/mini_magick/logger.rb
mini_magick-4.0.1 lib/mini_magick/logger.rb
mini_magick-4.0.0 lib/mini_magick/logger.rb
mini_magick-4.0.0.rc lib/mini_magick/logger.rb