From 5a5b4eb1f74e30d1a337c22a1a8cc6c7934599c4 Mon Sep 17 00:00:00 2001 From: Emil Tin Date: Mon, 17 Dec 2012 17:18:41 +0100 Subject: [PATCH] output last lines from log if binary exists during testing --- features/support/data.rb | 16 ++++++++++++++-- features/support/file.rb | 22 ++++++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) create mode 100644 features/support/file.rb diff --git a/features/support/data.rb b/features/support/data.rb index d45ea47e3..bf916b4fe 100644 --- a/features/support/data.rb +++ b/features/support/data.rb @@ -197,17 +197,28 @@ def write_timestamp File.open( "#{@osm_file}.osrm.timestamp", 'w') {|f| f.write(OSM_TIMESTAMP) } end +def log_tail path, n + File.open(path) do |f| + return f.tail(n).map { |line| " #{line}" }.join "\n" + end +end + def reprocess Dir.chdir TEST_FOLDER do write_osm write_timestamp convert_osm_to_pbf + + log_path = 'preprocessing.log' + log_lines = 3 + unless extracted? log_preprocess_info log "== Extracting #{@osm_file}.osm...", :preprocess unless system "../osrm-extract #{@osm_file}.osm.pbf 1>>#{PREPROCESS_LOG_FILE} 2>>#{PREPROCESS_LOG_FILE} ../profiles/#{@profile}.lua" log "*** Exited with code #{$?.exitstatus}.", :preprocess - raise OSRMError.new 'osrm-extract', $?.exitstatus, "*** osrm-extract exited with code #{$?.exitstatus}. The file preprocess.log might contain more info." + tail = log_tail log_path,log_lines + raise OSRMError.new 'osrm-extract', $?.exitstatus, "*** osrm-extract exited with code #{$?.exitstatus}. Last #{log_lines} lines from #{log_path}:\n#{tail}\n" end log '', :preprocess end @@ -216,7 +227,8 @@ def reprocess log "== Preparing #{@osm_file}.osm...", :preprocess unless system "../osrm-prepare #{@osm_file}.osrm #{@osm_file}.osrm.restrictions 1>>#{PREPROCESS_LOG_FILE} 2>>#{PREPROCESS_LOG_FILE} ../profiles/#{@profile}.lua" log "*** Exited with code #{$?.exitstatus}.", :preprocess - raise OSRMError.new 'osrm-prepare', $?.exitstatus, "*** osrm-prepare exited with code #{$?.exitstatus}. The file preprocess.log might contain more info." + tail = log_tail log_path,log_lines + raise OSRMError.new 'osrm-prepare', $?.exitstatus, "*** osrm-prepare exited with code #{$?.exitstatus}. Last #{log_lines} lines from #{log_path}:\n#{tail}\n" end log '', :preprocess end diff --git a/features/support/file.rb b/features/support/file.rb new file mode 100644 index 000000000..cd8fd14b3 --- /dev/null +++ b/features/support/file.rb @@ -0,0 +1,22 @@ +class File + + #read last n lines of a file. useful for getting last part of a big log file. + def tail(n) + buffer = 1024 + idx = (size - buffer).abs + chunks = [] + lines = 0 + + begin + seek(idx) + chunk = read(buffer) + lines += chunk.count("\n") + chunks.unshift chunk + idx -= buffer + end while lines < ( n + 1 ) && pos != 0 + + tail_of_file = chunks.join('') + ary = tail_of_file.split(/\n/) + lines_to_return = ary[ ary.size - n, ary.size - 1 ] + end +end \ No newline at end of file