[Groonga-commit] groonga/groonga at ffe08c3 [master] groonga: add --log-flags option

Back to archive index
Kouhei Sutou null+****@clear*****
Mon Jan 21 14:44:11 JST 2019


Kouhei Sutou	2019-01-21 14:44:11 +0900 (Mon, 21 Jan 2019)

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

  Message:
    groonga: add --log-flags option

  Added files:
    test/command_line/suite/groonga/test_options.rb
  Modified files:
    include/groonga/groonga.h
    lib/logger.c
    src/groonga.c
    test/command_line/helper/command_runner.rb

  Modified: include/groonga/groonga.h (+16 -2)
===================================================================
--- include/groonga/groonga.h    2019-01-21 12:56:39 +0900 (2ccf88ed8)
+++ include/groonga/groonga.h    2019-01-21 14:44:11 +0900 (bb6af7004)
@@ -1,6 +1,6 @@
 /*
   Copyright(C) 2009-2018 Brazil
-  Copyright(C) 2018 Kouhei Sutou <kou****@clear*****>
+  Copyright(C) 2018-2019 Kouhei Sutou <kou****@clear*****>
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -1010,12 +1010,24 @@ GRN_API grn_rc grn_snip_get_result(grn_ctx *ctx, grn_obj *snip, const unsigned i
 
 /* log */
 
+#define GRN_LOG_NONE                   (0x00<<0)
 #define GRN_LOG_TIME                   (0x01<<0)
 #define GRN_LOG_TITLE                  (0x01<<1)
 #define GRN_LOG_MESSAGE                (0x01<<2)
 #define GRN_LOG_LOCATION               (0x01<<3)
 #define GRN_LOG_PID                    (0x01<<4)
+#define GRN_LOG_PROCESS_ID             GRN_LOG_PID
 #define GRN_LOG_THREAD_ID              (0x01<<5)
+#define GRN_LOG_ALL                             \
+  (GRN_LOG_TIME |                               \
+   GRN_LOG_TITLE |                              \
+   GRN_LOG_MESSAGE |                            \
+   GRN_LOG_LOCATION |                           \
+   GRN_LOG_PROCESS_ID |                         \
+   GRN_LOG_THREAD_ID)
+#define GRN_LOG_DEFAULT                         \
+  (GRN_LOG_TIME |                               \
+   GRN_LOG_MESSAGE)
 
 /* Deprecated since 2.1.2. Use grn_logger instead. */
 typedef struct _grn_logger_info grn_logger_info;
@@ -1044,7 +1056,9 @@ struct _grn_logger {
   void (*fin)(grn_ctx *ctx, void *user_data);
 };
 
-GRN_API int grn_log_flags_parse(const char *string);
+GRN_API grn_bool grn_log_flags_parse(const char *string,
+                                     int string_size,
+                                     int *flags);
 
 GRN_API grn_rc grn_logger_set(grn_ctx *ctx, const grn_logger *logger);
 

  Modified: lib/logger.c (+66 -21)
