Skip to content

Commit

Permalink
Add quiet flag to server configuration (false by default)
Browse files Browse the repository at this point in the history
It suppresses start and stop messages that Sinatra generates.
As requested here: #1148.
  • Loading branch information
304 committed Jul 26, 2016
1 parent bd1a9dc commit d8a6930
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lib/sinatra/base.rb
Expand Up @@ -1451,7 +1451,7 @@ def quit!
return unless running?
# Use Thin's hard #stop! if available, otherwise just #stop.
running_server.respond_to?(:stop!) ? running_server.stop! : running_server.stop
$stderr.puts "== Sinatra has ended his set (crowd applauds)" unless handler_name =~/cgi/i
$stderr.puts "== Sinatra has ended his set (crowd applauds)" unless supress_messages?
set :running_server, nil
set :handler_name, nil
end
Expand Down Expand Up @@ -1533,7 +1533,7 @@ def caller_locations
# Starts the server by running the Rack Handler.
def start_server(handler, server_settings, handler_name)
handler.run(self, server_settings) do |server|
unless handler_name =~ /cgi/i
unless supress_messages?
$stderr.puts "== Sinatra (v#{Sinatra::VERSION}) has taken the stage on #{port} for #{environment} with backup from #{handler_name}"
end

Expand All @@ -1546,6 +1546,10 @@ def start_server(handler, server_settings, handler_name)
end
end

def supress_messages?
handler_name =~ /cgi/i || quiet
end

def setup_traps
if traps?
at_exit { quit! }
Expand Down Expand Up @@ -1801,6 +1805,7 @@ class << self
set :server, %w[HTTP webrick]
set :bind, Proc.new { development? ? 'localhost' : '0.0.0.0' }
set :port, Integer(ENV['PORT'] && !ENV['PORT'].empty? ? ENV['PORT'] : 4567)
set :quiet, false

ruby_engine = defined?(RUBY_ENGINE) && RUBY_ENGINE

Expand Down
1 change: 1 addition & 0 deletions lib/sinatra/main.rb
Expand Up @@ -17,6 +17,7 @@ class Application < Base
op.on('-o addr', "set the host (default is #{bind})") { |val| set :bind, val }
op.on('-e env', 'set the environment (default is development)') { |val| set :environment, val.to_sym }
op.on('-s server', 'specify rack server/handler (default is thin)') { |val| set :server, val }
op.on('-q', 'turn on quiet mode (default is off)') { set :quiet, true }
op.on('-x', 'turn on the mutex lock (default is off)') { set :lock, true }
}.parse!(ARGV.dup)
end
Expand Down
14 changes: 14 additions & 0 deletions test/server_test.rb
Expand Up @@ -53,4 +53,18 @@ def teardown
it "falls back on the next server handler when not found" do
@app.run! :server => %w[foo bar mock]
end

describe "Quiet mode" do
it "sends data to stderr when server starts and stops" do
@app.run!
assert_match(/\=\= Sinatra/, $stderr.string)
end

context "when quiet mode is activated" do
it "does not generate Sinatra start and stop messages" do
@app.run! quiet: true
refute_match(/\=\= Sinatra/, $stderr.string)
end
end
end
end

0 comments on commit d8a6930

Please sign in to comment.