Skip to content

Commit

Permalink
Add parser and dumper for query log flags
Browse files Browse the repository at this point in the history
They are new API.
  • Loading branch information
kou committed Jun 24, 2017
1 parent d7402e5 commit 1235f71
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
6 changes: 5 additions & 1 deletion include/groonga/groonga.h
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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);
Expand Down
3 changes: 3 additions & 0 deletions include/groonga/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
52 changes: 52 additions & 0 deletions lib/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
33 changes: 33 additions & 0 deletions lib/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down

0 comments on commit 1235f71

Please sign in to comment.