class Epuber::Logger::AbstractLogger

Constants

LEVELS

Attributes

debug_steps_times[RW]

@return [Boolean]

verbose[RW]

@return [Boolean]

Public Class Methods

new(opts = {}) click to toggle source

@param [Hash] opts @option opts [Boolean] :verbose (false) @option opts [Boolean] :debug_steps_times (false)

# File lib/epuber/utils/logger/abstract_logger.rb, line 30
def initialize(opts = {})
  @verbose = opts.fetch(:verbose, false)
  @debug_steps_times = opts.fetch(:debug_steps_times, false)

  @current_file = nil
  @sticky_message = nil
  @error_was_logged = false
end

Public Instance Methods

debug(message, sticky: false, location: nil, backtrace: caller_locations) click to toggle source

Report debug message

@param [String, to_s] message @param [Thread::Backtrace::Location, Epuber::Location, Nokogiri::XML::Node, nil] location

# File lib/epuber/utils/logger/abstract_logger.rb, line 83
def debug(message, sticky: false, location: nil, backtrace: caller_locations)
  return unless @verbose

  _common_log(:debug, message, sticky: sticky, location: location, backtrace: backtrace)
end
end_processing() click to toggle source
# File lib/epuber/utils/logger/abstract_logger.rb, line 99
def end_processing
  _remove_sticky_message
  @current_file = nil
end
error(message, sticky: false, location: nil, backtrace: caller_locations) click to toggle source

Report error message

@param [String, to_s] message @param [Thread::Backtrace::Location, Epuber::Location, Nokogiri::XML::Node, nil] location @param [Boolean] stick if true, the error message will be sticked to the previous one

# File lib/epuber/utils/logger/abstract_logger.rb, line 56
def error(message, sticky: false, location: nil, backtrace: caller_locations)
  _common_log(:error, message, sticky: sticky, location: location, backtrace: backtrace)
end
error!(message, location: nil, backtrace: caller_locations) click to toggle source

Report error message

@param [String, to_s] message @param [Thread::Backtrace::Location, Epuber::Location, Nokogiri::XML::Node, nil] location @param [Boolean] stick if true, the error message will be sticked to the previous one

# File lib/epuber/utils/logger/abstract_logger.rb, line 45
def error!(message, location: nil, backtrace: caller_locations)
  _common_log(:error, message, location: location, backtrace: backtrace)
  exit(1)
end
error?() click to toggle source

Return true if there was any error logged

@return [Boolean]

# File lib/epuber/utils/logger/abstract_logger.rb, line 145
def error?
  @error_was_logged
end
info(message, sticky: false, location: nil, backtrace: caller_locations) click to toggle source

Report info message

@param [String, to_s] message @param [Thread::Backtrace::Location, Epuber::Location, Nokogiri::XML::Node, nil] location

# File lib/epuber/utils/logger/abstract_logger.rb, line 74
def info(message, sticky: false, location: nil, backtrace: caller_locations)
  _common_log(:info, message, sticky: sticky, location: location, backtrace: backtrace)
end
print_processing_debug_info(info_text) click to toggle source

@param [String] info_text

print_step_processing_time(step_name, time = nil) { || ... } click to toggle source

@param [String] step_name @param [Fixnum] time

start_processing_file(file, index, count) click to toggle source

@param [Epuber::Compiler::FileTypes::AbstractFile] file @param [Fixnum] index @param [Fixnum] count

# File lib/epuber/utils/logger/abstract_logger.rb, line 93
def start_processing_file(file, index, count)
  @current_file = file

  _common_log(:debug, "▸ Processing #{file.source_path} (#{index + 1} of #{count})", sticky: true)
end
warning(message, sticky: false, location: nil, backtrace: caller_locations) click to toggle source

Report warning message

@param [String, to_s] message @param [Thread::Backtrace::Location, Epuber::Location, Nokogiri::XML::Node, nil] location

# File lib/epuber/utils/logger/abstract_logger.rb, line 65
def warning(message, sticky: false, location: nil, backtrace: caller_locations)
  _common_log(:warning, message, sticky: sticky, location: location, backtrace: backtrace)
end

Protected Instance Methods

_log(level, message, location: nil, backtrace: nil, remove_last: false) click to toggle source

Point of implementation for concrete logger

@param [Symbol] level @param [String] message @param [Epuber::Location, nil] location @param [Array<Thread::Backtrace::Location>, nil] backtrace @param [Boolean] sticky if true, the message will be sticky (will be always as the last message)

# File lib/epuber/utils/logger/abstract_logger.rb, line 159
def _log(level, message, location: nil, backtrace: nil, remove_last: false) # rubocop:disable Lint/UnusedMethodArgument
  raise 'Not implemented, this is abstract class'
end
_remove_sticky_message() click to toggle source

Remove last line of the output (if it was sticky)

@return [String, nil] last line

# File lib/epuber/utils/logger/abstract_logger.rb, line 167
def _remove_sticky_message; end

Private Instance Methods

_common_log(level, message, sticky: false, location: nil, backtrace: nil) click to toggle source

Core logging method

@param level [Symbol] @param message [String] @param location [Thread::Backtrace::Location, Epuber::Location, Nokogiri::XML::Node,

Epuber::Compiler::FileTypes::AbstractFile, nil]

@param sticky [Boolean] if true, the message will be sticky (will be always as the last message)

# File lib/epuber/utils/logger/abstract_logger.rb, line 179
def _common_log(level, message, sticky: false, location: nil, backtrace: nil)
  raise ArgumentError, "Unknown log level #{level}" unless LEVELS.include?(level)

  @error_was_logged = true if level == :error

  location = _location_from_obj(location)

  if @verbose && level == :error
    backtrace = location.try(:backtrace_locations) ||
                message.try(:backtrace_locations) ||
                backtrace ||
                caller_locations
  end

  _log(level, message, location: location, backtrace: backtrace, sticky: sticky)
end
_location_from_obj(obj) click to toggle source

@param [Thread::Backtrace::Location, Nokogiri::XML::Node] obj

@return [Location]

# File lib/epuber/utils/logger/abstract_logger.rb, line 200
def _location_from_obj(obj)
  case obj
  when ::Thread::Backtrace::Location
    Location.new(path: obj.path, lineno: obj.lineno)
  when ::Nokogiri::XML::Node
    Location.new(path: obj.document.file_path, lineno: obj.line)
  when Location
    obj
  when Epuber::Compiler::FileTypes::AbstractFile
    Location.new(path: obj.source_path)
  end
end