===================================================================
--- lib/logger.c    2019-01-21 12:56:39 +0900 (4e7ba7665)
+++ lib/logger.c    2019-01-21 14:44:11 +0900 (42a7a04f9)
@@ -1,7 +1,7 @@
 /* -*- c-basic-offset: 2 -*- */
 /*
   Copyright(C) 2009-2017 Brazil
-  Copyright(C) 2018 Kouhei Sutou <kou****@clear*****>
+  Copyright(C) 2018-2019 Kouhei Sutou <kou****@clear*****>
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -46,9 +46,15 @@ static const char *log_level_names[] = {
 
 #define GRN_LOG_LAST GRN_LOG_DUMP
 
+typedef enum {
+  GRN_FLAGS_OPERATOR_ADD,
+  GRN_FLAGS_OPERATOR_REMOVE,
+  GRN_FLAGS_OPERATOR_REPLACE,
+} grn_flags_operator;
+
 #define INITIAL_LOGGER {                        \
   GRN_LOG_DEFAULT_LEVEL,                        \
-  GRN_LOG_TIME|GRN_LOG_MESSAGE,                 \
+  GRN_LOG_DEFAULT,                              \
   NULL,                                         \
   NULL,                                         \
   NULL,                                         \
@@ -118,38 +124,77 @@ grn_log_level_parse(const char *string, grn_log_level *level)
   }
 }
 
-int
-grn_log_flags_parse(const char *string)
+grn_bool
+grn_log_flags_parse(const char *string,
+                    int string_size,
+                    int *flags)
 {
-  const char *string_end = string + strlen(string);
-  int flags = GRN_LOG_TIME|GRN_LOG_MESSAGE;
+  const char *string_end;
+
+  *flags = GRN_LOG_DEFAULT;
 
   if (!string) {
-    return flags;
+    return GRN_TRUE;
   }
 
+  if (string_size < 0) {
+    string_size = strlen(string);
+  }
+
+  string_end = string + string_size;
+
   while (string < string_end) {
+    grn_flags_operator operator = GRN_FLAGS_OPERATOR_REPLACE;
+
     if (*string == '|' || *string == ' ') {
       string += 1;
       continue;
     }
 
-    if (strcmp(string, "+pid")) {
-      flags |= GRN_LOG_PID;
-      string += strlen(string);
-      continue;
-    } else if (strcmp(string, "+thread-id")) {
-      flags |= GRN_LOG_THREAD_ID;
-      string += strlen(string);
-      continue;
-    } else if (strcmp(string, "+location")) {
-      flags |= GRN_LOG_LOCATION;
-      string += strlen(string);
-      continue;
+    if (*string == '+') {
+      operator = GRN_FLAGS_OPERATOR_ADD;
+      string++;
+    } else if (*string == '-') {
+      operator = GRN_FLAGS_OPERATOR_REMOVE;
+      string++;
     }
+
+#define CHECK_FLAG(name)                                        \
+    if (((string_end - string) >= (sizeof(#name) - 1)) &&       \
+        (strncasecmp(string, #name, sizeof(#name) - 1) == 0) &&  \
+        (((string_end - string) == (sizeof(#name) - 1)) ||      \
+         (string[sizeof(#name) - 1] == '|') ||                  \
+         (string[sizeof(#name) - 1] == ' ') ||                  \
+         (string[sizeof(#name) - 1] == '+') ||                  \
+         (string[sizeof(#name) - 1] == '-'))) {                 \
+      if (operator == GRN_FLAGS_OPERATOR_ADD) {                 \
+        *flags |= GRN_LOG_ ## name;                             \
+      } else if (operator == GRN_FLAGS_OPERATOR_REMOVE) {       \
+        *flags &= ~GRN_LOG_ ## name;                            \
+      } else {                                                  \
+        *flags = GRN_LOG_ ## name;                              \
+      }                                                         \
+      string += sizeof(#name) - 1;                              \
+      continue;                                                 \
+    }
+
+    CHECK_FLAG(NONE);
+    CHECK_FLAG(TIME);
+    CHECK_FLAG(TITLE);
+    CHECK_FLAG(MESSAGE);
+    CHECK_FLAG(LOCATION);
+    CHECK_FLAG(PID);
+    CHECK_FLAG(PROCESS_ID);
+    CHECK_FLAG(THREAD_ID);
+    CHECK_FLAG(ALL);
+    CHECK_FLAG(DEFAULT);
+
+#undef CHECK_FLAG
+
+    return GRN_FALSE;
   }
 
-  return flags;
+  return GRN_TRUE;
 }
 
 static void
@@ -269,7 +314,7 @@ default_logger_fin(grn_ctx *ctx, void *user_data)
 
 static grn_logger default_logger = {
   GRN_LOG_DEFAULT_LEVEL,
-  GRN_LOG_TIME|GRN_LOG_MESSAGE,
+  GRN_LOG_DEFAULT,
   NULL,
   default_logger_log,
   default_logger_reopen,

  Modified: src/groonga.c (+20 -4)
===================================================================
--- src/groonga.c    2019-01-21 12:56:39 +0900 (edb497730)
+++ src/groonga.c    2019-01-21 14:44:11 +0900 (42854c98f)
@@ -1,7 +1,7 @@
 /* -*- c-basic-offset: 2 -*- */
 /*
   Copyright(C) 2009-2018 Brazil
-  Copyright(C) 2018 Kouhei Sutou <kou****@clear*****>
+  Copyright(C) 2018-2019 Kouhei Sutou <kou****@clear*****>
 
   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
@@ -3189,6 +3189,18 @@ show_usage(FILE *output)
           "                           [none|emergency|alert|critical|\n"
           "                            error|warning|notice|info|debug|dump]\n"
           "                           (default: %s)\n"
+          "      --log-flags <log flags>:\n"
+          "                           specify log flags\n"
+          "                           '+' prefix means that 'add the flag'\n"
+          "                           '-' prefix means that 'remove the flag'\n"
+          "                           No prefix means that 'replace existing flags'\n"
+          "                           Multiple log flags can be specified by\n"
+          "                           separating flags with '|'\n"
+          "                           Example: default|+pid|-time\n"
+          "                           [none|time|title|message|location|\n"
+          "                            pid|process_id|thread_id|\n"
+          "                            all|default]\n"
+          "                           (default: %s)\n"
           "      --log-path <path>:   specify log path\n"
           "                           (default: %s)\n"
           "      --log-rotate-threshold-size <threshold>:\n"
@@ -3239,6 +3251,7 @@ show_usage(FILE *output)
           default_default_request_timeout,
           listen_backlog,
           grn_log_level_to_string(default_log_level),
+          "time|message", /* TODO: Generate from GRN_LOG_DEFAULT */
           default_log_path, default_query_log_path,
           default_config_path, default_default_command_version,
           (long long int)default_default_match_escalation_threshold,
