Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add quiet flag to server configuration (false by default) #1153

Merged
merged 2 commits into from Jul 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 7 additions & 1 deletion README.md
Expand Up @@ -2192,6 +2192,11 @@ set :protection, :session => true
<tt>app_file</tt> setting if not set.
</dd>

<dt>quiet</dt>
<dd>
Disables logs generated by Sinatra's start and stop commands. `false` by default.
</dd>

<dt>reload_templates</dt>
<dd>
Whether or not to reload templates between requests. Enabled in development
Expand Down Expand Up @@ -2839,7 +2844,7 @@ being [extending the main object](https://github.com/sinatra/sinatra/blob/ca0636
Sinatra applications can be run directly:

```shell
ruby myapp.rb [-h] [-x] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s HANDLER]
ruby myapp.rb [-h] [-x] [-q] [-e ENVIRONMENT] [-p PORT] [-o HOST] [-s HANDLER]
```

Options are:
Expand All @@ -2850,6 +2855,7 @@ Options are:
-o # set the host (default is 0.0.0.0)
-e # set the environment (default is development)
-s # specify rack server/handler (default is thin)
-q # turn on quiet mode for server (default is off)
-x # turn on the mutex lock (default is off)
```

Expand Down
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