lib/puma/events.rb in piesync-puma-3.12.6.1 vs lib/puma/events.rb in piesync-puma-5.4.0.1

- old
+ new

@@ -1,9 +1,9 @@ # frozen_string_literal: true -require 'puma/const' require "puma/null_io" +require 'puma/error_logger' require 'stringio' module Puma # The default implement of an event sink object used by Server # for when certain kinds of events occur in the life of the server. @@ -21,23 +21,19 @@ def call(str) "[#{$$}] #{str}" end end - include Const - # Create an Events object that prints to +stdout+ and +stderr+. # def initialize(stdout, stderr) @formatter = DefaultFormatter.new @stdout = stdout @stderr = stderr - @stdout.sync = true - @stderr.sync = true - @debug = ENV.key? 'PUMA_DEBUG' + @error_logger = ErrorLogger.new(@stderr) @hooks = Hash.new { |h,k| h[k] = [] } end attr_reader :stdout, :stderr @@ -64,11 +60,14 @@ end # Write +str+ to +@stdout+ # def log(str) - @stdout.puts format(str) + @stdout.puts format(str) if @stdout.respond_to? :puts + + @stdout.flush unless @stdout.sync + rescue Errno::EPIPE end def write(str) @stdout.write format(str) end @@ -78,59 +77,84 @@ end # Write +str+ to +@stderr+ # def error(str) - @stderr.puts format("ERROR: #{str}") + @error_logger.info(text: format("ERROR: #{str}")) exit 1 end def format(str) formatter.call(str) end + # An HTTP connection error has occurred. + # +error+ a connection exception, +req+ the request, + # and +text+ additional info + # @version 5.0.0 + # + def connection_error(error, req, text="HTTP connection error") + @error_logger.info(error: error, req: req, text: text) + end + # An HTTP parse error has occurred. - # +server+ is the Server object, +env+ the request, and +error+ a - # parsing exception. + # +error+ a parsing exception, + # and +req+ the request. # - def parse_error(server, env, error) - @stderr.puts "#{Time.now}: HTTP parse error, malformed request (#{env[HTTP_X_FORWARDED_FOR] || env[REMOTE_ADDR]}): #{error.inspect}\n---\n" + def parse_error(error, req) + @error_logger.info(error: error, req: req, text: 'HTTP parse error, malformed request') end # An SSL error has occurred. - # +server+ is the Server object, +peeraddr+ peer address, +peercert+ - # any peer certificate (if present), and +error+ an exception object. + # @param error <Puma::MiniSSL::SSLError> + # @param ssl_socket <Puma::MiniSSL::Socket> # - def ssl_error(server, peeraddr, peercert, error) + def ssl_error(error, ssl_socket) + peeraddr = ssl_socket.peeraddr.last rescue "<unknown>" + peercert = ssl_socket.peercert subject = peercert ? peercert.subject : nil - @stderr.puts "#{Time.now}: SSL error, peer: #{peeraddr}, peer cert: #{subject}, #{error.inspect}" + @error_logger.info(error: error, text: "SSL error, peer: #{peeraddr}, peer cert: #{subject}") end # An unknown error has occurred. - # +server+ is the Server object, +error+ an exception object, - # +kind+ some additional info, and +env+ the request. + # +error+ an exception object, +req+ the request, + # and +text+ additional info # - def unknown_error(server, error, kind="Unknown", env=nil) - if error.respond_to? :render - error.render "#{Time.now}: #{kind} error", @stderr - else - if env - string_block = [ "#{Time.now}: #{kind} error handling request { #{env['REQUEST_METHOD']} #{env['PATH_INFO']} }" ] - string_block << error.inspect - else - string_block = [ "#{Time.now}: #{kind} error: #{error.inspect}" ] - end - string_block << error.backtrace - @stderr.puts string_block.join("\n") - end + def unknown_error(error, req=nil, text="Unknown error") + @error_logger.info(error: error, req: req, text: text) end + # Log occurred error debug dump. + # +error+ an exception object, +req+ the request, + # and +text+ additional info + # @version 5.0.0 + # + def debug_error(error, req=nil, text="") + @error_logger.debug(error: error, req: req, text: text) + end + def on_booted(&block) register(:on_booted, &block) end + def on_restart(&block) + register(:on_restart, &block) + end + + def on_stopped(&block) + register(:on_stopped, &block) + end + def fire_on_booted! fire(:on_booted) + end + + def fire_on_restart! + fire(:on_restart) + end + + def fire_on_stopped! + fire(:on_stopped) end DEFAULT = new(STDOUT, STDERR) # Returns an Events object which writes its status to 2 StringIO