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

#fileObject (readonly, protected)

Returns the value of attribute file.



39
40
41
# File 'lib/osvm/shell_log.rb', line 39

def file
  @file
end

#mutexObject (readonly, protected)

Returns the value of attribute mutex.



39
40
41
# File 'lib/osvm/shell_log.rb', line 39

def mutex
  @mutex
end

#pathObject (readonly, protected)

Returns the value of attribute path.



39
40
41
# File 'lib/osvm/shell_log.rb', line 39

def path
  @path
end

Instance Method Details

#closeObject



33
34
35
# File 'lib/osvm/shell_log.rb', line 33

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

#log_beginObject (protected)



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/osvm/shell_log.rb', line 41

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)



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/osvm/shell_log.rb', line 53

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