Class: Console::Capture

Inherits:
Object
  • Object
show all
Includes:
Enumerable
Defined in:
lib/console/capture.rb

Overview

A buffer which captures all logged messages into a buffer.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCapture

Create a new log capture buffer.



13
14
15
16
# File 'lib/console/capture.rb', line 13

def initialize
	@records = []
	@verbose = false
end

Instance Attribute Details

#All records captured by this buffer.(recordscapturedbythisbuffer.) ⇒ Object (readonly)



19
# File 'lib/console/capture.rb', line 19

attr :records

#recordsObject (readonly) Also known as: buffer, to_a

Returns the value of attribute records.



19
20
21
# File 'lib/console/capture.rb', line 19

def records
  @records
end

#verboseObject (readonly)

Returns the value of attribute verbose.



27
28
29
# File 'lib/console/capture.rb', line 27

def verbose
  @verbose
end

Instance Method Details

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

Record a log message in the buffer.



87
88
89
90
91
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
118
119
120
121
122
123
# File 'lib/console/capture.rb', line 87

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

#clearObject

Clear all records from the buffer.



59
60
61
# File 'lib/console/capture.rb', line 59

def clear
	@records.clear
end

#each(&block) ⇒ Object

Iterate over all records in the buffer.



42
43
44
# File 'lib/console/capture.rb', line 42

def each(&block)
	@records.each(&block)
end

#empty?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'lib/console/capture.rb', line 64

def empty?
	@records.empty?
end

#firstObject



49
50
51
# File 'lib/console/capture.rb', line 49

def first
	@records.first
end

#If true, the buffer will capture verbose messages.=(true, thebufferwillcaptureverbosemessages. = (value)) ⇒ Object



27
# File 'lib/console/capture.rb', line 27

attr :verbose

#include?(pattern) ⇒ Boolean

Whether the buffer includes any records with the given subject or message pattern.

Returns:

  • (Boolean)


32
33
34
35
36
# File 'lib/console/capture.rb', line 32

def include?(pattern)
	@records.any? do |record|
		record[:subject].to_s&.match?(pattern) or record[:message].to_s&.match?(pattern)
	end
end

#lastObject



54
55
56
# File 'lib/console/capture.rb', line 54

def last
	@records.last
end

#verbose!(value = true) ⇒ Object

Sets the verbose flag which controls whether verbose messages are captured.



69
70
71
# File 'lib/console/capture.rb', line 69

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

#verbose?Boolean

Returns:

  • (Boolean)


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

def verbose?
	@verbose
end