Skip to content

Commit

Permalink
More WIP.
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix committed May 2, 2024
1 parent e56e532 commit 9b95554
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 41 deletions.
4 changes: 4 additions & 0 deletions lib/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,9 @@ def fatal(...)
def call(...)
Logger.instance.call(...)
end

def failure(...)
Logger.instance.failure(...)
end
end
end
6 changes: 5 additions & 1 deletion lib/console/capture.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def verbose?
@verbose
end

def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block)
def call(subject = nil, *arguments, severity: UNKNOWN, event: nil, **options, &block)
message = {
time: ::Time.now.iso8601,
severity: severity,
Expand All @@ -51,6 +51,10 @@ def call(subject = nil, *arguments, severity: UNKNOWN, **options, &block)
message[:subject] = subject
end

if event
message[:event] = event.to_hash
end

if arguments.any?
message[:arguments] = arguments
end
Expand Down
2 changes: 1 addition & 1 deletion lib/console/event/failure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def initialize(exception, root = Dir.getwd)

def to_hash
Hash.new.tap do |hash|
hash[:event] = :failure
hash[:type] = :failure
hash[:root] = @root if @root
extract(@exception, hash)
end
Expand Down
11 changes: 7 additions & 4 deletions lib/console/event/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def self.now
Process.clock_gettime(Process::CLOCK_MONOTONIC)
end

def initialize(subject, total = 0, minimum_output_duration: 0.1, output: Console, **options)
def initialize(output, subject, total = 0, minimum_output_duration: 0.1, **options)
@output = output
@subject = subject
@options = options
Expand Down Expand Up @@ -57,17 +57,20 @@ def estimated_remaining_time

def to_hash
Hash.new.tap do |hash|
hash[:event] = :progress
hash[:type] = :progress
hash[:current] = @current
hash[:total] = @total

hash[:duration] = self.duration
hash[:estimated_remaining_time] = self.estimated_remaining_time
end
end

def increment(amount = 1)
@current += amount

if output?
@output.info(@subject, self.to_s, **@options, **self)
@output.info(@subject, self.to_s, event: self.to_hash, **@options)
@last_output_time = Progress.now
end

Expand All @@ -77,7 +80,7 @@ def increment(amount = 1)
def resize(total)
@total = total

@output.call(@subject, self.to_s, **@options, **self)
@output.call(@subject, self.to_s, event: self.to_hash, **@options)
@last_output_time = Progress.now

return self
Expand Down
14 changes: 3 additions & 11 deletions lib/console/event/spawn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ module Event
# event.status = Process.wait
# ```
class Spawn < Generic
def self.for(*arguments, **options, output: Console)
def self.for(*arguments, **options)
# Extract out the command environment:
if arguments.first.is_a?(Hash)
environment = arguments.shift
Expand All @@ -27,7 +27,7 @@ def self.for(*arguments, **options, output: Console)
end
end

def initialize(environment, arguments, options, output: Console)
def initialize(environment, arguments, options)
@environment = environment
@arguments = arguments
@options = options
Expand All @@ -36,8 +36,6 @@ def initialize(environment, arguments, options, output: Console)

@end_time = nil
@status = nil

@output = output
end

def duration
Expand All @@ -48,7 +46,7 @@ def duration

def to_hash
Hash.new.tap do |hash|
hash[:event] = :spawn
hash[:type] = :spawn
hash[:environment] = @environment if @environment&.any?
hash[:arguments] = @arguments if @arguments&.any?
hash[:options] = @options if @options&.any?
Expand All @@ -61,15 +59,9 @@ def to_hash
end
end

def emit
@output.info(self, **to_hash)
end

def status=(status)
@end_time = Time.now
@status = status

self.emit
end
end
end
Expand Down
4 changes: 4 additions & 0 deletions lib/console/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,5 +81,9 @@ def error(subject, *arguments, **options, &block)

super
end

def failure(subject, exception, **options)
error(subject, event: Event::Failure.for(exception), **options)
end
end
end
15 changes: 10 additions & 5 deletions lib/console/output/terminal.rb
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ def call(subject = nil, *arguments, name: nil, severity: UNKNOWN, event: nil, **
end

if event
format_event(event)
format_event(event, buffer)
end

if options&.any?
format_options(options, buffer)
end
Expand All @@ -132,12 +134,15 @@ def call(subject = nil, *arguments, name: nil, severity: UNKNOWN, event: nil, **

protected

def format_event(event)
def format_event(event, buffer)
event = event.to_hash
type = event[:type]

if formatter = @formatters[event]

formatter.format(options, buffer, verbose: @verbose, width: width - indent_size)
if formatter = @formatters[type]
formatter.format(options, buffer, verbose: @verbose, width: width - indent_size)
else
format_value(::JSON.pretty_generate(event), buffer)
end
end

def format_options(options, output)
Expand Down
16 changes: 10 additions & 6 deletions test/console.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,12 @@ def around
expect(capture.last).to have_keys(
time: be_a(String),
severity: be == :error,
event: be == :failure,
subject: be == self,
message: be == "It failed!",
name: be == "test"
name: be == "test",
event: have_keys(
type: be == :failure,
message: be == "It failed!",
),
)
end

Expand All @@ -103,10 +105,12 @@ def around
expect(capture.last).to have_keys(
time: be_a(String),
severity: be == :error,
event: be == :failure,
subject: be == self,
message: be == "It failed!",
name: be == "test"
name: be == "test",
event: have_keys(
type: be == :failure,
message: be == "It failed!",
)
)
end
end
Expand Down
10 changes: 6 additions & 4 deletions test/console/event/progress.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,12 @@
expect(last).to have_keys(
severity: be == :info,
subject: be == "My Measurement",
event: be == :progress,
current: be == 100,
total: be == 100,
arguments: be == ["100/100 completed in 0.0s, 0.0s remaining."]
arguments: be == ["100/100 completed in 0.0s, 0.0s remaining."],
event: have_keys(
type: be == :progress,
current: be == 100,
total: be == 100,
),
)
end
end
Expand Down
14 changes: 8 additions & 6 deletions test/console/logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,14 @@
last = output.last
expect(last).to have_keys(
severity: be == :error,
event: be == :failure,
root: be_a(String),
class: be == "StandardError",
message: be == "It failed!",
backtrace: be_a(Array),
subject: be == self
subject: be == self,
event: have_keys(
type: be == :failure,
root: be_a(String),
class: be == "StandardError",
message: be == "It failed!",
backtrace: be_a(Array),
)
)
end
end
Expand Down
8 changes: 5 additions & 3 deletions test/console/output/serialized.rb
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,14 @@
let(:event) {Console::Event::Spawn.for("ls -lah")}

it "can log structured events" do
logger.call(subject, **event)
logger.call(subject, event: event)

expect(record).to have_keys(
subject: be == subject.name,
event: be == "spawn",
arguments: be == ["ls -lah"],
event: have_keys(
type: be == "spawn",
arguments: be == ["ls -lah"],
),
)
end
end
Expand Down

0 comments on commit 9b95554

Please sign in to comment.