Kouhei Sutou
null+****@clear*****
Thu May 14 18:55:21 JST 2015
Kouhei Sutou 2015-05-14 18:55:21 +0900 (Thu, 14 May 2015) New Revision: b01d36e8edd9db5119ff52a3046d4dca2b5abb97 https://github.com/ranguba/rroonga/commit/b01d36e8edd9db5119ff52a3046d4dca2b5abb97 Message: Add Groonga::QueryLogger.log Modified files: ext/groonga/rb-grn-query-logger.c test/test-query-logger.rb Modified: ext/groonga/rb-grn-query-logger.c (+69 -1) =================================================================== --- ext/groonga/rb-grn-query-logger.c 2015-05-14 18:27:17 +0900 (33ad1bb) +++ ext/groonga/rb-grn-query-logger.c 2015-05-14 18:55:21 +0900 (24ce6fa) @@ -1,6 +1,6 @@ /* -*- coding: utf-8; mode: C; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ /* - Copyright (C) 2012-2013 Kouhei Sutou <kou �� clear-code.com> + Copyright (C) 2012-2015 Kouhei Sutou <kou �� clear-code.com> This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -45,6 +45,72 @@ rb_grn_query_log_flags_to_ruby_object (unsigned int flags) return UINT2NUM(flags); } +/* + * 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 :flags [nil, Integer, String] (0) + * The flags for the message. + * + * The flags are passed to query logger. You can custom query + * logger behavior by the flags. For example, you can omit elapsed + * time by passing `Groonga::QueryLogger::COMMAND` flag or + * `Groonga::QueryLogger::DESTINATION`. + * + * If `:flags` value is `String`, parsed by + * {Groonga::QueryLogger.parse}. + * + * `nil` equals to `0`. + * @option options :mark [String] ("") + * The mark for the message. + * + * Normally, a character is used as a mark such as `":"`, `"<"` and `">"`. + * @return [void] + * + * @since 5.0.2 + */ +static VALUE +rb_grn_query_logger_s_log (int argc, VALUE *argv, VALUE klass) +{ + VALUE rb_message; + const char *message; + VALUE rb_context = Qnil; + grn_ctx *context; + VALUE rb_flags; + unsigned int flags = GRN_QUERY_LOG_NONE; + VALUE rb_mark; + const char *mark = ""; + 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, + "flags", &rb_flags, + "mark", &rb_mark, + NULL); + + context = rb_grn_context_ensure(&rb_context); + + if (!NIL_P(rb_flags)) { + flags = rb_funcall(mGrnQueryLoggerFlags, id_parse, 2, + UINT2NUM(flags), rb_flags); + } + if (!NIL_P(rb_mark)) { + mark = StringValueCStr(rb_mark); + } + + grn_query_logger_put(context, flags, mark, "%s", message); + + return Qnil; +} + static void rb_grn_query_logger_log (grn_ctx *ctx, unsigned int flag, const char *timestamp, const char *info, @@ -305,6 +371,8 @@ rb_grn_init_query_logger (VALUE mGrn) cGrnQueryLogger = rb_define_class_under(mGrn, "QueryLogger", rb_cObject); rb_cv_set(cGrnQueryLogger, "@@current_logger", Qnil); + rb_define_singleton_method(cGrnQueryLogger, "log", + rb_grn_query_logger_s_log, -1); rb_define_singleton_method(cGrnQueryLogger, "register", rb_grn_query_logger_s_register, -1); rb_define_singleton_method(cGrnQueryLogger, "unregister", Modified: test/test-query-logger.rb (+32 -0) =================================================================== --- test/test-query-logger.rb 2015-05-14 18:27:17 +0900 (f73e329) +++ test/test-query-logger.rb 2015-05-14 18:55:21 +0900 (33502d3) @@ -38,4 +38,36 @@ class QueryLoggerTest < Test::Unit::TestCase @query_log_path.exist? end end + + sub_test_case ".log" do + test "no options" do + messages = [] + Groonga::QueryLogger.register do |action, flag, timestamp, info, message| + messages << message + end + Groonga::QueryLogger.log("1") + Groonga::QueryLogger.log("2") + Groonga::QueryLogger.log("3") + assert_equal(["1", "2", "3"], + messages) + end + + test ":mark" do + infos = [] + Groonga::QueryLogger.register do |action, flag, timestamp, info, message| + infos << info + end + Groonga::QueryLogger.log("default") + Groonga::QueryLogger.log("mark", :mark => ":") + normalized_infos = infos.collect do |info| + info.gsub(/\A0x[a-f\d]+\|([^\d])?[\d]+ \z/, + "context_id|\\1timestamp ") + end + assert_equal([ + "context_id|timestamp ", + "context_id|:timestamp ", + ], + normalized_infos) + end + end end -------------- next part -------------- HTML����������������������������...Download