Class: Sentry::DebugLogFile

Inherits:
Object
  • Object
show all
Defined in:
lib/sentry/transport/debug_transport.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, prefix) ⇒ DebugLogFile

Returns a new instance of DebugLogFile.



13
14
15
16
17
18
19
20
21
22
23
# File 'lib/sentry/transport/debug_transport.rb', line 13

def initialize(path, prefix)
  unless path
    @temporary_file = Tempfile.new([prefix, ".log"])
    @temporary_file.close
  end

  @path = Pathname(path || @temporary_file.path)
  @offset = 0
  @generation = nil
  FileUtils.mkdir_p(@path.dirname) unless @path.dirname.exist?
end

Instance Attribute Details

#path ⇒ Object (readonly)

Returns the value of attribute path.



11
12
13
# File 'lib/sentry/transport/debug_transport.rb', line 11

def path
  @path
end

Instance Method Details

#append(entry) ⇒ Object



25
26
27
28
29
30
31
# File 'lib/sentry/transport/debug_transport.rb', line 25

def append(entry)
  json = "#{JSON.dump(entry)}\n"
  File.open(path, "a") do |file|
    file.flock(File::LOCK_EX)
    file.write(json)
  end
end

#clear ⇒ Object



48
49
50
51
52
53
54
# File 'lib/sentry/transport/debug_transport.rb', line 48

def clear
  File.open(path, "a") do |file|
    file.flock(File::LOCK_SH)
    @offset = file.size
    @generation = file.stat.ino
  end
end

#entries ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/sentry/transport/debug_transport.rb', line 33

def entries
  return [] unless path.exist?

  lines = File.open(path, "r") do |file|
    file.flock(File::LOCK_SH)
    current_generation = file.stat.ino
    @offset = 0 if @generation != current_generation
    @generation = current_generation
    file.seek(@offset)
    file.readlines
  end

  lines.select { |line| line.end_with?("\n") }.map { |line| JSON.parse(line) }
end

#start_run ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/sentry/transport/debug_transport.rb', line 56

def start_run
  return if @temporary_file

  File.open(path, "a") do |file|
    file.flock(File::LOCK_EX)
    previous = Pathname("#{path}.1")
    previous.rename("#{path}.2") if previous.exist?
    path.rename("#{path}.1")
    FileUtils.touch(path)
  end
  @offset = 0
  @generation = nil
end