Class: HybridPlatformsConductor::LoggerHelpers::ProgressBarLogDevice

Inherits:
Object
  • Object
show all
Defined in:
lib/hybrid_platforms_conductor/logger_helpers.rb

Overview

Small custom log device that can use a progress bar currently in use.

Instance Method Summary collapse

Constructor Details

#initialize(progress_bar, stream) ⇒ ProgressBarLogDevice

Constructor

Parameters
  • progress_bar (ProgressBar): The progress bar to be used for logging

  • stream (IO): Stream to be used for logging (like $stdoutā€¦)



19
20
21
22
23
24
# File 'lib/hybrid_platforms_conductor/logger_helpers.rb', line 19

def initialize(progress_bar, stream)
  @progress_bar = progress_bar
  @stream = stream
  # Store the current line in case it wasn't finished by \n
  @current_line = nil
end

Instance Method Details

#closeObject

Close the log device This method is needed for Ruby loggers to detect this class as a log device.



53
54
# File 'lib/hybrid_platforms_conductor/logger_helpers.rb', line 53

def close
end

#flushObject

Make sure if the current line is not flushed we still do it



57
58
59
60
61
62
# File 'lib/hybrid_platforms_conductor/logger_helpers.rb', line 57

def flush
  unless @current_line.nil?
    @stream << @current_line
    @current_line = nil
  end
end

#write(msg) ⇒ Object

Write a message

Parameters
  • msg (String): Message to log



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/hybrid_platforms_conductor/logger_helpers.rb', line 30

def write(msg)
  if msg.end_with?("\n")
    @progress_bar.clear
    if @current_line.nil?
      # New messages to be displayed
      @stream << msg
    else
      # Ending previous line
      @stream << (@current_line + msg)
      @current_line = nil
    end
    @progress_bar.refresh(force: true) unless @progress_bar.stopped?
  elsif @current_line.nil?
    # Beginning new line
    @current_line = msg
  else
    # Continuing current line
    @current_line << msg
  end
end