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
# File 'lib/osvm/machine_log.rb', line 3

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

Instance Attribute Details

#fileObject (readonly, protected)

Returns the value of attribute file.



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

def file
  @file
end

#pathObject (readonly, protected)

Returns the value of attribute path.



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

def path
  @path
end

Instance Method Details

#closeObject



49
50
51
# File 'lib/osvm/machine_log.rb', line 49

def close
  file.close
end

#execute(command, status, output) ⇒ Object



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

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

#execute_begin(command) ⇒ Object



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

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

#execute_end(status, output) ⇒ Object



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

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

#exit(status) ⇒ Object



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

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

#kill(signal) ⇒ Object



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

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

#logObject (protected)



57
58
59
60
61
# File 'lib/osvm/machine_log.rb', line 57

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

#log_begin {|file| ... } ⇒ Object (protected)

Yields:



63
64
65
66
67
68
# File 'lib/osvm/machine_log.rb', line 63

def log_begin
  @log_begun_at = Time.now
  file.puts("START: #{@log_begun_at}")
  yield(file) if block_given?
  file.flush
end

#log_cont {|file| ... } ⇒ Object (protected)

Yields:



70
71
72
73
# File 'lib/osvm/machine_log.rb', line 70

def log_cont
  yield(file)
  file.flush
end

#log_end(&block) ⇒ Object (protected)



75
76
77
78
79
80
81
82
83
84
# File 'lib/osvm/machine_log.rb', line 75

def log_end(&block)
  t = Time.now
  file.puts("END: #{t}")
  file.puts("ELAPSED: #{(t - @log_begun_at).round(2)}s")
  file.flush
  log_cont(&block) if block
  file.puts('---')
  file.puts
  file.flush
end