Class: Brut::TUI::Script::LoggingSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/brut/tui/script/logging_subscriber.rb

Overview

A subscriber that uses Ruby's logger to log messages. The purpose of this is to ensure that every bit of available information about a Script is placed somewhere for later review. This allows any output to the terminal to be more brief or user-friendly without losing information.

Instance Method Summary collapse

Constructor Details

#initialize(progname, logfile:) ⇒ LoggingSubscriber

Returns a new instance of LoggingSubscriber.



6
7
8
9
10
11
12
13
# File 'lib/brut/tui/script/logging_subscriber.rb', line 6

def initialize(progname, logfile:)
  @logger = Logger.new(logfile, progname:)
  @logger.formatter = proc { |severity, time, progname, msg|
    "#{time} - [ #{progname} ] #{severity}: #{strip_ansi(msg)}\n"
  }
  @stdout = {}
  @stderr = {}
end

Instance Method Details

#on_any_event(event) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/brut/tui/script/logging_subscriber.rb', line 70

def on_any_event(event)
  case event
  in { description: }
    @logger.info(description)
  in { message:, type: :warning }
    @logger.warn(message)
  in { message:, type: :error }
    @logger.error(message)
  in { message: }
    @logger.info(message)
  in { handler_method_name: }
    @logger.info(handler_method_name)
  else
    @logger.info(event.to_s)
  end
end

#on_command_execution_failed(command:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'lib/brut/tui/script/logging_subscriber.rb', line 47

def on_command_execution_failed(command:)
  if !@stdout[command].empty?
    @logger.info("\n#{strip_ansi(@stdout[command])}")
  end
  if !@stderr[command].empty?
    @logger.warn("\n#{strip_ansi(@stderr[command])}")
  end
  @logger.error("Command `#{command}` failed.")
  raise "DOH"
end

#on_command_execution_succeeded(command:) ⇒ Object



37
38
39
40
41
42
43
44
45
# File 'lib/brut/tui/script/logging_subscriber.rb', line 37

def on_command_execution_succeeded(command:)
  if !@stdout[command].empty?
    @logger.info("\n#{strip_ansi(@stdout[command])}")
  end
  if !@stderr[command].empty?
    @logger.warn("\n#{strip_ansi(@stderr[command])}")
  end
  @logger.info("Command `#{command}` executed successfully.")
end

#on_command_std_err(command:, output:) ⇒ Object



33
34
35
# File 'lib/brut/tui/script/logging_subscriber.rb', line 33

def on_command_std_err(command:, output:)
  @stderr[command] << output
end

#on_command_std_out(command:, output:) ⇒ Object



29
30
31
# File 'lib/brut/tui/script/logging_subscriber.rb', line 29

def on_command_std_out(command:, output:)
  @stdout[command] << output
end

#on_event_loop_started(event) ⇒ Object



15
16
17
# File 'lib/brut/tui/script/logging_subscriber.rb', line 15

def on_event_loop_started(event)
  @logger.debug("TUI Event loop started")
end

#on_exception(event) ⇒ Object



19
20
21
# File 'lib/brut/tui/script/logging_subscriber.rb', line 19

def on_exception(event)
  @logger.error("#{event.exit? ? 'FATAL' : 'non-fatal'} ExceptionEvent: #{event.exception.class}: #{event.exception.message}\n    #{event.exception.backtrace.join("\n    ")}")
end

#on_executing_command(command:) ⇒ Object



23
24
25
26
27
# File 'lib/brut/tui/script/logging_subscriber.rb', line 23

def on_executing_command(command:)
  @logger.info("Executing command `#{command}`")
  @stdout[command] = ''
  @stderr[command] = ''
end

#on_model_updatedObject



58
59
# File 'lib/brut/tui/script/logging_subscriber.rb', line 58

def on_model_updated(*)
end

#on_script_completedObject



64
65
# File 'lib/brut/tui/script/logging_subscriber.rb', line 64

def on_script_completed(*)
end

#on_script_startedObject



67
68
# File 'lib/brut/tui/script/logging_subscriber.rb', line 67

def on_script_started(*)
end

#on_tickObject



61
62
# File 'lib/brut/tui/script/logging_subscriber.rb', line 61

def on_tick(*)
end