class Terminal
Represents a terminal output, and formats log messages for display.
Nested
Definitions
CONSOLE_START_AT = "CONSOLE_START_AT"
The environment variable used to store the start time of the console terminal output.
def self.start_at!(env = ENV)
Exports CONSOLE_START_AT which can be used to synchronize the start times of all child processes when they log using delta time.
Implementation
def self.start_at!(env = ENV)
if time_string = env[CONSOLE_START_AT]
start_at = Time.parse(time_string) rescue nil
end
unless start_at
start_at = Time.now
env[CONSOLE_START_AT] = start_at.to_s
end
return start_at
end
def initialize(stream, verbose: nil, start_at: Terminal.start_at!, format: nil, **options)
Create a new terminal output.
Signature
-
parameter
stream
IO
The output stream.
-
parameter
verbose
Boolean
Whether to print verbose output.
-
parameter
start_at
Time
The start time of the terminal output.
-
parameter
format
Console::Terminal::Format
The format to use for terminal output.
-
parameter
options
Hash
Additional options to customize the output.
Implementation
def initialize(stream, verbose: nil, start_at: Terminal.start_at!, format: nil, **options)
@stream = stream
@start_at = start_at
@terminal = format.nil? ? Console::Terminal.for(@stream) : format.new(@stream)
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)
@formatters = {}
self.register_formatters
end
def last_output
This a final output.
Implementation
def last_output
self
end
attr :stream
Signature
-
attribute
IO
The output stream.
attr_accessor :verbose
Signature
-
attribute
Boolean
Whether to print verbose output.
attr :start
Signature
-
attribute
Time
The start time of the terminal output.
attr :terminal
Signature
-
attribute
Console::Terminal::Format
The format to use for terminal output.
def verbose!(value = true)
Set the verbose output.
Signature
-
parameter
value
Boolean
Whether to print verbose output.
Implementation
def verbose!(value = true)
@verbose = value
end
def register_formatters(namespace = Console::Terminal::Formatter)
Register all formatters in the given namespace.
Implementation
def register_formatters(namespace = Console::Terminal::Formatter)
namespace.constants.each do |name|
formatter = namespace.const_get(name)
@formatters[formatter::KEY] = formatter.new(@terminal)
end
end
UNKNOWN = :unknown
The default severity for log messages, if not specified.
def call(subject = nil, *arguments, name: nil, severity: UNKNOWN, event: nil, **options, &block)
Log a message with the given severity.
Signature
-
parameter
subject
String
The subject of the log message.
-
parameter
arguments
Array
The arguments to log.
-
parameter
name
String | Nil
The optional name of the log message, used as a prefix, otherwise defaults to the severity name.
-
parameter
severity
Symbol
The severity of the log message.
-
parameter
event
Hash
The event to log.
-
parameter
options
Hash
Additional options.
-
yields
{|buffer, terminal| ...}
An optional block used to generate the log message.
-
parameter
buffer
Console::Output::Terminal::Buffer
The output buffer.
-
parameter
terminal
Console::Terminal
The terminal instance.
-
parameter
Implementation
def call(subject = nil, *arguments, name: nil, severity: UNKNOWN, event: nil, **options, &block)
width = @terminal.width
prefix = build_prefix(name || severity.to_s)
indent = " " * prefix.size
buffer = Buffer.new("#{indent}| ")
indent_size = buffer.prefix.size
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 event
format_event(event, buffer, width - indent_size)
end
if options&.any?
format_options(options, buffer)
end
@stream.write buffer.string
end