Class: OsVm::MachineLog

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ MachineLog

Returns a new instance of MachineLog.



3
4
5
6
7
# File 'lib/osvm/machine_log.rb', line 3

def initialize(path)
  @path = path
  @file = File.open(path, 'w')
  @mutex = Mutex.new
end

Instance Attribute Details

#fileObject (readonly, protected)

Returns the value of attribute file.



51
52
53
# File 'lib/osvm/machine_log.rb', line 51

def file
  @file
end

#mutexObject (readonly, protected)

Returns the value of attribute mutex.



51
52
53
# File 'lib/osvm/machine_log.rb', line 51

def mutex
  @mutex
end

#pathObject (readonly, protected)

Returns the value of attribute path.



51
52
53
# File 'lib/osvm/machine_log.rb', line 51

def path
  @path
end

Instance Method Details

#closeObject



45
46
47
# File 'lib/osvm/machine_log.rb', line 45

def close
  file.close
end

#console_wait_begin(regex) ⇒ Object



31
32
33
34
35
36
# File 'lib/osvm/machine_log.rb', line 31

def console_wait_begin(regex)
  log_begin do |io|
    io.puts('ACTION: console-wait')
    io.puts("REGEXP: #{regex.inspect}")
  end
end

#console_wait_end(status, error = nil, begun_at = nil) ⇒ Object



38
39
40
41
42
43
# File 'lib/osvm/machine_log.rb', line 38

def console_wait_end(status, error = nil, begun_at = nil)
  log_end(begun_at) do |io|
    io.puts("MATCH: #{status}")
    io.puts("ERROR: #{error}") if error
  end
end

#exit(status) ⇒ Object



24
25
26
27
28
29
# File 'lib/osvm/machine_log.rb', line 24

def exit(status)
  log do |io|
    io.puts('ACTION: qemu_exit')
    io.puts("STATUS: #{status}")
  end
end

#kill(signal) ⇒ Object



17
18
19
20
21
22
# File 'lib/osvm/machine_log.rb', line 17

def kill(signal)
  log do |io|
    io.puts('ACTION: kill')
    io.puts("SIGNAL: #{signal}")
  end
end

#logObject (protected)



53
54
55
56
57
# File 'lib/osvm/machine_log.rb', line 53

def log(&)
  begun_at = log_begin
  log_cont(&)
  log_end(begun_at)
end

#log_beginObject (protected)



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

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_contObject (protected)



71
72
73
74
75
76
# File 'lib/osvm/machine_log.rb', line 71

def log_cont
  mutex.synchronize do
    yield(file)
    file.flush
  end
end

#log_end(begun_at = nil, &block) ⇒ Object (protected)



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/osvm/machine_log.rb', line 78

def log_end(begun_at = nil, &block)
  t = Time.now
  started_at = begun_at || t

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