[Groonga-commit] groonga/groonga at e0f0c02 [master] Support grndb --log-flags (#944)

Zurück zum Archiv-Index
Kentaro Hayashi null+****@clear*****
Tue May 21 18:31:42 JST 2019


Kentaro Hayashi	2019-05-21 18:31:42 +0900 (Tue, 21 May 2019)

  Revision: e0f0c0291f7247412ae6414b4bf53361f82f3697
  https://github.com/groonga/groonga/commit/e0f0c0291f7247412ae6414b4bf53361f82f3697

  Message:
    Support grndb --log-flags (#944)
    
    * test: move common method to helper
    
    It is used from Groonga and GrnDB test case.
    
    * test: add missing --log-path option
    
    * test: add grndb test case
    
    * grndb: support --log-flags option

  Added files:
    lib/mrb/scripts/logger/flags.rb
    test/command_line/suite/grndb/test_options.rb
  Modified files:
    lib/mrb/mrb_logger.c
    lib/mrb/scripts/command_line_parser.rb
    lib/mrb/scripts/logger/sources.am
    src/grndb.c
    test/command_line/helper/command_runner.rb
    test/command_line/suite/groonga/test_options.rb

  Modified: lib/mrb/mrb_logger.c (+14 -0)
===================================================================
--- lib/mrb/mrb_logger.c    2019-05-21 17:57:05 +0900 (7ca77e072)
+++ lib/mrb/mrb_logger.c    2019-05-21 18:31:42 +0900 (5358bb4a9)
@@ -58,6 +58,17 @@ logger_s_get_default_level(mrb_state *mrb, mrb_value self)
 }
 
 static mrb_value
+logger_s_get_default_flags(mrb_state *mrb, mrb_value self)
+{
+  mrb_value mrb_flags_class;
+  mrb_value mrb_flags;
+
+  mrb_flags_class = mrb_const_get(mrb, self, mrb_intern_lit(mrb, "Flags"));
+  mrb_flags = mrb_fixnum_value(grn_default_logger_get_flags());
+  return mrb_funcall(mrb, mrb_flags_class, "new", 1, mrb_flags);
+}
+
+static mrb_value
 logger_need_log_p(mrb_state *mrb, mrb_value self)
 {
   grn_ctx *ctx = (grn_ctx *)mrb->ud;
@@ -122,6 +133,8 @@ grn_mrb_logger_init(grn_ctx *ctx)
                               logger_s_get_default_path, MRB_ARGS_NONE());
   mrb_define_singleton_method(mrb, (struct RObject *)klass, "default_level",
                               logger_s_get_default_level, MRB_ARGS_NONE());
+  mrb_define_singleton_method(mrb, (struct RObject *)klass, "default_flags",
+                              logger_s_get_default_flags, MRB_ARGS_NONE());
 
   mrb_define_method(mrb, klass, "need_log?", logger_need_log_p, MRB_ARGS_REQ(1));
   mrb_define_method(mrb, klass, "need_location_in_message?",
@@ -129,6 +142,7 @@ grn_mrb_logger_init(grn_ctx *ctx)
   mrb_define_method(mrb, klass, "log_raw", logger_log_raw, MRB_ARGS_REQ(5));
 
   grn_mrb_load(ctx, "logger/level.rb");
+  grn_mrb_load(ctx, "logger/flags.rb");
   grn_mrb_load(ctx, "logger.rb");
 }
 #endif

  Modified: lib/mrb/scripts/command_line_parser.rb (+4 -0)
===================================================================
--- lib/mrb/scripts/command_line_parser.rb    2019-05-21 17:57:05 +0900 (7dad55a75)
+++ lib/mrb/scripts/command_line_parser.rb    2019-05-21 18:31:42 +0900 (be3f02c85)
@@ -97,6 +97,10 @@ module Groonga
       options.string("--log-level",
                      "Change log level (#{default_log_level.name})",
                      default: default_log_level)
+      default_log_flags = Logger.default_flags
+      options.string("--log-flags",
+                     "Change log flags (#{default_log_flags.to_s})",
+                     default: default_log_flags)
     end
 
     def find_command(name)

  Added: lib/mrb/scripts/logger/flags.rb (+60 -0) 100644
===================================================================
--- /dev/null
+++ lib/mrb/scripts/logger/flags.rb    2019-05-21 18:31:42 +0900 (8b3198ddd)
@@ -0,0 +1,60 @@
+module Groonga
+  class Logger
+    class Flags
+      VALUES = {
+        time: 1,
+        title: 2,
+        message: 4,
+        location: 8,
+        process_id: 16,
+        thread_id: 32,
+      }
+
+      attr_reader :value
+      def initialize(value)
+        case value
+        when String, Symbol
+          @value = VALUES[value.to_sym]
+          if****@value*****?
+            available_names = VALUES.keys.inspect
+            message = "unknown flag name: #{value.inspect}: #{available_names}"
+            raise ArgumentError, message
+          end
+        else
+          @value = value
+        end
+      end
+
+      def to_i
+        @value
+      end
+
+      def to_s
+        names = []
+        VALUES.each do |name, value|
+          names << name.to_s unless (value & @value) == 0
+        end
+        names.join("|")
+      end
+
+      def |(other)
+        other = self.class.new(other) unless other.is_a?(self.class)
+        new(@value | other.to_i)
+      end
+
+      def &(other)
+        other = self.class.new(other) unless other.is_a?(self.class)
+        new(@value & other.to_i)
+      end
+
+      NONE = new(0)
+      TIME = new(:time)
+      TITLE = new(:title)
+      MESSAGE = new(:message)
+      LOCATION = new(:location)
+      PROCESS_ID = new(:process_id)
+      PID = PROCESS_ID
+      THREAD_ID = new(:thread_id)
+    end
+  end
+end

  Modified: lib/mrb/scripts/logger/sources.am (+2 -1)
===================================================================
--- lib/mrb/scripts/logger/sources.am    2019-05-21 17:57:05 +0900 (7231ee4ee)
+++ lib/mrb/scripts/logger/sources.am    2019-05-21 18:31:42 +0900 (2096660af)
@@ -1,2 +1,3 @@
 RUBY_SCRIPT_FILES =				\
-	level.rb
+	level.rb				\
+	flags.rb

  Modified: src/grndb.c (+22 -0)
===================================================================
--- src/grndb.c    2019-05-21 17:57:05 +0900 (5a968b680)
+++ src/grndb.c    2019-05-21 18:31:42 +0900 (8b4016fac)
@@ -133,6 +133,7 @@ main(int argc, char **argv)
   int exit_code = EXIT_SUCCESS;
   const char *log_path = GRN_LOG_PATH;
   const char *log_level_name = NULL;
+  const char *log_flags_name = NULL;
 
   {
     int i;
@@ -149,6 +150,7 @@ main(int argc, char **argv)
 
 #define log_path_prefix "--log-path"
 #define log_level_prefix "--log-level"
+#define log_flags_prefix "--log-flags"
       if (strcmp(arg, log_path_prefix) == 0) {
         if (i + 1 < argc) {
           log_path = argv[i + 1];
@@ -167,9 +169,19 @@ main(int argc, char **argv)
                          log_level_prefix "=",
                          strlen(log_level_prefix "=")) == 0) {
         log_level_name = arg + strlen(log_level_prefix "=");
+      } else if (strcmp(arg, log_flags_prefix) == 0) {
+        if (i + 1 < argc) {
+          log_flags_name = argv[i + 1];
+          i++;
+        }
+      } else if (strncmp(arg,
+                         log_flags_prefix "=",
+                         strlen(log_flags_prefix "=")) == 0) {
+        log_flags_name = arg + strlen(log_flags_prefix "=");
       }
 #undef log_path_prefix
 #undef log_level_prefix
+#undef log_flags_prefix
     }
   }
 
