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.



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

def file
  @file
end

#pathObject (readonly, protected)

Returns the value of attribute path.



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

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

#log(&block) ⇒ Object (protected)



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

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

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

Yields:



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

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:



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

def log_cont
  yield(file)
  file.flush
end

#log_end(&block) ⇒ Object (protected)



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

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