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

72 lines
1.8 KiB
Ruby
Raw Normal View History

2011-09-30 11:29:37 -04:00
require 'puma/const'
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
include Const
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)
@stdout = stdout
@stderr = stderr
end
attr_reader :stdout, :stderr
2012-07-02 18:09:35 -04:00
# Write +str+ to +@stdout+
#
def log(str)
@stdout.puts str
end
2012-08-01 13:11:27 -04:00
def write(str)
@stdout.write str
end
2012-07-02 18:09:35 -04:00
# Write +str+ to +@stderr+
#
def error(str)
@stderr.puts "ERROR: #{str}"
exit 1
end
2011-12-01 18:23:14 -05:00
# An HTTP parse error has occured.
# +server+ is the Server object, +env+ the request, and +error+ a
# parsing exception.
#
2011-09-30 11:29:37 -04:00
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}"
@stderr.puts "#{Time.now}: ENV: #{env.inspect}\n---\n"
end
2011-12-01 18:23:14 -05:00
# An unknown error has occured.
# +server+ is the Server object, +env+ the request, +error+ an exception
# object, and +kind+ some additional info.
#
2012-01-13 19:29:50 -05:00
def unknown_error(server, error, kind="Unknown")
2011-09-30 11:29:37 -04:00
if error.respond_to? :render
error.render "#{Time.now}: #{kind} error", @stderr
else
@stderr.puts "#{Time.now}: #{kind} error: #{error.inspect}"
@stderr.puts error.backtrace.join("\n")
end
end
DEFAULT = new(STDOUT, STDERR)
2011-12-01 18:23:14 -05:00
# Returns an Events object which writes it's status to 2 StringIO
# objects.
#
2011-09-30 11:29:37 -04:00
def self.strings
Events.new StringIO.new, StringIO.new
end
end
end