[Groonga-commit] groonga/groonga at c7b9b11 [master] Add grn_log_level_to_string() and grn_log_level_parse()

Zurück zum Archiv-Index

Kouhei Sutou null+****@clear*****
Mon Jul 27 17:22:11 JST 2015


Kouhei Sutou	2015-07-27 17:22:11 +0900 (Mon, 27 Jul 2015)

  New Revision: c7b9b11aa2afbedef1af0eff7a0e8f755e8bb688
  https://github.com/groonga/groonga/commit/c7b9b11aa2afbedef1af0eff7a0e8f755e8bb688

  Message:
    Add grn_log_level_to_string() and grn_log_level_parse()

  Added files:
    test/unit/core/test-log-level.c
  Modified files:
    include/groonga/groonga.h
    include/groonga/portability.h
    lib/logger.c
    test/unit/core/Makefile.am

  Modified: include/groonga/groonga.h (+3 -0)
===================================================================
--- include/groonga/groonga.h    2015-07-27 16:34:57 +0900 (6c08593)
+++ include/groonga/groonga.h    2015-07-27 17:22:11 +0900 (ebae344)
@@ -162,6 +162,9 @@ typedef enum {
   GRN_LOG_DUMP
 } grn_log_level;
 
+GRN_API const char *grn_log_level_to_string(grn_log_level level);
+GRN_API grn_bool grn_log_level_parse(const char *string, grn_log_level *level);
+
 /* query log flags */
 #define GRN_QUERY_LOG_NONE        (0x00)
 #define GRN_QUERY_LOG_COMMAND     (0x01<<0)

  Modified: include/groonga/portability.h (+8 -0)
===================================================================
--- include/groonga/portability.h    2015-07-27 16:34:57 +0900 (1d76376)
+++ include/groonga/portability.h    2015-07-27 17:22:11 +0900 (cc68542)
@@ -118,6 +118,14 @@
 #endif /* WIN32 */
 
 #ifdef WIN32
+# define grn_strcasecmp(string1, string2)       \
+  _stricmp((string1), (string2))
+#else /* WIN32 */
+# define grn_strcasecmp(string1, string2)       \
+  strcasecmp((string1), (string2))
+#endif /* WIN32 */
+
+#ifdef WIN32
 # define grn_snprintf(dest, dest_size, n, format, ...)          \
   _snprintf_s((dest), (dest_size), (n) - 1, (format), __VA_ARGS__)
 #else /* WIN32 */

  Modified: lib/logger.c (+76 -0)
===================================================================
--- lib/logger.c    2015-07-27 16:34:57 +0900 (10fb3c1)
+++ lib/logger.c    2015-07-27 17:22:11 +0900 (ffd913b)
@@ -32,6 +32,82 @@
 # define fileno(file) _fileno(file)
 #endif
 
