Sha256: b85958f57255726f2e1abfe2d4604e0ca010078b4f369f9b897647d28024023a
Contents?: true
Size: 1.63 KB
Versions: 4
Compression:
Stored size: 1.63 KB
Contents
# frozen_string_literal: true # Released under the MIT License. # Copyright, 2019-2024, by Samuel Williams. require_relative "filter" require_relative "output/failure" module Console # A general sink which captures all events into a buffer. class Capture def initialize @records = [] @verbose = false end attr :records # @deprecated Use {#records} instead of {#buffer}. alias buffer records alias to_a records attr :verbose def include?(pattern) @records.any? do |record| record[:subject].to_s&.match?(pattern) or record[:message].to_s&.match?(pattern) end end def each(&block) @records.each(&block) end include Enumerable def first @records.first end def last @records.last end def clear @records.clear end def empty? @records.empty? end def verbose!(value = true) @verbose = value end def verbose? @verbose end 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 end end
Version data entries
4 entries across 4 versions & 1 rubygems
Version | Path |
---|---|
console-1.29.3 | lib/console/capture.rb |
console-1.29.2 | lib/console/capture.rb |
console-1.29.1 | lib/console/capture.rb |
console-1.29.0 | lib/console/capture.rb |