This commit is contained in:
@@ -0,0 +1,79 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Released under the MIT License.
|
||||
# Copyright, 2019-2024, by Samuel Williams.
|
||||
# Copyright, 2021, by Robert Schulze.
|
||||
# Copyright, 2024, by Patrik Wenger.
|
||||
|
||||
require_relative "generic"
|
||||
|
||||
module Console
|
||||
module Event
|
||||
# Represents a failure event.
|
||||
#
|
||||
# ```ruby
|
||||
# Console::Event::Failure.for(exception).emit(self)
|
||||
# ```
|
||||
class Failure < Generic
|
||||
def self.default_root
|
||||
Dir.getwd
|
||||
rescue # e.g. Errno::EMFILE
|
||||
nil
|
||||
end
|
||||
|
||||
def self.for(exception)
|
||||
self.new(exception, self.default_root)
|
||||
end
|
||||
|
||||
def self.log(subject, exception, **options)
|
||||
Console.error(subject, **self.for(exception).to_hash, **options)
|
||||
end
|
||||
|
||||
attr_reader :exception
|
||||
|
||||
def initialize(exception, root = Dir.getwd)
|
||||
@exception = exception
|
||||
@root = root
|
||||
end
|
||||
|
||||
def to_hash
|
||||
Hash.new.tap do |hash|
|
||||
hash[:type] = :failure
|
||||
hash[:root] = @root if @root
|
||||
extract(@exception, hash)
|
||||
end
|
||||
end
|
||||
|
||||
def emit(*arguments, **options)
|
||||
options[:severity] ||= :error
|
||||
|
||||
super
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def extract(exception, hash)
|
||||
hash[:class] = exception.class.name
|
||||
|
||||
if exception.respond_to?(:detailed_message)
|
||||
message = exception.detailed_message
|
||||
|
||||
# We want to remove the trailling exception class as we format it differently:
|
||||
message.sub!(/\s*\(.*?\)$/, "")
|
||||
|
||||
hash[:message] = message
|
||||
else
|
||||
hash[:message] = exception.message
|
||||
end
|
||||
|
||||
hash[:backtrace] = exception.backtrace
|
||||
|
||||
if cause = exception.cause
|
||||
hash[:cause] = Hash.new.tap do |cause_hash|
|
||||
extract(cause, cause_hash)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,26 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Released under the MIT License.
|
||||
# Copyright, 2019-2024, by Samuel Williams.
|
||||
|
||||
module Console
|
||||
module Event
|
||||
class Generic
|
||||
def as_json(...)
|
||||
to_hash
|
||||
end
|
||||
|
||||
def to_json(...)
|
||||
JSON.generate(as_json, ...)
|
||||
end
|
||||
|
||||
def to_s
|
||||
to_json
|
||||
end
|
||||
|
||||
def emit(*arguments, **options)
|
||||
Console.call(*arguments, event: self, **options)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -0,0 +1,73 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# Released under the MIT License.
|
||||
# Copyright, 2019-2024, by Samuel Williams.
|
||||
|
||||
require_relative "generic"
|
||||
require_relative "../clock"
|
||||
|
||||
module Console
|
||||
module Event
|
||||
# Represents a spawn event.
|
||||
#
|
||||
# ```ruby
|
||||
# Console.info(self, **Console::Event::Spawn.for("ls", "-l"))
|
||||
#
|
||||
# event = Console::Event::Spawn.for("ls", "-l")
|
||||
# event.status = Process.wait
|
||||
# ```
|
||||
class Spawn < Generic
|
||||
def self.for(*arguments, **options)
|
||||
# Extract out the command environment:
|
||||
if arguments.first.is_a?(Hash)
|
||||
environment = arguments.shift
|
||||
self.new(environment, arguments, options)
|
||||
else
|
||||
self.new(nil, arguments, options)
|
||||
end
|
||||
end
|
||||
|
||||
def initialize(environment, arguments, options)
|
||||
@environment = environment
|
||||
@arguments = arguments
|
||||
@options = options
|
||||
|
||||
@start_time = Clock.now
|
||||
|
||||
@end_time = nil
|
||||
@status = nil
|
||||
end
|
||||
|
||||
def duration
|
||||
if @end_time
|
||||
@end_time - @start_time
|
||||
end
|
||||
end
|
||||
|
||||
def to_hash
|
||||
Hash.new.tap do |hash|
|
||||
hash[:type] = :spawn
|
||||
hash[:environment] = @environment if @environment&.any?
|
||||
hash[:arguments] = @arguments if @arguments&.any?
|
||||
hash[:options] = @options if @options&.any?
|
||||
|
||||
hash[:status] = @status.to_i if @status
|
||||
|
||||
if duration = self.duration
|
||||
hash[:duration] = duration
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def emit(*arguments, **options)
|
||||
options[:severity] ||= :info
|
||||
super
|
||||
end
|
||||
|
||||
def status=(status)
|
||||
@end_time = Time.now
|
||||
@status = status
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user