+static const char *log_level_names[] = {
+  "none",
+  "emergency",
+  "alert",
+  "critical",
+  "error",
+  "warning",
+  "notice",
+  "info",
+  "debug",
+  "dump"
+};
+
+#define GRN_LOG_LAST GRN_LOG_DUMP
+
+const char *
+grn_log_level_to_string(grn_log_level level)
+{
+  if (level <= GRN_LOG_LAST) {
+    return log_level_names[level];
+  } else {
+    return "unknown";
+  }
+}
+
+grn_bool
+grn_log_level_parse(const char *string, grn_log_level *level)
+{
+  if (strcmp(string, " ") == 0 ||
+      grn_strcasecmp(string, "none") == 0) {
+    *level = GRN_LOG_NONE;
+    return GRN_TRUE;
+  } else if (strcmp(string, "E") == 0 ||
+             grn_strcasecmp(string, "emerg") == 0 ||
+             grn_strcasecmp(string, "emergency") == 0) {
+    *level = GRN_LOG_EMERG;
+    return GRN_TRUE;
+  } else if (strcmp(string, "A") == 0 ||
+             grn_strcasecmp(string, "alert") == 0) {
+    *level = GRN_LOG_ALERT;
+    return GRN_TRUE;
+  } else if (strcmp(string, "C") == 0 ||
+             grn_strcasecmp(string, "crit") == 0 ||
+             grn_strcasecmp(string, "critical") == 0) {
+    *level = GRN_LOG_CRIT;
+    return GRN_TRUE;
+  } else if (strcmp(string, "e") == 0 ||
+             grn_strcasecmp(string, "error") == 0) {
+    *level = GRN_LOG_ERROR;
+    return GRN_TRUE;
+  } else if (strcmp(string, "w") == 0 ||
+             grn_strcasecmp(string, "warn") == 0 ||
+             grn_strcasecmp(string, "warning") == 0) {
+    *level = GRN_LOG_WARNING;
+    return GRN_TRUE;
+  } else if (strcmp(string, "n") == 0 ||
+             grn_strcasecmp(string, "notice") == 0) {
+    *level = GRN_LOG_NOTICE;
+    return GRN_TRUE;
+  } else if (strcmp(string, "i") == 0 ||
+             grn_strcasecmp(string, "info") == 0) {
+    *level = GRN_LOG_INFO;
+    return GRN_TRUE;
+  } else if (strcmp(string, "d") == 0 ||
+             grn_strcasecmp(string, "debug") == 0) {
+    *level = GRN_LOG_DEBUG;
+    return GRN_TRUE;
+  } else if (strcmp(string, "-") == 0 ||
+             grn_strcasecmp(string, "dump") == 0) {
+    *level = GRN_LOG_DUMP;
+    return GRN_TRUE;
+  } else {
+    return GRN_FALSE;
+  }
+}
+
 static void
 rotate_log_file(grn_ctx *ctx, const char *current_path)
 {

  Modified: test/unit/core/Makefile.am (+2 -0)
===================================================================
--- test/unit/core/Makefile.am    2015-07-27 16:34:57 +0900 (54dbdd5)
+++ test/unit/core/Makefile.am    2015-07-27 17:22:11 +0900 (b4943ba)
@@ -37,6 +37,7 @@ noinst_LTLIBRARIES =				\
 	test-function-edit-distance.la		\
 	test-store-ja.la			\
 	test-log.la				\
+	test-log-level.la			\
 	test-table-sort-key-from-str.la		\
 	test-inspect.la				\
 	test-command-load.la			\
@@ -125,6 +126,7 @@ test_function_la_SOURCES		= test-function.c
 test_function_edit_distance_la_SOURCES	= test-function-edit-distance.c
 test_store_ja_la_SOURCES		= test-store-ja.c
 test_log_la_SOURCES			= test-log.c
+test_log_level_la_SOURCES		= test-log-level.c
 test_table_sort_key_from_str_la_SOURCES	= test-table-sort-key-from-str.c
 test_inspect_la_SOURCES			= test-inspect.c
 test_command_load_la_SOURCES		= test-command-load.c

  Added: test/unit/core/test-log-level.c (+153 -0) 100644
===================================================================
--- /dev/null
+++ test/unit/core/test-log-level.c    2015-07-27 17:22:11 +0900 (d4ccb26)
@@ -0,0 +1,153 @@
+/* -*- c-basic-offset: 2; coding: utf-8 -*- */
+/*
+  Copyright (C) 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
+  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
+*/
+
+#include <groonga.h>
+
+#include <gcutter.h>
+#include <glib/gstdio.h>
+
+#include "../lib/grn-assertions.h"
+
+void data_to_string(void);
+void test_to_string(gconstpointer data);
+void data_parse_success(void);
+void test_parse_success(gconstpointer data);
+void data_parse_failure(void);
+void test_parse_failure(gconstpointer data);
+
+void
+data_to_string(void)
+{
+#define ADD_DATUM(expected, level)                                      \
+  gcut_add_datum(#expected,                                             \
+                 "expected", G_TYPE_STRING, expected,                   \
+                 "level", G_TYPE_INT, level,                            \
+                 NULL)
+
+  ADD_DATUM("none",      GRN_LOG_NONE);
+  ADD_DATUM("emergency", GRN_LOG_EMERG);
+  ADD_DATUM("alert",     GRN_LOG_ALERT);
+  ADD_DATUM("critical",  GRN_LOG_CRIT);
+  ADD_DATUM("error",     GRN_LOG_ERROR);
+  ADD_DATUM("warning",   GRN_LOG_WARNING);
+  ADD_DATUM("notice",    GRN_LOG_NOTICE);
+  ADD_DATUM("info",      GRN_LOG_INFO);
+  ADD_DATUM("debug",     GRN_LOG_DEBUG);
+  ADD_DATUM("dump",      GRN_LOG_DUMP);
+  ADD_DATUM("unknown",   GRN_LOG_DUMP + 1);
+
+#undef ADD_DATUM
+}
+
+void
+test_to_string(gconstpointer data)
+{
+  const gchar *expected;
+  grn_log_level level;
+
+  expected = gcut_data_get_string(data, "expected");
+  level = gcut_data_get_int(data, "level");
+  cut_assert_equal_string(expected,
+                          grn_log_level_to_string(level));
+}
+
+void
+data_parse_success(void)
+{
+#define ADD_DATUM(label, expected, input)                               \
+  gcut_add_datum(label,                                                 \
+                 "expected", G_TYPE_INT, expected,                      \
+                 "input", G_TYPE_STRING, input,                         \
+                 NULL)
+
+  ADD_DATUM("none",      GRN_LOG_NONE,   " ");
+  ADD_DATUM("none",      GRN_LOG_NONE,   "none");
+  ADD_DATUM("none",      GRN_LOG_NONE,   "NONE");
+  ADD_DATUM("emergency", GRN_LOG_EMERG,  "E");
+  ADD_DATUM("emergency", GRN_LOG_EMERG,  "emerg");
+  ADD_DATUM("emergency", GRN_LOG_EMERG,  "emergency");
+  ADD_DATUM("emergency", GRN_LOG_EMERG,  "EMERG");
+  ADD_DATUM("emergency", GRN_LOG_EMERG,  "EMERGENCY");
+  ADD_DATUM("alert",     GRN_LOG_ALERT,  "A");
+  ADD_DATUM("alert",     GRN_LOG_ALERT,  "alert");
+  ADD_DATUM("alert",     GRN_LOG_ALERT,  "ALERT");
+  ADD_DATUM("critical",  GRN_LOG_CRIT,   "C");
+  ADD_DATUM("critical",  GRN_LOG_CRIT,   "crit");
+  ADD_DATUM("critical",  GRN_LOG_CRIT,   "critical");
+  ADD_DATUM("critical",  GRN_LOG_CRIT,   "CRIT");
+  ADD_DATUM("critical",  GRN_LOG_CRIT,   "CRITICAL");
+  ADD_DATUM("error",     GRN_LOG_ERROR,  "e");
+  ADD_DATUM("error",     GRN_LOG_ERROR,  "error");
+  ADD_DATUM("error",     GRN_LOG_ERROR,  "ERROR");
+  ADD_DATUM("warning",   GRN_LOG_WARNING, "w");
+  ADD_DATUM("warning",   GRN_LOG_WARNING, "warn");
+  ADD_DATUM("warning",   GRN_LOG_WARNING, "warning");
+  ADD_DATUM("warning",   GRN_LOG_WARNING, "WARN");
+  ADD_DATUM("warning",   GRN_LOG_WARNING, "WARNING");
+  ADD_DATUM("notice",    GRN_LOG_NOTICE,  "n");
+  ADD_DATUM("notice",    GRN_LOG_NOTICE,  "notice");
+  ADD_DATUM("notice",    GRN_LOG_NOTICE,  "NOTICE");
+  ADD_DATUM("info",      GRN_LOG_INFO,    "i");
+  ADD_DATUM("info",      GRN_LOG_INFO,    "info");
+  ADD_DATUM("info",      GRN_LOG_INFO,    "INFO");
+  ADD_DATUM("debug",     GRN_LOG_DEBUG,   "d");
+  ADD_DATUM("debug",     GRN_LOG_DEBUG,   "debug");
+  ADD_DATUM("debug",     GRN_LOG_DEBUG,   "DEBUG");
+  ADD_DATUM("dump",      GRN_LOG_DUMP,    "-");
+  ADD_DATUM("dump",      GRN_LOG_DUMP,    "dump");
+  ADD_DATUM("dump",      GRN_LOG_DUMP,    "DUMP");
+
+#undef ADD_DATUM
+}
+
+void
+test_parse_success(gconstpointer data)
+{
+  grn_log_level expected;
+  const gchar *input;
+  grn_log_level level;
+
+  expected = gcut_data_get_int(data, "expected");
+  input = gcut_data_get_string(data, "input");
+  cut_assert_true(grn_log_level_parse(input, &level));
+  cut_assert_equal_int(expected, level);
+}
+
+void
+data_parse_failure(void)
+{
+#define ADD_DATUM(label, input)                                         \
+  gcut_add_datum(label,                                                 \
+                 "input", G_TYPE_STRING, input,                         \
+                 NULL)
+
+  ADD_DATUM("one character", "X");
+  ADD_DATUM("unknown name",  "unknown");
+
+#undef ADD_DATUM
+}
+
+void
+test_parse_failure(gconstpointer data)
+{
+  const gchar *input;
+  grn_log_level level;
+
+  input = gcut_data_get_string(data, "input");
+  cut_assert_false(grn_log_level_parse(input, &level));
+}
-------------- next part --------------
HTML����������������������������...
Download 



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