Add option to redirect stdout and stderr to a file
When using a daemon, stdout and stderr are redirected to /dev/null. If the program generates an exception and crashes, we can't get the message. This option allows to get everything from stdout and stderr in a file.
This commit is contained in:
parent
6530471210
commit
f2e78c476a
@ -13,6 +13,7 @@ module SMSd
|
|||||||
self.machine = yield block if block_given?
|
self.machine = yield block if block_given?
|
||||||
|
|
||||||
init_logger
|
init_logger
|
||||||
|
redirect_output
|
||||||
@modem = Biju::Hayes.new(options[:modem], pin: options[:pin])
|
@modem = Biju::Hayes.new(options[:modem], pin: options[:pin])
|
||||||
rescue Errno::ENOENT => e
|
rescue Errno::ENOENT => e
|
||||||
logger.warn e.message
|
logger.warn e.message
|
||||||
@ -23,7 +24,7 @@ module SMSd
|
|||||||
@running_since = DateTime.now
|
@running_since = DateTime.now
|
||||||
|
|
||||||
catch_signals
|
catch_signals
|
||||||
Process.daemon if options[:daemonize]
|
Process.daemon(false, !@options[:output].nil?) if options[:daemonize]
|
||||||
|
|
||||||
loop do
|
loop do
|
||||||
pong if options[:alive]
|
pong if options[:alive]
|
||||||
@ -60,6 +61,14 @@ module SMSd
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def redirect_output
|
||||||
|
if @options[:output]
|
||||||
|
output_file = File.new(@options[:output], 'a')
|
||||||
|
$stdout.reopen(output_file)
|
||||||
|
$stderr.reopen(output_file)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def catch_signals
|
def catch_signals
|
||||||
signal_term = proc { @terminate = true }
|
signal_term = proc { @terminate = true }
|
||||||
Signal.trap('SIGTERM', signal_term)
|
Signal.trap('SIGTERM', signal_term)
|
||||||
|
@ -34,6 +34,11 @@ module SMSd
|
|||||||
options[:pin] = pin
|
options[:pin] = pin
|
||||||
end
|
end
|
||||||
|
|
||||||
|
opts.on('-r', '--redirect-output FILE',
|
||||||
|
'Redirect stdout and stderr to a file') do |output|
|
||||||
|
options[:output] = output
|
||||||
|
end
|
||||||
|
|
||||||
opts.separator ''
|
opts.separator ''
|
||||||
opts.separator 'Common options:'
|
opts.separator 'Common options:'
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user