From e502b20f3818701220bf4c84c888605e126a63fc Mon Sep 17 00:00:00 2001 From: Chris Schlaeger Date: Fri, 29 Jan 2016 21:53:17 +0100 Subject: NEW: Trap CTRL-C aborts by user. Don't show a backtrace when the user aborts with CTRL-C. This patch also adds a --debug mode to show the backtrace again. --- lib/postrunner/Main.rb | 67 +++++++++++++++++++++++++++++++++++--------------- 1 file changed, 47 insertions(+), 20 deletions(-) diff --git a/lib/postrunner/Main.rb b/lib/postrunner/Main.rb index 9d7c17e..dab970d 100644 --- a/lib/postrunner/Main.rb +++ b/lib/postrunner/Main.rb @@ -40,28 +40,27 @@ module PostRunner return if (args = parse_options(args)).nil? - create_directory(@db_dir, 'PostRunner data') - @db = PEROBS::Store.new(File.join(@db_dir, 'database')) - # Create a hash to store configuration data in the store unless it - # exists already. - cfg = (@db['config'] ||= @db.new(PEROBS::Hash)) - cfg['unit_system'] ||= :metric - cfg['version'] ||= VERSION - # We always override the data_dir as the user might have moved the data - # directory. The only reason we store it in the DB is to have it - # available throught the application. - cfg['data_dir'] = @db_dir - # Always update html_dir setting so that the DB directory can be moved - # around by the user. - cfg['html_dir'] = File.join(@db_dir, 'html') - - setup_directories - if (errors = @db.check) != 0 - Log.fatal "Postrunner database is corrupted: #{errors} errors found" + unless $DEBUG + Kernel.trap('INT') do + begin + Log.fatal('Aborting on user request!') + rescue RuntimeError + exit 1 + end + end end - execute_command(args) - @db.sync + begin + main(args) + rescue Exception => e + if e.is_a?(SystemExit) || e.is_a?(Interrupt) + $stderr.puts e.backtrace.join("\n") if $DEBUG + else + Log.fatal("#{e}\n#{e.backtrace.join("\n")}\n\n" + + "#{'*' * 79}\nYou have triggered a bug in PostRunner " + + "#{VERSION}!") + end + end end private @@ -123,6 +122,9 @@ EOT 'Directory for the activity database and related files') do |d| @db_dir = d end + opts.on('--debug', 'Enable debug mode') do + $DEBUG = true + end opts.on('-v', '--verbose', 'Show internal messages helpful for debugging problems') do Log.level = Logger::DEBUG @@ -218,6 +220,31 @@ EOT end end + def main(args) + create_directory(@db_dir, 'PostRunner data') + @db = PEROBS::Store.new(File.join(@db_dir, 'database')) + # Create a hash to store configuration data in the store unless it + # exists already. + cfg = (@db['config'] ||= @db.new(PEROBS::Hash)) + cfg['unit_system'] ||= :metric + cfg['version'] ||= VERSION + # We always override the data_dir as the user might have moved the data + # directory. The only reason we store it in the DB is to have it + # available throught the application. + cfg['data_dir'] = @db_dir + # Always update html_dir setting so that the DB directory can be moved + # around by the user. + cfg['html_dir'] = File.join(@db_dir, 'html') + + setup_directories + if (errors = @db.check) != 0 + Log.fatal "Postrunner database is corrupted: #{errors} errors found" + end + execute_command(args) + + @db.sync + end + def setup_directories create_directory(@db['config']['html_dir'], 'HTML output') -- cgit v1.2.3