Module: Kernel

Defined in:
lib/jets/core_ext/kernel.rb

Overview

Works with io.rb IO.flush # flush output and write to disk for node shim

Constant Summary collapse

JETS_OUTPUT =
"/tmp/jets-output.log"
OVERRIDE_METHODS =

List from ruby-doc.org/core-2.5.1/Kernel.html Note, will lose pp format in the @io_buffer but looks like a lot of work to keep the pp format. Must override stdout which can be messy quick: www.ruby-forum.com/topic/43725

%w[
  p
  pp
  print
  printf
  putc
  puts
]
@@io_buffer =
[]

Instance Method Summary collapse

Instance Method Details

#io_bufferObject



37
38
39
# File 'lib/jets/core_ext/kernel.rb', line 37

def io_buffer
  @@io_buffer
end

#io_flushObject

Note: Writing binary data to the log will crash the process with an error like this:

jets/lib/jets/core_ext/kernel.rb:20:in `write': "\x89" from ASCII-8BIT to UTF-8 (Encoding::UndefinedConversionError)

Rescue and discard it to keep the process alive.



44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/jets/core_ext/kernel.rb', line 44

def io_flush
  chunk = @@io_buffer.join("\n")
  chunk += "\n" unless chunk == ''
  begin
    # since we always append to the file, the node shim is responsible for truncating the file
    IO.write(JETS_OUTPUT, chunk, mode: 'a') if chunk
  # Writing to log with binary content will crash the process so rescuing it and writing an info message.
  rescue Encoding::UndefinedConversionError
    IO.write(JETS_OUTPUT, "[BINARY DATA]\n", mode: 'a')
  end
  @@io_buffer = []
end