Kouhei Sutou
null+****@clear*****
Sun Aug 10 00:23:18 JST 2014
Kouhei Sutou 2014-08-10 00:23:18 +0900 (Sun, 10 Aug 2014) New Revision: deb32126909f94e53148c53b7c19cbbadabcef6d https://github.com/groonga/groonga/commit/deb32126909f94e53148c53b7c19cbbadabcef6d Message: mruby: introduce backtrace entry object Added files: lib/mrb/scripts/backtrace_entry.rb Modified files: lib/ctx_impl_mrb.c lib/mrb/scripts/context.rb lib/mrb/scripts/logger.rb lib/mrb/scripts/sources.am Modified: lib/ctx_impl_mrb.c (+2 -0) =================================================================== --- lib/ctx_impl_mrb.c 2014-08-09 23:34:27 +0900 (67ed270) +++ lib/ctx_impl_mrb.c 2014-08-10 00:23:18 +0900 (657e95a) @@ -44,6 +44,8 @@ grn_ctx_impl_mrb_init_bindings(grn_ctx *ctx) mrb->ud = ctx; ctx->impl->mrb.module = mrb_define_module(mrb, "Groonga"); + grn_mrb_load(ctx, "backtrace_entry.rb"); + grn_mrb_id_init(ctx); grn_mrb_operator_init(ctx); grn_mrb_ctx_init(ctx); Added: lib/mrb/scripts/backtrace_entry.rb (+20 -0) 100644 =================================================================== --- /dev/null +++ lib/mrb/scripts/backtrace_entry.rb 2014-08-10 00:23:18 +0900 (68ea9e4) @@ -0,0 +1,20 @@ +module Groonga + class BacktraceEntry + class << self + def parse(entry) + match_data = /:(\d+):?/.match(entry) + file = match_data.pre_match + line = match_data[1].to_i + method = match_data.post_match.gsub(/\Ain /, "") + new(file, line, method) + end + end + + attr_reader :file, :line, :method + def initialize(file, line, method) + @file = file + @line = line + @method = method + end + end +end Modified: lib/mrb/scripts/context.rb (+5 -10) =================================================================== --- lib/mrb/scripts/context.rb 2014-08-09 23:34:27 +0900 (a4b8eda) +++ lib/mrb/scripts/context.rb 2014-08-10 00:23:18 +0900 (8ebe2c9) @@ -4,11 +4,7 @@ module Groonga begin yield rescue => error - backtrace = error.backtrace - puts "#{error.class}: #{error.message}" - backtrace.each do |entry| - puts entry - end + logger.log_error(error) fallback end end @@ -23,11 +19,10 @@ module Groonga self.error_level = ErrorLevel.find(:error).to_i backtrace = error.backtrace - entry = backtrace.first - match_data = /:(\d+):/.match(entry) - self.error_file = match_data.pre_match - self.error_line = match_data[1].to_i - self.error_method = match_data.post_match.gsub(/^in /, "") + entry = BacktraceEntry.parse(backtrace.first) + self.error_file = entry.file + self.error_line = entry.line + self.error_method = entry.method self.error_message = error.message logger.log_error(error) Modified: lib/mrb/scripts/logger.rb (+19 -6) =================================================================== --- lib/mrb/scripts/logger.rb 2014-08-09 23:34:27 +0900 (4a6c99a) +++ lib/mrb/scripts/logger.rb 2014-08-10 00:23:18 +0900 (06f99c1) @@ -2,12 +2,25 @@ module Groonga class Logger def log_error(error) log_level = Level::ERROR.to_i - error.backtrace.each do |entry| - match_data = /:(\d+):/.match(entry) - file = match_data.pre_match - line = match_data[1].to_i - method = match_data.post_match.gsub(/^in /, "") - log(log_level, file, line, method, match_data.post_match) + + message = "#{error.class}: #{error.message}" + backtrace = error.backtrace + first_raw_entry = backtrace.first + if first_raw_entry + first_entry = BacktraceEntry.parse(first_raw_entry) + file = first_entry.file + line = first_entry.line + method = first_entry.method + else + file = "" + line = 0 + method = "" + end + log(log_level, file, line, method, message) + + backtrace.each do |raw_entry| + entry = BacktraceEntry.parse(raw_entry) + log(log_level, entry.file, entry.line, entry.method, raw_entry) end end end Modified: lib/mrb/scripts/sources.am (+1 -0) =================================================================== --- lib/mrb/scripts/sources.am 2014-08-09 23:34:27 +0900 (d82aca4) +++ lib/mrb/scripts/sources.am 2014-08-10 00:23:18 +0900 (93ade16) @@ -1,4 +1,5 @@ RUBY_SCRIPT_FILES = \ + backtrace_entry.rb \ context.rb \ context/error_level.rb \ context/rc.rb \ -------------- next part -------------- HTML����������������������������...Download