@@ -184,6 +196,16 @@ main(int argc, char **argv)
     grn_default_logger_set_max_level(log_level);
   }
 
+  if (log_flags_name) {
+    int log_flags;
+    if (!grn_log_flags_parse(log_flags_name, -1, &log_flags)) {
+      fprintf(stderr, "%s: failed to parse log flags: <%s>\n",
+              argv[0], log_flags_name);
+      return EXIT_FAILURE;
+    }
+    grn_default_logger_set_flags(log_flags);
+  }
+
   if (grn_init() != GRN_SUCCESS) {
     return EXIT_FAILURE;
   }

  Modified: test/command_line/helper/command_runner.rb (+39 -0)
===================================================================
--- test/command_line/helper/command_runner.rb    2019-05-21 17:57:05 +0900 (7f9444dc7)
+++ test/command_line/helper/command_runner.rb    2019-05-21 18:31:42 +0900 (e52ad080f)
@@ -96,6 +96,7 @@ module CommandRunner
     command_line = [
       grndb_path,
       command,
+      "--log-path", @log_path.to_s,
       @database_path.to_s,
     ]
     command_line.concat(arguments)
@@ -143,6 +144,44 @@ module CommandRunner
     find_program("grndb", :prefer_libtool => true)
   end
 
+  def normalize_init_line(line)
+    line.chomp.gsub(/\A
+                         (\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2}\.\d+)?
+                         \|\
+                         ([a-zA-Z])
+                         \|\
+                         ([^: ]+)?
+                         ([|:]\ )?
+                         (.+)
+                      \z/x) do
+      timestamp = $1
+      level = $2
+      id_section = $3
+      separator = $4
+      message = $5
+      timestamp = "1970-01-01 00:00:00.000000" if timestamp
+      case id_section
+      when nil
+      when /\|/
+        id_section = "PROCESS_ID|THREAD_ID"
+      when /[a-zA-Z]/
+        id_section = "THREAD_ID"
+      when /\A\d{8,}\z/
+        id_section = "THREAD_ID"
+      else
+        id_section = "PROCESS_ID"
+      end
+      message = message.gsub(/grn_init: <.+?>/, "grn_init: <VERSION>")
+      timestamp.to_s +
+        "|" +
+        level +
+        "|" +
+        id_section.to_s +
+        separator.to_s +
+        message
+    end
+  end
+
   private
   def run_command_interactive(*command_line)
     IO.pipe do |input_read, input_write|

  Added: test/command_line/suite/grndb/test_options.rb (+81 -0) 100644