@@ -3526,10 +3539,13 @@ main(int argc, char **argv)
   }
 
   if (log_flags_arg) {
-    int log_flags = grn_log_flags_parse(log_flags_arg);
-    if (!log_flags) {
-      grn_default_logger_set_flags(log_flags);
+    int log_flags = GRN_LOG_DEFAULT;
+    if (!grn_log_flags_parse(log_flags_arg, -1, &log_flags)) {
+      fprintf(stderr, "invalid log flags: <%s>\n",
+              log_flags_arg);
+      return EXIT_FAILURE;
     }
+    grn_default_logger_set_flags(log_flags);
   }
 
   if (query_log_path_arg) {

  Modified: test/command_line/helper/command_runner.rb (+22 -0)
===================================================================
--- test/command_line/helper/command_runner.rb    2019-01-21 12:56:39 +0900 (61ffd457d)
+++ test/command_line/helper/command_runner.rb    2019-01-21 14:44:11 +0900 (7e4440c29)
@@ -1,3 +1,18 @@
+# Copyright(C) 2016-2019 Kouhei Sutou <kou****@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
+
 module CommandRunner
   class Error < StandardError
     attr_reader :output
@@ -49,11 +64,18 @@ module CommandRunner
   end
 
   def groonga(*groonga_command_line, &block)
+    if groonga_command_line.last.is_a?(Hash)
+      options = groonga_command_line.pop
+    end
     command_line = [
       groonga_path,
       "--log-path", @log_path.to_s,
       "--query-log-path", @query_log_path.to_s,
     ]
+    if options
+      more_command_line = options[:command_line]
+      command_line.concat(more_command_line) if more_command_line
+    end
     command_line << "-n" unless @database_path.exist?
     command_line << @database_path.to_s
     command_line.concat(groonga_command_line)

  Added: test/command_line/suite/groonga/test_options.rb (+94 -0) 100644
===================================================================
--- /dev/null
+++ test/command_line/suite/groonga/test_options.rb    2019-01-21 14:44:11 +0900 (081e5e9fc)
@@ -0,0 +1,94 @@
+# Copyright(C) 2019 Kouhei Sutou <kou****@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 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"])
+      assert_equal("1970-01-01 00:00:00.000000|n|PROCESS_ID: " +
+                   "grn_init: <VERSION>",
+                   normalize_init_line(File.readlines(@log_path).first))
+    end
+
+    test("add: multiple") do
+      groonga("status",
+              command_line: ["--log-flags", "+process_id|+thread_id"])
+      assert_equal("1970-01-01 00:00:00.000000|n|PROCESS_ID|THREAD_ID: " +
+                   "grn_init: <VERSION>",
+                   normalize_init_line(File.readlines(@log_path).first))
+    end
+
+    test("remove: one") do
+      groonga("status",
+              command_line: ["--log-flags", "-time"])
+      assert_equal("|n| grn_init: <VERSION>",
+                   normalize_init_line(File.readlines(@log_path).first))
+    end
+
+    test("remove: multiple") do
+      groonga("status",
+              command_line: ["--log-flags", "+pid|-time|-process_id"])
+      assert_equal("|n| grn_init: <VERSION>",
+                   normalize_init_line(File.readlines(@log_path).first))
+    end
+
+    test("replace") do
+      groonga("status",
+              command_line: ["--log-flags", "+process_id|default|+thread_id"])
+      assert_equal("1970-01-01 00:00:00.000000|n|THREAD_ID: " +
+                   "grn_init: <VERSION>",
+                   normalize_init_line(File.readlines(@log_path).first))
+    end
+  end
+end
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.osdn.me/mailman/archives/groonga-commit/attachments/20190121/6edd01cb/attachment-0001.html>


More information about the Groonga-commit mailing list
Back to archive index