Class: Cline::Logs

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Serializable::File
Defined in:
lib/cline/logs.rb

Overview

Cline Logs file content

Instance Attribute Summary

Attributes included from Serializable::File

#file

Public API collapse

Instance Method Details

#<<(line) ⇒ Logs

Add a new log line to the logs

Parameters:

  • line (Log, String)

    The log entry to add (either a Log object or a raw string)

Returns:



30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/cline/logs.rb', line 30

def <<(line)
  logs_lines << (
    if line.is_a?(Log)
      line.to_cline_json
    elsif line.is_a?(Hash)
      Log.cast(line).to_cline_json
    else
      line
    end
  )
  self
end

#==(other) ⇒ Boolean

Equality check

Parameters:

  • other (Object)

    The other to check equality with

Returns:

  • (Boolean)

    True if objects are equal



92
93
94
95
# File 'lib/cline/logs.rb', line 92

def ==(other)
  other.is_a?(Logs) &&
    other.logs_lines == logs_lines
end

#logs(from: nil) ⇒ Array<Log>

Fetch logs

Parameters:

  • from (Time, String, nil) (defaults to: nil)

    The horizon (exclusive) from which we select lines. Can be one of:

    • [Time] The timestamp to get lines from
    • [String] The exact log line to start after
    • [nil] Get all log lines

Returns:

  • (Array<Log>)

    Logs list



19
20
21
# File 'lib/cline/logs.rb', line 19

def logs(from: nil)
  logs_lines_from(from).map { |line| line.start_with?('{') ? Log.of_hash(JSON.parse(line)) : line }
end

#monitor(on_log:, from: nil, monitoring_interval_secs: 1) { ... } ⇒ FileMonitor?

Monitor logs with a callback called when new or updated logs arrive

Parameters:

  • on_log (#call)

    Block called each time there is a new log.

    • Param log [Log, String] Log that has happened, either as a Log object or as a String if it is not JSON.
    • Param last [Boolean] Is this the last log fetched from the logs?
  • from (Time, String, nil) (defaults to: nil)

    The horizon (exclusive) from which we select lines (see #logs)

  • monitoring_interval_secs (Float) (defaults to: 1)

    The monitoring interval in seconds

Yields:

  • Optional code called while monitoring is in place. If used then monitoring is stopped at the end of the block's execution.

Returns:

  • (FileMonitor, nil)

    If no block has been given, return the monitor that needs to be stopped by the caller when monitoring should end.



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/cline/logs.rb', line 62

def monitor(on_log:, from: nil, monitoring_interval_secs: 1, &)
  # Keep the last log line that we have read
  last_log = from
  Logs.monitor_file_changes(
    file,
    on_change: proc do |_mtime|
      refresh!
      new_lines = logs_lines_from(last_log)
      unless new_lines.empty?
        last_idx = new_lines.size - 1
        new_lines.each.with_index do |line, idx|
          on_log.call(line.start_with?('{') ? Log.of_hash(JSON.parse(line)) : line, idx == last_idx)
        end
        last_log = new_lines.last
      end
    end,
    monitoring_interval_secs:,
    &
  )
end

#refresh!Object

Clear the cache in case the log file has changed



84
85
86
# File 'lib/cline/logs.rb', line 84

def refresh!
  @logs_lines = nil
end

#saveObject

Save the logs lines to the file



44
45
46
47
48
49
# File 'lib/cline/logs.rb', line 44

def save
  raise 'This instance has not been initialized from a file' unless file

  FileUtils.mkdir_p(::File.dirname(file))
  ::File.write(file, logs_lines.map { |line| "#{line}\n" }.join)
end