===================================================================
--- /dev/null
+++ test/command_line/suite/grndb/test_options.rb    2019-05-21 18:31:42 +0900 (efe0cf795)
@@ -0,0 +1,81 @@
+# Copyright(C) 2019 Kouhei Sutou <kou****@clear*****>
+# Copyright(C) 2019 Kentaro Hayashi <hayas****@clear*****>
+#
+# This library is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public
+# License version 2.1 as published by the Free Software Foundation.
+#
+# This library is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this library; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
+
+class TestGrnDBOptions < GroongaTestCase
+  sub_test_case("--log-flags") do
+    test("default") do
+      groonga("status")
+      grndb("check")
+      assert_equal("1970-01-01 00:00:00.000000|n| " +
+                   "grn_fin (0)",
+                   normalize_init_line(File.readlines(@log_path).last))
+    end
+
+    test("add: one") do
+      flags = "+pid"
+      groonga("status")
+      grndb("check", "--log-flags", flags)
+      assert_equal("1970-01-01 00:00:00.000000|n|PROCESS_ID: " +
+                   "grn_fin (0)",
+                   normalize_init_line(File.readlines(@log_path).last))
+    end
+
+    test("add: multiple") do
+      flags = "+process_id|+thread_id"
+      groonga("status")
+      grndb("check", "--log-flags", flags)
+      assert_equal("1970-01-01 00:00:00.000000|n|PROCESS_ID|THREAD_ID: " +
+                   "grn_fin (0)",
+                   normalize_init_line(File.readlines(@log_path).last))
+    end
+
+    test("remove: one") do
+      flags = "-time"
+      groonga("status")
+      grndb("check", "--log-flags", flags)
+      assert_equal("|n| grn_fin (0)",
+                   normalize_init_line(File.readlines(@log_path).last))
+    end
+
+    test("remove: multiple") do
+      flags = "+pid|-time|-process_id"
+      groonga("status")
+      grndb("check", "--log-flags", flags)
+      assert_equal("|n| grn_fin (0)",
+                   normalize_init_line(File.readlines(@log_path).last))
+    end
+
+    test("replace") do
+      flags = "+process_id|default|+thread_id"
+      groonga("status")
+      grndb("check", "--log-flags", flags)
+      assert_equal("1970-01-01 00:00:00.000000|n|THREAD_ID: " +
+                   "grn_fin (0)",
+                   normalize_init_line(File.readlines(@log_path).last))
+    end
+
+    test("unknown") do
+      flags = "unknown"
+      groonga("status")
+      error = assert_raise(CommandRunner::Error) do
+        grndb("check", "--log-flags", flags)
+      end
+      assert_equal(<<-MESSAGE, error.error_output)
+#{real_grndb_path}: failed to parse log flags: <unknown>
+      MESSAGE
+    end
+  end
+end

  Modified: test/command_line/suite/groonga/test_options.rb (+0 -38)
===================================================================
--- test/command_line/suite/groonga/test_options.rb    2019-05-21 17:57:05 +0900 (081e5e9fc)
+++ test/command_line/suite/groonga/test_options.rb    2019-05-21 18:31:42 +0900 (3c07025e1)
@@ -15,44 +15,6 @@
 
 class TestGroongaOptions < GroongaTestCase
   sub_test_case("--log-flags") do
-    def normalize_init_line(line)
-      line.chomp.gsub(/\A
-                         (\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2}\.\d+)?
-                         \|\
-                         ([a-zA-Z])
-                         \|\
-                         ([^: ]+)?
-                         ([|:]\ )?
-                         (.+)
-                      \z/x) do
-        timestamp = $1
-        level = $2
-        id_section = $3
-        separator = $4
-        message = $5
-        timestamp = "1970-01-01 00:00:00.000000" if timestamp
-        case id_section
-        when nil
-        when /\|/
-          id_section = "PROCESS_ID|THREAD_ID"
-        when /[a-zA-Z]/
-          id_section = "THREAD_ID"
-        when /\A\d{8,}\z/
-          id_section = "THREAD_ID"
-        else
-          id_section = "PROCESS_ID"
-        end
-        message = message.gsub(/grn_init: <.+?>/, "grn_init: <VERSION>")
-        timestamp.to_s +
-          "|" +
-          level +
-          "|" +
-          id_section.to_s +
-          separator.to_s +
-          message
-      end
-    end
-
     test("add: one") do
       groonga("status",
               command_line: ["--log-flags", "+pid"])
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190521/33bc60f1/attachment-0001.html>


More information about the Groonga-commit mailing list
Zurück zum Archiv-Index