1
0
Fork 0
mirror of https://github.com/puma/puma.git synced 2022-11-09 13:48:40 -05:00
puma--puma/lib/puma/events.rb

149 lines
3.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
require "puma/null_io"
2020-05-04 14:30:18 -04:00
require 'puma/debug_logger'
2011-09-30 11:29:37 -04:00
require 'stringio'
module Puma
2011-12-01 18:23:14 -05:00
# The default implement of an event sink object used by Server
# for when certain kinds of events occur in the life of the server.
#
# The methods available are the events that the Server fires.
#
2011-09-30 11:29:37 -04:00
class Events
class DefaultFormatter
def call(str)
str
end
end
class PidFormatter
def call(str)
"[#{$$}] #{str}"
end
end
2011-09-30 11:29:37 -04:00
2011-12-01 18:23:14 -05:00
# Create an Events object that prints to +stdout+ and +stderr+.
#
2011-09-30 11:29:37 -04:00
def initialize(stdout, stderr)
@formatter = DefaultFormatter.new
@stdout = stdout
@stderr = stderr
@stdout.sync = true
@stderr.sync = true
@debug = ENV.key? 'PUMA_DEBUG'
2020-05-04 14:30:18 -04:00
@debug_logger = DebugLogger.new(@stderr)
2013-09-13 12:56:39 -04:00
@hooks = Hash.new { |h,k| h[k] = [] }
2011-09-30 11:29:37 -04:00
end
attr_reader :stdout, :stderr
attr_accessor :formatter
2011-09-30 11:29:37 -04:00
2013-09-13 12:56:39 -04:00
# Fire callbacks for the named hook
#
def fire(hook, *args)
@hooks[hook].each { |t| t.call(*args) }
end
# Register a callback for a given hook
2013-09-13 12:56:39 -04:00
#
def register(hook, obj=nil, &blk)
if obj and blk
raise "Specify either an object or a block, not both"
end
h = obj || blk
@hooks[hook] << h
h
end
2012-07-02 18:09:35 -04:00
# Write +str+ to +@stdout+
#
def log(str)
@stdout.puts format(str)
2012-07-02 18:09:35 -04:00
end
2012-08-01 13:11:27 -04:00
def write(str)
@stdout.write format(str)
2012-08-01 13:11:27 -04:00
end
def debug(str)
log("% #{str}") if @debug
end
2012-07-02 18:09:35 -04:00
# Write +str+ to +@stderr+
#
def error(str)
2020-05-09 11:04:04 -04:00
@debug_logger.error_dump(text: format("ERROR: #{str}"))
2012-07-02 18:09:35 -04:00
exit 1
end
def format(str)
formatter.call(str)
end
2020-05-09 12:03:32 -04:00
# An HTTP connection error has occurred.
# +error+ a connection exception, +env+ the request
#
def connection_error(error, env, text="HTTP connection error")
2020-05-10 11:43:42 -04:00
@debug_logger.error_dump(error: error, env: env, text: text)
2020-05-09 12:03:32 -04:00
end
2015-07-15 10:59:34 -04:00
# An HTTP parse error has occurred.
2020-05-09 12:03:32 -04:00
# +env+ the request, and +error+ a
2011-12-01 18:23:14 -05:00
# parsing exception.
#
2020-05-09 12:03:32 -04:00
def parse_error(error, env)
2020-05-09 11:04:04 -04:00
@debug_logger.error_dump(error: error, env: env, text: 'HTTP parse error, malformed request', force: true)
2011-09-30 11:29:37 -04:00
end
2015-07-15 10:59:34 -04:00
# An SSL error has occurred.
2020-05-09 12:03:32 -04:00
# +peeraddr+ peer address, +peercert+
# any peer certificate (if present), and +error+ an exception object.
#
2020-05-09 12:03:32 -04:00
def ssl_error(error, peeraddr, peercert)
subject = peercert ? peercert.subject : nil
2020-05-09 11:04:04 -04:00
@debug_logger.error_dump(error: error, text: "SSL error, peer: #{peeraddr}, peer cert: #{subject}", force: true)
end
2015-07-15 10:59:34 -04:00
# An unknown error has occurred.
2020-05-09 12:03:32 -04:00
# +error+ an exception object,
# +kind+ some additional info, and +env+ the request.
2011-12-01 18:23:14 -05:00
#
2020-05-09 12:03:32 -04:00
def unknown_error(error, env=nil, kind="Unknown")
2020-05-09 11:04:04 -04:00
@debug_logger.error_dump(error: error, env: env, text: kind, force: true)
2011-09-30 11:29:37 -04:00
end
def on_booted(&block)
register(:on_booted, &block)
end
def fire_on_booted!
fire(:on_booted)
end
2011-09-30 11:29:37 -04:00
DEFAULT = new(STDOUT, STDERR)
# Returns an Events object which writes its status to 2 StringIO
2011-12-01 18:23:14 -05:00
# objects.
#
2011-09-30 11:29:37 -04:00
def self.strings
Events.new StringIO.new, StringIO.new
end
def self.stdio
Events.new $stdout, $stderr
end
2016-02-07 17:51:54 -05:00
def self.null
n = NullIO.new
Events.new n, n
end
2011-09-30 11:29:37 -04:00
end
end