Sha256: 95930f46a794ed519e685049492386c2340f84e52d46c8e2a1036730327b5d6a

Contents?: true

Size: 1.49 KB

Versions: 15

Compression:

Stored size: 1.49 KB

Contents

# Works with jets/io.rb
module Kernel
  @@io_buffer = []

  # List from https://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: https://www.ruby-forum.com/topic/43725
  OVERRIDE_METHODS = %w[
    p
    pp
    print
    printf
    putc
    puts
    sprintf
  ]
  OVERRIDE_METHODS.each do |meth|
    # Example of generated code:
    #
    #   alias_method :original_puts, :puts
    #   def puts(*args, &block)
    #     @@io_buffer << args.first # message
    #     original_puts(*args, &block)
    #   end
    #
    class_eval <<~CODE
      alias_method :original_#{meth}, :#{meth}
      def #{meth}(*args, &block)
        @@io_buffer << args.first # message
        original_#{meth}(*args, &block)
      end
    CODE
  end

  def io_buffer
    @@io_buffer
  end

  # 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.
  def io_flush
    chunk = @@io_buffer.join("\n")
    begin
      IO.write("/tmp/jets-output.log", chunk)
    # Writing to log with binary content will crash the process so rescuing it and writing an info message.
    rescue Encoding::UndefinedConversionError
      IO.write("/tmp/jets-output.log", "[BINARY DATA]")
    end
    @@io_buffer = []
  end
end

Version data entries

15 entries across 15 versions & 1 rubygems

Version Path
jets-0.10.4 lib/jets/core_ext/kernel.rb
jets-0.10.3 lib/jets/core_ext/kernel.rb
jets-0.10.2 lib/jets/core_ext/kernel.rb
jets-0.10.1 lib/jets/core_ext/kernel.rb
jets-0.10.0 lib/jets/core_ext/kernel.rb
jets-0.9.2 lib/jets/core_ext/kernel.rb
jets-0.9.1 lib/jets/core_ext/kernel.rb
jets-0.9.0 lib/jets/core_ext/kernel.rb
jets-0.8.18 lib/jets/core_ext/kernel.rb
jets-0.8.17 lib/jets/core_ext/kernel.rb
jets-0.8.15 lib/jets/core_ext/kernel.rb
jets-0.8.14 lib/jets/core_ext/kernel.rb
jets-0.8.13 lib/jets/core_ext/kernel.rb
jets-0.8.12 lib/jets/core_ext/kernel.rb
jets-0.8.11 lib/jets/core_ext/kernel.rb