Kouhei Sutou
null+****@clear*****
Wed Jan 9 18:53:34 JST 2013
Kouhei Sutou 2013-01-09 18:53:34 +0900 (Wed, 09 Jan 2013) New Revision: 5ff5cec986e8e118054dee6b6e7f2ed71de76f12 https://github.com/groonga/groonga/commit/5ff5cec986e8e118054dee6b6e7f2ed71de76f12 Log: Add grn_logger It supports reopen for custom logger. grn_logger_info API is deprecated. It doesn't break backward compatibility. Modified files: include/groonga.h lib/ctx.c lib/ctx.h Modified: include/groonga.h (+19 -0) =================================================================== --- include/groonga.h 2013-01-08 18:16:25 +0900 (e8fb8b2) +++ include/groonga.h 2013-01-09 18:53:34 +0900 (53a297b) @@ -2033,8 +2033,10 @@ GRN_API grn_rc grn_snip_get_result(grn_ctx *ctx, grn_snip *snip, const unsigned #define GRN_LOG_MESSAGE (0x01<<2) #define GRN_LOG_LOCATION (0x01<<3) +/* Deprecated since 2.1.2. Use grn_logger instead. */ typedef struct _grn_logger_info grn_logger_info; +/* Deprecated since 2.1.2. Use grn_logger instead. */ struct _grn_logger_info { grn_log_level max_level; int flags; @@ -2042,8 +2044,24 @@ struct _grn_logger_info { void *func_arg; }; +/* Deprecated since 2.1.2. Use grn_logger_set() instead. */ GRN_API grn_rc grn_logger_info_set(grn_ctx *ctx, const grn_logger_info *info); +typedef struct _grn_logger grn_logger; + +struct _grn_logger { + grn_log_level max_level; + int flags; + void *user_data; + void (*log)(grn_ctx *ctx, grn_log_level level, + const char *time, const char *title, const char *message, + const char *location, void *user_data); + void (*reopen)(grn_ctx *ctx, void *user_data); + void (*fin)(grn_ctx *ctx, void *user_data); +}; + +GRN_API grn_rc grn_logger_set(grn_ctx *ctx, const grn_logger *logger); + #ifdef __GNUC__ #define GRN_ATTRIBUTE_PRINTF(fmt_pos) \ __attribute__ ((format(printf, fmt_pos, fmt_pos + 1))) @@ -2053,6 +2071,7 @@ GRN_API grn_rc grn_logger_info_set(grn_ctx *ctx, const grn_logger_info *info); GRN_API void grn_logger_put(grn_ctx *ctx, grn_log_level level, const char *file, int line, const char *func, const char *fmt, ...) GRN_ATTRIBUTE_PRINTF(6); +GRN_API void grn_logger_reopen(grn_ctx *ctx); GRN_API int grn_logger_pass(grn_ctx *ctx, grn_log_level level); Modified: lib/ctx.c (+109 -50) =================================================================== --- lib/ctx.c 2013-01-08 18:16:25 +0900 (590f45f) +++ lib/ctx.c 2013-01-09 18:53:34 +0900 (473c9e7) @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2009-2012 Brazil + Copyright(C) 2009-2013 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -684,15 +684,16 @@ const char *grn_log_path = GRN_LOG_PATH; const char *grn_qlog_path = NULL; static FILE *default_logger_fp = NULL; -static grn_critical_section grn_logger_lock; +static grn_critical_section default_logger_lock; static void -default_logger_func(int level, const char *time, const char *title, - const char *msg, const char *location, void *func_arg) +default_logger_log(grn_ctx *ctx, grn_log_level level, + const char *time, const char *title, + const char *msg, const char *location, void *user_data) { const char slev[] = " EACewnid-"; if (grn_log_path) { - CRITICAL_SECTION_ENTER(grn_logger_lock); + CRITICAL_SECTION_ENTER(default_logger_lock); if (!default_logger_fp) { default_logger_fp = fopen(grn_log_path, "a"); } @@ -705,23 +706,61 @@ default_logger_func(int level, const char *time, const char *title, } fflush(default_logger_fp); } - CRITICAL_SECTION_LEAVE(grn_logger_lock); + CRITICAL_SECTION_LEAVE(default_logger_lock); } } -static grn_logger_info default_logger = { +static void +default_logger_reopen(grn_ctx *ctx, void *user_data) +{ + if (grn_log_path) { + GRN_LOG(ctx, GRN_LOG_NOTICE, "log will be closed."); + CRITICAL_SECTION_ENTER(default_logger_lock); + if (default_logger_fp) { + fclose(default_logger_fp); + default_logger_fp = NULL; + } + CRITICAL_SECTION_LEAVE(default_logger_lock); + GRN_LOG(ctx, GRN_LOG_NOTICE, "log opened."); + } +} + +static void +default_logger_fin(grn_ctx *ctx, void *user_data) +{ + CRITICAL_SECTION_ENTER(default_logger_lock); + if (default_logger_fp) { + fclose(default_logger_fp); + default_logger_fp = NULL; + } + CRITICAL_SECTION_LEAVE(default_logger_lock); +} + +static grn_logger default_logger = { GRN_LOG_DEFAULT_LEVEL, GRN_LOG_TIME|GRN_LOG_MESSAGE, - default_logger_func, - NULL + NULL, + default_logger_log, + default_logger_reopen, + default_logger_fin }; -static const grn_logger_info *grn_logger = &default_logger; +static grn_logger current_logger = { + GRN_LOG_DEFAULT_LEVEL, + GRN_LOG_TIME|GRN_LOG_MESSAGE, + NULL, + NULL, + NULL, + NULL +}; void -grn_default_logger_set_max_level(grn_log_level level) +grn_default_logger_set_max_level(grn_log_level max_level) { - default_logger.max_level = level; + default_logger.max_level = max_level; + if (current_logger.log == default_logger_log) { + current_logger.max_level = max_level; + } } grn_log_level @@ -730,6 +769,13 @@ grn_default_logger_get_max_level(void) return default_logger.max_level; } +void +grn_logger_reopen(grn_ctx *ctx) +{ + if (current_logger.reopen) { + current_logger.reopen(ctx, current_logger.user_data); + } +} static FILE *default_query_logger_file = NULL; static grn_critical_section default_query_logger_lock; @@ -817,22 +863,7 @@ grn_default_query_logger_get_flags(void) void grn_log_reopen(grn_ctx *ctx) { - if (grn_logger == &default_logger) { - if (grn_log_path) { - GRN_LOG(ctx, GRN_LOG_NOTICE, "log will be closed."); - CRITICAL_SECTION_ENTER(grn_logger_lock); - if (default_logger_fp) { - fclose(default_logger_fp); - default_logger_fp = NULL; - } - CRITICAL_SECTION_LEAVE(grn_logger_lock); - GRN_LOG(ctx, GRN_LOG_NOTICE, "log opened."); - } - } else { - ERR(GRN_FUNCTION_NOT_IMPLEMENTED, - "grn_log_reopen() is not implemented with a custom grn_logger."); - } - + grn_logger_reopen(ctx); grn_query_logger_reopen(ctx); } @@ -879,10 +910,10 @@ grn_init(void) { grn_rc rc; grn_ctx *ctx = &grn_gctx; - grn_logger = &default_logger; + memcpy(¤t_logger, &default_logger, sizeof(grn_logger)); memcpy(¤t_query_logger, &default_query_logger, sizeof(grn_query_logger)); CRITICAL_SECTION_INIT(grn_glock); - CRITICAL_SECTION_INIT(grn_logger_lock); + CRITICAL_SECTION_INIT(default_logger_lock); CRITICAL_SECTION_INIT(default_query_logger_lock); grn_gtick = 0; ctx->next = ctx; @@ -1055,9 +1086,9 @@ grn_fin(void) grn_ctx_fin(ctx); grn_com_fin(); GRN_LOG(ctx, GRN_LOG_NOTICE, "grn_fin (%d)", alloc_count); - grn_logger_fin(); + grn_logger_fin(ctx); CRITICAL_SECTION_FIN(default_query_logger_lock); - CRITICAL_SECTION_FIN(grn_logger_lock); + CRITICAL_SECTION_FIN(default_logger_lock); CRITICAL_SECTION_FIN(grn_glock); return GRN_SUCCESS; } @@ -2410,23 +2441,54 @@ grn_ctx_log(grn_ctx *ctx, const char *fmt, ...) } void -grn_logger_fin(void) +grn_logger_fin(grn_ctx *ctx) { - CRITICAL_SECTION_ENTER(grn_logger_lock); - if (default_logger_fp) { - fclose(default_logger_fp); - default_logger_fp = NULL; + if (current_logger.fin) { + current_logger.fin(ctx, current_logger.user_data); } - CRITICAL_SECTION_LEAVE(grn_logger_lock); +} + +static void +logger_info_func_wrapper(grn_ctx *ctx, grn_log_level level, + const char *time, const char *title, + const char *message, const char *location, + void *user_data) +{ + grn_logger_info *info = user_data; + info->func(level, time, title, message, location, info->func_arg); } grn_rc grn_logger_info_set(grn_ctx *ctx, const grn_logger_info *info) { if (info) { - grn_logger = info; + grn_logger logger; + + memset(&logger, 0, sizeof(grn_logger)); + logger.max_level = info->max_level; + logger.flags = info->flags; + if (info->func) { + logger.log = logger_info_func_wrapper; + logger.user_data = (grn_logger_info *)info; + } else { + logger.log = default_logger_log; + logger.reopen = default_logger_reopen; + logger.fin = default_logger_fin; + } + return grn_logger_set(ctx, &logger); } else { - grn_logger = &default_logger; + return grn_logger_set(ctx, NULL); + } +} + +grn_rc +grn_logger_set(grn_ctx *ctx, const grn_logger *logger) +{ + grn_logger_fin(ctx); + if (logger) { + current_logger = *logger; + } else { + current_logger = default_logger; } return GRN_SUCCESS; } @@ -2434,7 +2496,7 @@ grn_logger_info_set(grn_ctx *ctx, const grn_logger_info *info) int grn_logger_pass(grn_ctx *ctx, grn_log_level level) { - return level <= grn_logger->max_level; + return level <= current_logger.max_level; } #define TBUFSIZE GRN_TIMEVAL_STR_SIZE @@ -2445,17 +2507,17 @@ void grn_logger_put(grn_ctx *ctx, grn_log_level level, const char *file, int line, const char *func, const char *fmt, ...) { - if (level <= grn_logger->max_level) { + if (level <= current_logger.max_level && current_logger.log) { char tbuf[TBUFSIZE]; char mbuf[MBUFSIZE]; char lbuf[LBUFSIZE]; tbuf[0] = '\0'; - if (grn_logger->flags & GRN_LOG_TIME) { + if (current_logger.flags & GRN_LOG_TIME) { grn_timeval tv; grn_timeval_now(ctx, &tv); grn_timeval2str(ctx, &tv, tbuf); } - if (grn_logger->flags & GRN_LOG_MESSAGE) { + if (current_logger.flags & GRN_LOG_MESSAGE) { va_list argp; va_start(argp, fmt); vsnprintf(mbuf, MBUFSIZE - 1, fmt, argp); @@ -2464,17 +2526,14 @@ grn_logger_put(grn_ctx *ctx, grn_log_level level, } else { mbuf[0] = '\0'; } - if (grn_logger->flags & GRN_LOG_LOCATION) { + if (current_logger.flags & GRN_LOG_LOCATION) { snprintf(lbuf, LBUFSIZE - 1, "%d %s:%d %s()", getpid(), file, line, func); lbuf[LBUFSIZE - 1] = '\0'; } else { lbuf[0] = '\0'; } - if (grn_logger->func) { - grn_logger->func(level, tbuf, "", mbuf, lbuf, grn_logger->func_arg); - } else { - default_logger_func(level, tbuf, "", mbuf, lbuf, grn_logger->func_arg); - } + current_logger.log(ctx, level, tbuf, "", mbuf, lbuf, + current_logger.user_data); } } Modified: lib/ctx.h (+2 -2) =================================================================== --- lib/ctx.h 2013-01-08 18:16:25 +0900 (287f98e) +++ lib/ctx.h 2013-01-09 18:53:34 +0900 (b062c64) @@ -1,6 +1,6 @@ /* -*- c-basic-offset: 2 -*- */ /* - Copyright(C) 2009-2012 Brazil + Copyright(C) 2009-2013 Brazil This library is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public @@ -413,7 +413,7 @@ void grn_ctx_qe_fin(grn_ctx *ctx); void grn_ctx_loader_clear(grn_ctx *ctx); void grn_log_reopen(grn_ctx *ctx); -void grn_logger_fin(void); +void grn_logger_fin(grn_ctx *ctx); void grn_query_logger_fin(grn_ctx *ctx); GRN_API grn_rc grn_ctx_sendv(grn_ctx *ctx, int argc, char **argv, int flags); -------------- next part -------------- HTML����������������������������...Télécharger