Class: LogsExplorer::LogFile

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

Overview

%i(debug info warn error fatal unknown).each do |method_name|

  define_method(method_name) { |*_args| }
end

end

Constant Summary collapse

MAX_LINES =
1000

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path:, client_total_lines:) ⇒ LogFile

Returns a new instance of LogFile.



61
62
63
64
# File 'lib/logs_explorer.rb', line 61

def initialize(path:, client_total_lines:)
  @path = path
  @client_total_lines = client_total_lines.presence || MAX_LINES
end

Instance Attribute Details

#client_total_linesObject (readonly)

Returns the value of attribute client_total_lines.



59
60
61
# File 'lib/logs_explorer.rb', line 59

def client_total_lines
  @client_total_lines
end

#pathObject (readonly)

Returns the value of attribute path.



59
60
61
# File 'lib/logs_explorer.rb', line 59

def path
  @path
end

Instance Method Details

#amountObject



78
79
80
81
82
83
# File 'lib/logs_explorer.rb', line 78

def amount
  result = total_lines - client_total_lines.to_i
  result = MAX_LINES if result < 0
  result = MAX_LINES if result > MAX_LINES
  result
end

#readlinesObject



73
74
75
76
# File 'lib/logs_explorer.rb', line 73

def readlines
  # puts "tail -n #{amount} #{path}"
  `tail -n #{amount} #{path}`.split(/\n/)
end

#total_linesObject



66
67
68
69
70
71
# File 'lib/logs_explorer.rb', line 66

def total_lines
  @total_lines ||= begin
                     # puts "wc -l #{path}"
                     `wc -l #{path}`.split.first.to_i
                   end
end