Class: RaceGuard::Reporters::FileReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/race_guard/reporters/file_reporter.rb

Overview

Appends one JSON line per event to a file.

Instance Method Summary collapse

Constructor Details

#initialize(path, append: true) ⇒ FileReporter

Returns a new instance of FileReporter.



9
10
11
12
13
# File 'lib/race_guard/reporters/file_reporter.rb', line 9

def initialize(path, append: true)
  @path = path
  @append = append
  @mutex = Mutex.new
end

Instance Method Details

#report(event) ⇒ Object



15
16
17
18
19
20
21
# File 'lib/race_guard/reporters/file_reporter.rb', line 15

def report(event)
  line = JSON.generate(event.to_h)
  mode = @append ? 'a' : 'w'
  @mutex.synchronize do
    File.open(@path, mode) { |f| f.puts(line) }
  end
end