Class: RubyJobs::Logging::ProgressLogger

Inherits:
PlainLogger show all
Defined in:
lib/logging/progress_logger.rb

Constant Summary collapse

PATH =
"log/progress"

Constants inherited from PlainLogger

RubyJobs::Logging::PlainLogger::DEFAULT_PATH

Instance Attribute Summary collapse

Attributes inherited from PlainLogger

#extension, #path, #print_timestamp, #separator, #time_prefix

Instance Method Summary collapse

Methods inherited from PlainLogger

#def_puts, #exist?, #file, #file_name, #file_path, #log_dir, #puts, #timestamp

Constructor Details

#initialize(*names) ⇒ ProgressLogger

Returns a new instance of ProgressLogger.



6
7
8
9
10
11
12
13
14
# File 'lib/logging/progress_logger.rb', line 6

def initialize(*names)
  super
  @path = PATH
  @start = 0.0
  @end = 100.0
  @progress = 0.0
  @progress_key = :count
  @print_timestamp = true
end

Instance Attribute Details

#endObject

Returns the value of attribute end.



17
18
19
# File 'lib/logging/progress_logger.rb', line 17

def end
  @end
end

#progressObject

Returns the value of attribute progress.



16
17
18
# File 'lib/logging/progress_logger.rb', line 16

def progress
  @progress
end

#progress_keyObject

Returns the value of attribute progress_key.



16
17
18
# File 'lib/logging/progress_logger.rb', line 16

def progress_key
  @progress_key
end

#startObject

Returns the value of attribute start.



17
18
19
# File 'lib/logging/progress_logger.rb', line 17

def start
  @start
end

Instance Method Details

#log(progress, *message) {|@progress, message| ... } ⇒ Object

Yields:



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

def log(progress, *message)
  if progress.is_a?(Hash) && progress.include?(@progress_key)
    @progress = (progress[@progress_key].to_f - @start) / (@end - @start) * 100.0
    message = ([progress] + message)
  elsif !progress.is_a?(String) && progress.respond_to?(:to_f)
    @progress = (progress.to_f - @start) / (@end - @start) * 100.0
    message = [progress] if message.empty?
  else
    message = ([progress] + message)
  end

  if @print_timestamp
    @print_timestamp = false
    messsage = super @progress.to_i, "%", timestamp, "-", *message
    @print_timestamp = true
  else
    messsage = super @progress.to_i, "%", *message
  end

  yield @progress, message if block_given?

  messsage
end