Kouhei Sutou
null+****@clear*****
Thu May 14 16:19:41 JST 2015
Kouhei Sutou 2015-05-14 16:19:41 +0900 (Thu, 14 May 2015) New Revision: 62cdbd77998d20a42468b29eedd278b0eccfe782 https://github.com/ranguba/rroonga/commit/62cdbd77998d20a42468b29eedd278b0eccfe782 Message: Add Groonga::Logger.log Modified files: ext/groonga/rb-grn-logger.c test/test-logger.rb Modified: ext/groonga/rb-grn-logger.c (+102 -0) =================================================================== --- ext/groonga/rb-grn-logger.c 2015-05-13 18:46:25 +0900 (1be342c) +++ ext/groonga/rb-grn-logger.c 2015-05-14 16:19:41 +0900 (700f017) @@ -32,6 +32,11 @@ VALUE cGrnLogger; VALUE mGrnLoggerFlags; VALUE cGrnCallbackLogger; +static ID id_caller_locations; +static ID id_path; +static ID id_lineno; +static ID id_label; + static ID id_new; static ID id_parse; static ID id_log; @@ -122,6 +127,96 @@ rb_grn_log_level_to_ruby_object (grn_log_level level) return rb_level; } +/* + * Logs a message. + * + * @overload log(message, options={}) + * @param message [String] The log message. + * @param options [::Hash] + * @option options :context [Groonga::Context] (Groonga::Context.default) + * The context for the message. + * @option options :level [nil, :none, :emergency, :alert, :critical, + * :error, :warning, :notice, :info, :debug, :dump] (:notice) + * The level for the message. + * + * `nil` equals to `:notice`. + * @option options :file [nil, String] (nil) + * The file name where the message is occurred. + * + * If all of `:file`, `:line` and `:function` are nil, these + * values are guessed from `Kernel.#caller_locations` result. + * @option options :line [nil, Integer] (nil) + * The line number where the message is occurred. + * @option options :function [nil, String] (nil) + * The function or related name such as method name where the + * message is occurred. + * @return [void] + * + * @since 5.0.2 + */ +static VALUE +rb_grn_logger_s_log (int argc, VALUE *argv, VALUE klass) +{ + VALUE rb_message; + const char *message; + VALUE rb_context = Qnil; + grn_ctx *context; + VALUE rb_level; + grn_log_level level = GRN_LOG_DEFAULT_LEVEL; + VALUE rb_file; + const char *file = NULL; + VALUE rb_line; + int line; + VALUE rb_function; + const char *function = NULL; + VALUE rb_options; + + rb_scan_args(argc, argv, "11", &rb_message, &rb_options); + + message = StringValueCStr(rb_message); + + rb_grn_scan_options(rb_options, + "context", &rb_context, + "level", &rb_level, + "file", &rb_file, + "line", &rb_line, + "function", &rb_function, + NULL); + + context = rb_grn_context_ensure(&rb_context); + + if (!NIL_P(rb_level)) { + level = RVAL2GRNLOGLEVEL(rb_level); + } + + if (NIL_P(rb_file) && NIL_P(rb_line) && NIL_P(rb_function)) { + VALUE rb_locations; + VALUE rb_location; + rb_locations = rb_funcall(rb_cObject, + id_caller_locations, + 2, + INT2NUM(1), INT2NUM(1)); + rb_location = RARRAY_PTR(rb_locations)[0]; + rb_file = rb_funcall(rb_location, id_path, 0); + rb_line = rb_funcall(rb_location, id_lineno, 0); + rb_function = rb_funcall(rb_location, id_label, 0); + } + + if (!NIL_P(rb_file)) { + file = StringValueCStr(rb_file); + } + if (!NIL_P(rb_line)) { + line = NUM2INT(rb_line); + } + if (!NIL_P(rb_function)) { + function = StringValueCStr(rb_function); + } + + grn_logger_put(context, level, file, line, function, "%s", message); + + return Qnil; +} + static void rb_grn_logger_reset_with_error_check (VALUE klass, grn_ctx *context) { @@ -414,6 +509,11 @@ rb_grn_logger_s_set_path (VALUE klass, VALUE rb_path) void rb_grn_init_logger (VALUE mGrn) { + id_caller_locations = rb_intern("caller_locations"); + id_path = rb_intern("path"); + id_lineno = rb_intern("lineno"); + id_label = rb_intern("label"); + id_new = rb_intern("new"); id_parse = rb_intern("parse"); id_log = rb_intern("log"); @@ -429,6 +529,8 @@ rb_grn_init_logger (VALUE mGrn) cGrnLogger = rb_define_class_under(mGrn, "Logger", rb_cObject); rb_cv_set(cGrnLogger, "@@current_logger", Qnil); + rb_define_singleton_method(cGrnLogger, "log", + rb_grn_logger_s_log, -1); rb_define_singleton_method(cGrnLogger, "register", rb_grn_logger_s_register, -1); rb_define_singleton_method(cGrnLogger, "unregister", Modified: test/test-logger.rb (+86 -0) =================================================================== --- test/test-logger.rb 2015-05-13 18:46:25 +0900 (c8279b0) +++ test/test-logger.rb 2015-05-14 16:19:41 +0900 (3725126) @@ -34,4 +34,90 @@ class LoggerTest < Test::Unit::TestCase Groonga::Logger.reopen assert_true(@log_path.exist?) end + + sub_test_case ".log" do + test "no options" do + messages = [] + Groonga::Logger.register do |event, level, time, title, message, location| + messages << message + end + Groonga::Logger.log("1") + Groonga::Logger.log("2") + Groonga::Logger.log("3") + assert_equal(["1", "2", "3"], + messages) + end + + test ":level" do + levels = [] + Groonga::Logger.register(:max_level => :dump) do |event, level, *rest| + levels << level + end + Groonga::Logger.log("default") + Groonga::Logger.log("debug", :level => :debug) + assert_equal([:notice, :debug], + levels) + end + + test "default location" do + locations = [] + Groonga::Logger.register do |event, level, time, title, message, location| + locations << location + end + Groonga::Logger.log("message"); line = __LINE__ + function = caller_locations(0, 1)[0].label + assert_equal([ + "#{Process.pid} #{__FILE__}:#{line} #{function}()", + ], + locations) + end + + test ":file" do + locations = [] + Groonga::Logger.register do |event, level, time, title, message, location| + locations << location + end + Groonga::Logger.log("message", :file => "file.rb") + locations = locations.collect do |location| + location.gsub(/\A(\d+) (.*?):(\d+) (.*?)\(\)\z/, + "0 \\2:0 function()") + end + assert_equal([ + "0 file.rb:0 function()", + ], + locations) + end + + test ":line" do + locations = [] + Groonga::Logger.register do |event, level, time, title, message, location| + locations << location + end + Groonga::Logger.log("message", :line => 100) + locations = locations.collect do |location| + location.gsub(/\A(\d+) (.*?):(\d+) (.*?)\(\)\z/, + "0 test.rb:\\3 function()") + end + assert_equal([ + "0 test.rb:100 function()", + ], + locations) + end + + test ":function" do + locations = [] + Groonga::Logger.register do |event, level, time, title, message, location| + locations << location + end + Groonga::Logger.log("message", :function => "method_name") + locations = locations.collect do |location| + location.gsub(/\A(\d+) (.*?):(\d+) (.*?)\(\)\z/, + "0 test.rb:0 \\4()") + end + assert_equal([ + "0 test.rb:0 method_name()", + ], + locations) + end + end end -------------- next part -------------- HTML����������������������������...Download