class Console::Terminal::Logger

Constants

UNKNOWN

Attributes

io[R]
start[R]
terminal[R]
verbose[RW]

Public Class Methods

new(io = $stderr, verbose: nil, start_at: Terminal.start_at!, format: nil, **options) click to toggle source
# File lib/console/terminal/logger.rb, line 58
def initialize(io = $stderr, verbose: nil, start_at: Terminal.start_at!, format: nil, **options)
        @io = io
        @start_at = start_at
        
        @terminal = format.nil? ? Terminal.for(io) : format.new(io)
        
        if verbose.nil?
                @verbose = !@terminal.colors?
        else
                @verbose = verbose
        end
        
        @terminal[:logger_suffix] ||= @terminal.style(:white, nil, :faint)
        @terminal[:subject] ||= @terminal.style(nil, nil, :bold)
        @terminal[:debug] = @terminal.style(:cyan)
        @terminal[:info] = @terminal.style(:green)
        @terminal[:warn] = @terminal.style(:yellow)
        @terminal[:error] = @terminal.style(:red)
        @terminal[:fatal] = @terminal[:error]
        
        self.register_defaults(@terminal)
end

Public Instance Methods

call(subject = nil, *arguments, name: nil, severity: UNKNOWN, **options) { || ... } click to toggle source
# File lib/console/terminal/logger.rb, line 101
def call(subject = nil, *arguments, name: nil, severity: UNKNOWN, **options, &block)
        prefix = build_prefix(name || severity.to_s)
        indent = " " * prefix.size
        
        buffer = Buffer.new("#{indent}| ")
        
        if subject
                format_subject(severity, prefix, subject, buffer)
        end
        
        if options&.any?
                format_options(options, buffer)
        end
        
        arguments.each do |argument|
                format_argument(argument, buffer)
        end
        
        if block_given?
                if block.arity.zero?
                        format_argument(yield, buffer)
                else
                        yield(buffer, @terminal)
                end
        end
        
        @io.write buffer.string
end
register_defaults(terminal) click to toggle source
# File lib/console/terminal/logger.rb, line 92
def register_defaults(terminal)
        Event.constants.each do |constant|
                klass = Event.const_get(constant)
                klass.register(terminal)
        end
end
verbose!(value = true) click to toggle source
# File lib/console/terminal/logger.rb, line 88
def verbose!(value = true)
        @verbose = value
end

Protected Instance Methods

build_prefix(name) click to toggle source
# File lib/console/terminal/logger.rb, line 203
def build_prefix(name)
        if @verbose
                "#{time_offset_prefix} #{name.rjust(8)}"
        else
                time_offset_prefix
        end
end
default_suffix(object = nil) click to toggle source
# File lib/console/terminal/logger.rb, line 157
def default_suffix(object = nil)
        buffer = +" #{@terminal[:logger_suffix]}"
        
        if object
                buffer << "[oid=0x#{object.object_id.to_s(16)}] "
        end
        
        buffer << "[ec=0x#{Fiber.current.object_id.to_s(16)}] [pid=#{Process.pid}] [#{::Time.now}]#{@terminal.reset}"
end
format_argument(argument, output) click to toggle source
# File lib/console/terminal/logger.rb, line 136
def format_argument(argument, output)
        case argument
        when Exception
                Event::Failure.for(argument).format(output, @terminal, @verbose)
        when Event::Generic
                argument.format(output, @terminal, @verbose)
        else
                format_value(argument, output)
        end
end
format_object_subject(severity, prefix, subject, output) click to toggle source
# File lib/console/terminal/logger.rb, line 167
def format_object_subject(severity, prefix, subject, output)
        prefix_style = @terminal[severity]
        
        if @verbose
                suffix = default_suffix(subject)
        end
        
        prefix = "#{prefix_style}#{prefix}:#{@terminal.reset} "
        
        output.puts "#{@terminal[:subject]}#{subject.class}#{@terminal.reset}#{suffix}", prefix: prefix
end
format_options(options, output) click to toggle source
# File lib/console/terminal/logger.rb, line 132
def format_options(options, output)
        format_value(options.to_json, output)
end
format_string_subject(severity, prefix, subject, output) click to toggle source
# File lib/console/terminal/logger.rb, line 179
def format_string_subject(severity, prefix, subject, output)
        prefix_style = @terminal[severity]
        
        if @verbose
                suffix = default_suffix
        end
        
        prefix = "#{prefix_style}#{prefix}:#{@terminal.reset} "
        
        output.puts "#{@terminal[:subject]}#{subject}#{@terminal.reset}#{suffix}", prefix: prefix
end
format_subject(severity, prefix, subject, buffer) click to toggle source
# File lib/console/terminal/logger.rb, line 147
def format_subject(severity, prefix, subject, buffer)
        if subject.is_a?(String)
                format_string_subject(severity, prefix, subject, buffer)
        elsif subject.is_a?(Module)
                format_string_subject(severity, prefix, subject.to_s, buffer)
        else
                format_object_subject(severity, prefix, subject, buffer)
        end
end
format_value(value, output) click to toggle source
# File lib/console/terminal/logger.rb, line 191
def format_value(value, output)
        string = value.to_s
        
        string.each_line do |line|
                output.puts "#{line}"
        end
end
time_offset_prefix() click to toggle source
# File lib/console/terminal/logger.rb, line 199
def time_offset_prefix
        Clock.formatted_duration(Time.now - @start_at).rjust(6)
end