Class: Cline::Logs
- Inherits:
-
Object
- Object
- Cline::Logs
- 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
Public API collapse
-
#<<(line) ⇒ Logs
Add a new log line to the logs.
-
#==(other) ⇒ Boolean
Equality check.
-
#logs(from: nil) ⇒ Array<Log>
Fetch logs.
-
#monitor(on_log:, from: nil, monitoring_interval_secs: 1) { ... } ⇒ FileMonitor?
Monitor logs with a callback called when new or updated logs arrive.
-
#refresh! ⇒ Object
Clear the cache in case the log file has changed.
-
#save ⇒ Object
Save the logs lines to the file.
Instance Method Details
#<<(line) ⇒ Logs
Add a new log line to the logs
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
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
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
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 |
#save ⇒ Object
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 |