Sha256: 8de9a59c96e07d9d13053927b829b921789b8666d2af68ac5d5566a26fc42ad9

Contents?: true

Size: 965 Bytes

Versions: 3

Compression:

Stored size: 965 Bytes

Contents

require 'logger'
require 'stringio'
require 'ansi/code'

module Autoshell
  # Mixin for handling log stuff
  module Log
    # Instance-specific logger
    #
    #   shell = Autoshell.new '~/test'
    #   shell.logger
    #
    #   shell.logger = Rails.logger
    #
    # @return [Logger] logger instance
    def logger
      return @logger if defined? @logger

      @log_device = StringIO.new
      @logger = Logger.new(@log_device)
      @logger.level = LOG_LEVEL
      @logger.formatter = LOG_FORMATTER
      @logger
    end
    attr_writer :logger

    # Get the complete output of the log
    # @return [String] log contents
    def log_output(color: false)
      return unless defined? @log_device
      if color
        @log_device.string
      else
        ANSI.unansi(@log_device.string)
      end
    end

    # Reset the log contents
    # @return self
    def log_reset
      @log_device.truncate(0) if defined? @log_device
      self
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
autoshell-1.0.6 lib/autoshell/log.rb
autoshell-1.0.5 lib/autoshell/log.rb
autoshell-1.0.4 lib/autoshell/log.rb