diff --git a/include/groonga/groonga.h b/include/groonga/groonga.h index 790ca4a5ac..f3f473e543 100644 --- a/include/groonga/groonga.h +++ b/include/groonga/groonga.h @@ -1,5 +1,5 @@ /* - Copyright(C) 2009-2016 Brazil + Copyright(C) 2009-2017 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -1121,6 +1121,10 @@ struct _grn_query_logger { void (*fin)(grn_ctx *ctx, void *user_data); }; +GRN_API grn_bool grn_query_log_flags_parse(const char *string, + int string_size, + unsigned int *flags); + GRN_API grn_rc grn_query_logger_set(grn_ctx *ctx, const grn_query_logger *logger); GRN_API void grn_query_logger_set_flags(grn_ctx *ctx, unsigned int flags); GRN_API void grn_query_logger_add_flags(grn_ctx *ctx, unsigned int flags); diff --git a/include/groonga/util.h b/include/groonga/util.h index 9195df1ea3..0a574c665b 100644 --- a/include/groonga/util.h +++ b/include/groonga/util.h @@ -31,6 +31,9 @@ GRN_API grn_obj *grn_inspect_limited(grn_ctx *ctx, GRN_API grn_obj *grn_inspect_name(grn_ctx *ctx, grn_obj *buffer, grn_obj *obj); GRN_API grn_obj *grn_inspect_encoding(grn_ctx *ctx, grn_obj *buffer, grn_encoding encoding); GRN_API grn_obj *grn_inspect_type(grn_ctx *ctx, grn_obj *buffer, unsigned char type); +GRN_API grn_obj *grn_inspect_query_log_flags(grn_ctx *ctx, + grn_obj *buffer, + unsigned int flags); GRN_API void grn_p(grn_ctx *ctx, grn_obj *obj); GRN_API void grn_p_geo_point(grn_ctx *ctx, grn_geo_point *point); diff --git a/lib/logger.c b/lib/logger.c index de1540b0a0..f7c8d5a075 100644 --- a/lib/logger.c +++ b/lib/logger.c @@ -459,6 +459,58 @@ static grn_critical_section default_query_logger_lock; static off_t default_query_logger_size = 0; static off_t default_query_logger_rotate_threshold_size = 0; +grn_bool +grn_query_log_flags_parse(const char *string, + int string_size, + unsigned int *flags) +{ + const char *string_end; + + *flags = GRN_QUERY_LOG_NONE; + + if (!string) { + return GRN_TRUE; + } + + if (string_size < 0) { + string_size = strlen(string); + } + + string_end = string + string_size; + + while (string < string_end) { + if (*string == '|' || *string == ' ') { + string += 1; + continue; + } + +#define CHECK_FLAG(name) \ + if (((string_end - string) >= (sizeof(#name) - 1)) && \ + (memcmp(string, #name, sizeof(#name) - 1) == 0) && \ + (((string_end - string) == (sizeof(#name) - 1)) || \ + (string[sizeof(#name) - 1] == '|') || \ + (string[sizeof(#name) - 1] == ' '))) { \ + *flags |= GRN_QUERY_LOG_ ## name; \ + string += sizeof(#name) - 1; \ + continue; \ + } + + CHECK_FLAG(NONE); + CHECK_FLAG(COMMAND); + CHECK_FLAG(RESULT_CODE); + CHECK_FLAG(DESTINATION); + CHECK_FLAG(CACHE); + CHECK_FLAG(SIZE); + CHECK_FLAG(SCORE); + +#undef CHECK_FLAG + + return GRN_FALSE; + } + + return GRN_TRUE; +} + static void default_query_logger_log(grn_ctx *ctx, unsigned int flag, const char *timestamp, const char *info, diff --git a/lib/util.c b/lib/util.c index cc57e2f4c6..27fc944d6a 100644 --- a/lib/util.c +++ b/lib/util.c @@ -248,6 +248,39 @@ grn_inspect_type(grn_ctx *ctx, grn_obj *buf, unsigned char type) return buf; } + +grn_obj * +grn_inspect_query_log_flags(grn_ctx *ctx, grn_obj *buffer, unsigned int flags) +{ + grn_bool have_content = GRN_FALSE; + + if (flags == GRN_QUERY_LOG_NONE) { + GRN_TEXT_PUTS(ctx, buffer, "NONE"); + return buffer; + } + +#define CHECK_FLAG(NAME) do { \ + if (flags & GRN_QUERY_LOG_ ## NAME) { \ + if (have_content) { \ + GRN_TEXT_PUTS(ctx, buffer, "|"); \ + } \ + GRN_TEXT_PUTS(ctx, buffer, #NAME); \ + have_content = GRN_TRUE; \ + } \ + } while (GRN_FALSE) + + CHECK_FLAG(COMMAND); + CHECK_FLAG(RESULT_CODE); + CHECK_FLAG(DESTINATION); + CHECK_FLAG(CACHE); + CHECK_FLAG(SIZE); + CHECK_FLAG(SCORE); + +#undef CHECK_FALG + + return buffer; +} + static grn_rc grn_proc_inspect(grn_ctx *ctx, grn_obj *buf, grn_obj *obj) {