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…)



36
37
38
39
40
41
# File 'lib/hybrid_platforms_conductor/logger_helpers.rb', line 36

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.



70
71
# File 'lib/hybrid_platforms_conductor/logger_helpers.rb', line 70

def close
end

#flushObject

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



74
75
76
77
78
79
# File 'lib/hybrid_platforms_conductor/logger_helpers.rb', line 74

def flush
  return if @current_line.nil?

  @stream << @current_line
  @current_line = nil
end

#write(msg) ⇒ Object

Write a message

Parameters
  • msg (String): Message to log



47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/hybrid_platforms_conductor/logger_helpers.rb', line 47

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