Sha256: 0a0f9bbf2148c02f9a8c2194727ca1c5d00ab37579be39a145bd2316d0f94c79

Contents?: true

Size: 1.6 KB

Versions: 2

Compression:

Stored size: 1.6 KB

Contents

# frozen_string_literal: true

# Released under the MIT License.
# Copyright, 2019-2024, by Samuel Williams.

require_relative "filter"

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

2 entries across 2 versions & 1 rubygems

Version Path
console-1.28.1 lib/console/capture.rb
console-1.28.0 lib/console/capture.rb