class Console::Capture

A buffer which captures all logged messages into a buffer.

def call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **options, &block)

@parameter buffer [IO] The (optional) buffer to write to.
@yields {|buffer| ...} A block which can be used to write additional information to the log message.
@parameter options [Hash] Additional options to pass to the log message.
@parameter event [Event] The event associated with the log message.
@parameter severity [Symbol] The severity of the log message.
@parameter arguments [Array] The arguments to the log message.
@parameter subject [Object] The subject of the log message.

Record a log message in the buffer.
def call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **options, &block)
	record = {
		time: ::Time.now.iso8601,
		severity: severity,
		**options,
	}
	
	if subject
		record[:subject] = subject
	end
	
	if event
		record[:event] = event.to_hash
	end
	
	if arguments.any?
		record[:arguments] = arguments
	end
	
	if annotation = Fiber.current.annotation
		record[:annotation] = annotation
	end
	
	if block_given?
		if block.arity.zero?
			record[:message] = yield
		else
			buffer = StringIO.new
			yield buffer
			record[:message] = buffer.string
		end
	else
		record[:message] = arguments.join(" ")
	end
	
	@records << record
end

def clear

Clear all records from the buffer.
def clear
	@records.clear
end

def each(&block)

@parameter record [Hash] The record itself.
@yields {|record| ...} each record in the buffer.

Iterate over all records in the buffer.
def each(&block)
	@records.each(&block)
end

def empty?

@returns [Boolean] True if the buffer is empty.
def empty?
	@records.empty?
end

def first

@returns [Hash] The first record in the buffer.
def first
	@records.first
end

def include?(pattern)

@returns [Boolean] True if the buffer includes any records with the given pattern.

Whether the buffer includes any records with the given subject or message pattern.
def include?(pattern)
	@records.any? do |record|
		record[:subject].to_s&.match?(pattern) or record[:message].to_s&.match?(pattern)
	end
end

def initialize

Create a new log capture buffer.
def initialize
	@records = []
	@verbose = false
end

def last

@returns [Hash] The last record in the buffer.
def last
	@records.last
end

def verbose!(value = true)

Sets the verbose flag which controls whether verbose messages are captured.
def verbose!(value = true)
	@verbose = value
end

def verbose?

@returns [Boolean] True if the buffer is capturing verbose messages.
def verbose?
	@verbose
end