Class: OsVm::ShellLog

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path, shell_index: nil, shell_name: nil) ⇒ ShellLog

Returns a new instance of ShellLog.



3
4
5
6
7
8
9
10
11
12
# File 'lib/osvm/shell_log.rb', line 3

def initialize(path, shell_index: nil, shell_name: nil)
  @path = path
  @file = File.open(path, 'w')
  @mutex = Mutex.new

  return unless shell_name || shell_index

  file.puts("SHELL: #{shell_name || shell_index}")
  file.puts
end

Instance Attribute Details

#file ⇒ Object (readonly, protected)

Returns the value of attribute file.



56
57
58
# File 'lib/osvm/shell_log.rb', line 56

def file
  @file
end

#mutex ⇒ Object (readonly, protected)

Returns the value of attribute mutex.



56
57
58
# File 'lib/osvm/shell_log.rb', line 56

def mutex
  @mutex
end

#path ⇒ Object (readonly, protected)

Returns the value of attribute path.



56
57
58
# File 'lib/osvm/shell_log.rb', line 56

def path
  @path
end

Instance Method Details

#close ⇒ Object



50
51
52
# File 'lib/osvm/shell_log.rb', line 50

def close
  file.close
end

#execute(command, status, output) ⇒ Object



28
29
30
31
# File 'lib/osvm/shell_log.rb', line 28

def execute(command, status, output)
  begun_at = execute_begin(command)
  execute_end(status, output, begun_at)
end

#execute_begin(command) ⇒ Object



14
15
16
17
18
# File 'lib/osvm/shell_log.rb', line 14

def execute_begin(command)
  log_begin do |io|
    io.puts("COMMAND: #{command}")
  end
end

#execute_end(status, output, begun_at) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/osvm/shell_log.rb', line 20

def execute_end(status, output, begun_at)
  log_end(begun_at) do |io|
    io.puts("STATUS: #{status}")
    io.puts('OUTPUT:')
    io.puts(output)
  end
end

#execute_timeout(error, trace) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/osvm/shell_log.rb', line 33

def execute_timeout(error, trace)
  buffer = trace[:reply_buffer].to_s
  begun_at = log_begin do |io|
    io.puts('ACTION: protocol-timeout')
    io.puts("PHASE: #{trace[:failed_phase] || trace[:phase]}")
  end
  log_end(begun_at) do |io|
    io.puts("ERROR: #{error.class}: #{error.message[0, 4096]}")
    io.puts("PROTOCOL_ERROR: #{trace[:protocol_error][0, 4096]}") if trace[:protocol_error]
    io.puts("REPLY_BUFFER_BYTES: #{buffer.bytesize}")
    io.puts("REPLY_BUFFER_PREFIX: #{buffer.byteslice(0, 32_768).inspect}")
  end
rescue IOError, SystemCallError
  # An unavailable log must not replace the command's original timeout.
  nil
end

#log_begin ⇒ Object (protected)



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/osvm/shell_log.rb', line 58

def log_begin
  begun_at = Time.now

  mutex.synchronize do
    file.puts("START: #{begun_at}")
    yield(file) if block_given?
    file.flush
  end

  begun_at
end

#log_end(begun_at) ⇒ Object (protected)



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/osvm/shell_log.rb', line 70

def log_end(begun_at)
  t = Time.now

  mutex.synchronize do
    file.puts("END: #{t}")
    file.puts("ELAPSED: #{(t - begun_at).round(2)}s")
    yield(file)
    file.puts('---')
    file.puts
    file.flush
  end
end