Class: Console::Terminal::Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/console/terminal/logger.rb

Constant Summary collapse

UNKNOWN =
:unknown

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io = $stderr, verbose: nil, start_at: Terminal.start_at!, format: nil, **options) ⇒ Logger

Returns a new instance of Logger.



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/console/terminal/logger.rb', line 46

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]
	
	@terminal[:annotation] = @terminal.reset
	@terminal[:value] = @terminal.style(:blue)
	
	self.register_defaults(@terminal)
end

Instance Attribute Details

#ioObject (readonly)

Returns the value of attribute io.



72
73
74
# File 'lib/console/terminal/logger.rb', line 72

def io
  @io
end

#startObject (readonly)

Returns the value of attribute start.



76
77
78
# File 'lib/console/terminal/logger.rb', line 76

def start
  @start
end

#terminalObject (readonly)

Returns the value of attribute terminal.



77
78
79
# File 'lib/console/terminal/logger.rb', line 77

def terminal
  @terminal
end

#verboseObject

Returns the value of attribute verbose.



74
75
76
# File 'lib/console/terminal/logger.rb', line 74

def verbose
  @verbose
end

Instance Method Details

#call(subject = nil, *arguments, name: nil, severity: UNKNOWN, **options, &block) ⇒ Object



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/console/terminal/logger.rb', line 92

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}| ")
	
	format_subject(severity, prefix, subject, buffer)
	
	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
	
	if options&.any?
		format_options(options, buffer)
	end
	
	@io.write buffer.string
end

#register_defaults(terminal) ⇒ Object



83
84
85
86
87
88
# File 'lib/console/terminal/logger.rb', line 83

def register_defaults(terminal)
	Event.constants.each do |constant|
		klass = Event.const_get(constant)
		klass.register(terminal)
	end
end

#verbose!(value = true) ⇒ Object



79
80
81
# File 'lib/console/terminal/logger.rb', line 79

def verbose!(value = true)
	@verbose = value
end