lib/protobuf/cli.rb in protobuf-3.2.1 vs lib/protobuf/cli.rb in protobuf-3.3.0

- old
+ new

@@ -1,14 +1,15 @@ require 'thor' require 'protobuf/version' -require 'protobuf/logger' +require 'protobuf/logging' require 'protobuf/rpc/servers/socket_runner' require 'protobuf/rpc/servers/zmq_runner' module Protobuf class CLI < ::Thor include ::Thor::Actions + include ::Protobuf::Logging attr_accessor :runner, :mode default_task :start @@ -19,11 +20,11 @@ option :backlog, :type => :numeric, :default => 100, :aliases => %w(-b), :desc => 'Backlog for listening socket when using Socket Server.' option :threshold, :type => :numeric, :default => 100, :aliases => %w(-t), :desc => 'Multi-threaded Socket Server cleanup threshold.' option :threads, :type => :numeric, :default => 5, :aliases => %w(-r), :desc => 'Number of worker threads to run. Only applicable in --zmq mode.' - option :log, :type => :string, :aliases => %w(-l), :desc => 'Log file or device. Default is STDOUT.' + option :log, :type => :string, :default => STDOUT, :aliases => %w(-l), :desc => 'Log file or device. Default is STDOUT.' option :level, :type => :numeric, :default => ::Logger::INFO, :aliases => %w(-v), :desc => 'Log level to use, 0-5 (see http://www.ruby-doc.org/stdlib/libdoc/logger/rdoc/)' option :socket, :type => :boolean, :aliases => %w(-s), :desc => 'Socket Mode for server and client connections.' option :zmq, :type => :boolean, :aliases => %w(-z), :desc => 'ZeroMQ Socket Mode for server and client connections.' @@ -84,16 +85,18 @@ end # Setup the protobuf logger. def configure_logger debug_say('Configuring logger') - ::Protobuf::Logger.configure({ :file => options.log || STDOUT, - :level => options.debug? ? ::Logger::DEBUG : options.level }) + log_level = options.debug? ? ::Logger::DEBUG : options.level + + ::Protobuf::Logging.initialize_logger(options.log, log_level) + # Debug output the server options to the log file. - ::Protobuf::Logger.debug { 'Debugging options:' } - ::Protobuf::Logger.debug { options.inspect } + logger.debug { 'Debugging options:' } + logger.debug { options.inspect } end # Re-write the $0 var to have a nice process name in ps. def configure_process_name(app_file) debug_say('Configuring process name') @@ -183,19 +186,20 @@ opt end def say_and_exit(message, exception = nil) - message = set_color(message, :red) if ::Protobuf::Logger.file == STDOUT + message = set_color(message, :red) if options.log == STDOUT - ::Protobuf::Logger.error { message } + logger.error { message } + if exception $stderr.puts "[#{exception.class.name}] #{exception.message}" $stderr.puts exception.backtrace.join("\n") - ::Protobuf::Logger.error { "[#{exception.class.name}] #{exception.message}" } - ::Protobuf::Logger.debug { exception.backtrace.join("\n") } + logger.error { "[#{exception.class.name}] #{exception.message}" } + logger.debug { exception.backtrace.join("\n") } end exit(1) end @@ -210,21 +214,21 @@ @runner = ::Protobuf::Rpc::ZmqRunner.new(runner_options) end def shutdown_server - ::Protobuf::Logger.info { 'RPC Server shutting down...' } + logger.info { 'RPC Server shutting down...' } @runner.try(:stop) ::Protobuf::Rpc::ServiceDirectory.instance.stop - ::Protobuf::Logger.info { 'Shutdown complete' } + logger.info { 'Shutdown complete' } end # Start the runner and log the relevant options. def start_server debug_say('Running server') @runner.run do - ::Protobuf::Logger.info { + logger.info { "pid #{::Process.pid} -- #{@runner_mode} RPC Server listening at #{options.host}:#{options.port}" } ::ActiveSupport::Notifications.instrument("after_server_bind") end