Mercurial > hg > freeDiameter
changeset 689:933d098fc75d
Cleanups in msg_log feature
author | Sebastien Decugis <sdecugis@nict.go.jp> |
---|---|
date | Thu, 20 Jan 2011 14:24:13 +0900 |
parents | 8c3dc8584dab |
children | a29e4201d511 |
files | include/freeDiameter/libfdproto.h libfdcore/core.c libfdproto/CMakeLists.txt libfdproto/fdproto-internal.h libfdproto/messages.c libfdproto/msg_log.c tests/tests.h |
diffstat | 7 files changed, 175 insertions(+), 131 deletions(-) [+] |
line wrap: on
line diff
--- a/include/freeDiameter/libfdproto.h Wed Jan 19 19:05:30 2011 +0900 +++ b/include/freeDiameter/libfdproto.h Thu Jan 20 14:24:13 2011 +0900 @@ -2067,7 +2067,7 @@ FD_MSG_LOGTO_DIR /* Messages are dumped in different files within one directory defined in arg. */ }; int fd_msg_log_config(enum fd_msg_log_cause cause, enum fd_msg_log_method method, const char * arg); -void ml_conf_init(struct dictionary *dict); +void fd_msg_log_init(struct dictionary *dict); /*********************************************/ /* Message metadata management functions */
--- a/libfdcore/core.c Wed Jan 19 19:05:30 2011 +0900 +++ b/libfdcore/core.c Thu Jan 20 14:24:13 2011 +0900 @@ -196,7 +196,7 @@ CHECK_FCT( fd_conf_init() ); /* Initialize the message logging facility */ - ml_conf_init(fd_g_config->cnf_dict); + fd_msg_log_init(fd_g_config->cnf_dict); /* Add definitions of the base protocol */ CHECK_FCT( fd_dict_base_protocol(fd_g_config->cnf_dict) );
--- a/libfdproto/CMakeLists.txt Wed Jan 19 19:05:30 2011 +0900 +++ b/libfdproto/CMakeLists.txt Thu Jan 20 14:24:13 2011 +0900 @@ -11,6 +11,7 @@ lists.c log.c messages.c + msg_log.c rt_data.c sessions.c )
--- a/libfdproto/fdproto-internal.h Wed Jan 19 19:05:30 2011 +0900 +++ b/libfdproto/fdproto-internal.h Thu Jan 20 14:24:13 2011 +0900 @@ -50,6 +50,9 @@ /* Where debug messages are sent */ FILE * fd_g_debug_fstr; +/* Special message dump function */ +void fd_msg_dump_fstr ( struct msg * msg, FILE * fstr ); + /* Iterator on the rules of a parent object */ int fd_dict_iterate_rules ( struct dict_object *parent, void * data, int (*cb)(void *, struct dict_rule_data *) );
--- a/libfdproto/messages.c Wed Jan 19 19:05:30 2011 +0900 +++ b/libfdproto/messages.c Thu Jan 20 14:24:13 2011 +0900 @@ -2324,130 +2324,3 @@ } -/***************************************************************************************************************/ -/* messages logging facility */ -#include <stdarg.h> - -static struct { - struct { - enum fd_msg_log_method meth; - const char * metharg; - } causes[FD_MSG_LOG_MAX + 1]; - pthread_mutex_t lock; - int init; - struct dictionary *dict; -} ml_conf = { .lock = PTHREAD_MUTEX_INITIALIZER, .init = 0 }; - -void ml_conf_init(struct dictionary *dict) -{ - memset(&ml_conf.causes, 0, sizeof(ml_conf.causes)); - ml_conf.init = 1; - ml_conf.dict = dict; -} - -/* Set a configuration property */ -int fd_msg_log_config(enum fd_msg_log_cause cause, enum fd_msg_log_method method, const char * arg) -{ - /* Check the parameters are valid */ - TRACE_ENTRY("%d %d %p", cause, method, arg); - CHECK_PARAMS( (cause >= 0) && (cause <= FD_MSG_LOG_MAX) ); - CHECK_PARAMS( (method >= FD_MSG_LOGTO_DEBUGONLY) && (method <= FD_MSG_LOGTO_DIR) ); - CHECK_PARAMS( (method == FD_MSG_LOGTO_DEBUGONLY) || (arg != NULL) ); - - /* Lock the configuration */ - CHECK_POSIX( pthread_mutex_lock(&ml_conf.lock) ); - if (!ml_conf.init) { - ASSERT(0); - } - - /* Now set the parameter */ - ml_conf.causes[cause].meth = method; - ml_conf.causes[cause].metharg = arg; - - if (method) { - TRACE_DEBUG(INFO, "Logging %s messages set to %s '%s'", - (cause == FD_MSG_LOG_DROPPED) ? "DROPPED" : - (cause == FD_MSG_LOG_RECEIVED) ? "RECEIVED" : - (cause == FD_MSG_LOG_SENT) ? "SENT" : - "???", - (method == FD_MSG_LOGTO_FILE) ? "file" : - (method == FD_MSG_LOGTO_DIR) ? "directory" : - "???", - arg); - } - - CHECK_POSIX( pthread_mutex_unlock(&ml_conf.lock) ); - - /* Done */ - return 0; -} - -/* Do not log anything within this one, since log lock is held */ -static void fd_cleanup_mutex_silent( void * mutex ) -{ - (void)pthread_mutex_unlock((pthread_mutex_t *)mutex); -} - -/* Really log the message */ -void fd_msg_log( enum fd_msg_log_cause cause, struct msg * msg, const char * prefix_format, ... ) -{ - va_list ap; - enum fd_msg_log_method meth; - const char * metharg; - FILE * fstr; - - TRACE_ENTRY("%d %p %p", cause, msg, prefix_format); - CHECK_PARAMS_DO( (cause >= 0) && (cause <= FD_MSG_LOG_MAX), - { - TRACE_DEBUG(INFO, "Invalid cause received (%d)! Message was:", cause); - fd_msg_dump_walk(INFO, msg); - } ); - - /* First retrieve the config for this message */ - CHECK_POSIX_DO( pthread_mutex_lock(&ml_conf.lock), ); - if (!ml_conf.init) { - ASSERT(0); - } - meth = ml_conf.causes[cause].meth; - metharg = ml_conf.causes[cause].metharg; - CHECK_POSIX_DO( pthread_mutex_unlock(&ml_conf.lock), ); - - /* Okay, now we will create the file descriptor */ - switch (meth) { - case FD_MSG_LOGTO_DEBUGONLY: - fstr = fd_g_debug_fstr; - break; - - case FD_MSG_LOGTO_FILE: - TODO("Log to arg file"); - TODO("Log a note to debug stream"); - break; - case FD_MSG_LOGTO_DIR: - TODO("Log to arg directory in a new file"); - TODO("Log a note to debug stream"); - break; - } - - /* For file methods, let's parse the message so it looks better */ - if ((meth != FD_MSG_LOGTO_DEBUGONLY) && ml_conf.dict) { - CHECK_FCT_DO( fd_msg_parse_dict( msg, ml_conf.dict, NULL ), ); - } - - /* Then dump the prefix message to this stream, & to debug stream */ - (void)pthread_mutex_lock(&fd_log_lock); - pthread_cleanup_push(fd_cleanup_mutex_silent, &fd_log_lock); - va_start(ap, prefix_format); - vfprintf( fstr, prefix_format, ap); - va_end(ap); - fflush(fstr); - pthread_cleanup_pop(0); - (void)pthread_mutex_unlock(&fd_log_lock); - fd_log_debug_fstr(fstr, "\n\n"); - - /* And now the message itself */ - fd_msg_dump_fstr(msg, fstr); - - /* And finally close the stream if needed */ - TODO("close?"); -} -
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/libfdproto/msg_log.c Thu Jan 20 14:24:13 2011 +0900 @@ -0,0 +1,165 @@ +/********************************************************************************************************* +* Software License Agreement (BSD License) * +* Author: Sebastien Decugis <sdecugis@nict.go.jp> * +* * +* Copyright (c) 2011, WIDE Project and NICT * +* All rights reserved. * +* * +* Redistribution and use of this software in source and binary forms, with or without modification, are * +* permitted provided that the following conditions are met: * +* * +* * Redistributions of source code must retain the above * +* copyright notice, this list of conditions and the * +* following disclaimer. * +* * +* * Redistributions in binary form must reproduce the above * +* copyright notice, this list of conditions and the * +* following disclaimer in the documentation and/or other * +* materials provided with the distribution. * +* * +* * Neither the name of the WIDE Project or NICT nor the * +* names of its contributors may be used to endorse or * +* promote products derived from this software without * +* specific prior written permission of WIDE Project and * +* NICT. * +* * +* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED * +* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A * +* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR * +* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT * +* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS * +* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR * +* TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF * +* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * +*********************************************************************************************************/ + +/* messages logging facility */ +#include "fdproto-internal.h" +#include <stdarg.h> + +/* Configuration of this module */ +static struct { + struct { + enum fd_msg_log_method meth; + const char * metharg; + } causes[FD_MSG_LOG_MAX + 1]; + pthread_mutex_t lock; + int init; + struct dictionary *dict; +} ml_conf = { .lock = PTHREAD_MUTEX_INITIALIZER, .init = 0 }; + + +/* Initialize the module, with a dictionary to use to better parse the messages */ +void fd_msg_log_init(struct dictionary *dict) +{ + memset(&ml_conf.causes, 0, sizeof(ml_conf.causes)); + ml_conf.init = 1; + ml_conf.dict = dict; +} + +/* Set a configuration property */ +int fd_msg_log_config(enum fd_msg_log_cause cause, enum fd_msg_log_method method, const char * arg) +{ + /* Check the parameters are valid */ + TRACE_ENTRY("%d %d %p", cause, method, arg); + CHECK_PARAMS( (cause >= 0) && (cause <= FD_MSG_LOG_MAX) ); + CHECK_PARAMS( (method >= FD_MSG_LOGTO_DEBUGONLY) && (method <= FD_MSG_LOGTO_DIR) ); + CHECK_PARAMS( (method == FD_MSG_LOGTO_DEBUGONLY) || (arg != NULL) ); + + /* Lock the configuration */ + CHECK_POSIX( pthread_mutex_lock(&ml_conf.lock) ); + if (!ml_conf.init) { + ASSERT(0); + } + + /* Now set the parameter */ + ml_conf.causes[cause].meth = method; + ml_conf.causes[cause].metharg = arg; + + if (method) { + TRACE_DEBUG(INFO, "Logging %s messages set to %s '%s'", + (cause == FD_MSG_LOG_DROPPED) ? "DROPPED" : + (cause == FD_MSG_LOG_RECEIVED) ? "RECEIVED" : + (cause == FD_MSG_LOG_SENT) ? "SENT" : + "???", + (method == FD_MSG_LOGTO_FILE) ? "file" : + (method == FD_MSG_LOGTO_DIR) ? "directory" : + "???", + arg); + } + + CHECK_POSIX( pthread_mutex_unlock(&ml_conf.lock) ); + + /* Done */ + return 0; +} + +/* Do not log anything within this one, since log lock is held */ +static void fd_cleanup_mutex_silent( void * mutex ) +{ + (void)pthread_mutex_unlock((pthread_mutex_t *)mutex); +} + +/* Really log the message */ +void fd_msg_log( enum fd_msg_log_cause cause, struct msg * msg, const char * prefix_format, ... ) +{ + va_list ap; + enum fd_msg_log_method meth; + const char * metharg; + FILE * fstr; + + TRACE_ENTRY("%d %p %p", cause, msg, prefix_format); + CHECK_PARAMS_DO( (cause >= 0) && (cause <= FD_MSG_LOG_MAX), + { + TRACE_DEBUG(INFO, "Invalid cause received (%d)! Message was:", cause); + fd_msg_dump_walk(INFO, msg); + } ); + + /* First retrieve the config for this message */ + CHECK_POSIX_DO( pthread_mutex_lock(&ml_conf.lock), ); + if (!ml_conf.init) { + ASSERT(0); + } + meth = ml_conf.causes[cause].meth; + metharg = ml_conf.causes[cause].metharg; + CHECK_POSIX_DO( pthread_mutex_unlock(&ml_conf.lock), ); + + /* Okay, now we will create the file descriptor */ + switch (meth) { + case FD_MSG_LOGTO_DEBUGONLY: + fstr = fd_g_debug_fstr; + break; + + case FD_MSG_LOGTO_FILE: + TODO("Log to arg file"); + TODO("Log a note to debug stream"); + break; + case FD_MSG_LOGTO_DIR: + TODO("Log to arg directory in a new file"); + TODO("Log a note to debug stream"); + break; + } + + /* For file methods, let's parse the message so it looks better */ + if ((meth != FD_MSG_LOGTO_DEBUGONLY) && ml_conf.dict) { + CHECK_FCT_DO( fd_msg_parse_dict( msg, ml_conf.dict, NULL ), ); + } + + /* Then dump the prefix message to this stream, & to debug stream */ + (void)pthread_mutex_lock(&fd_log_lock); + pthread_cleanup_push(fd_cleanup_mutex_silent, &fd_log_lock); + va_start(ap, prefix_format); + vfprintf( fstr, prefix_format, ap); + va_end(ap); + fflush(fstr); + pthread_cleanup_pop(0); + (void)pthread_mutex_unlock(&fd_log_lock); + fd_log_debug_fstr(fstr, "\n\n"); + + /* And now the message itself */ + fd_msg_dump_fstr(msg, fstr); + + /* And finally close the stream if needed */ + TODO("close?"); +} +
--- a/tests/tests.h Wed Jan 19 19:05:30 2011 +0900 +++ b/tests/tests.h Thu Jan 20 14:24:13 2011 +0900 @@ -67,8 +67,6 @@ #define FAILTEST( message... ){ \ fprintf(stderr, ## message); \ TRACE_DEBUG(INFO, "Test failed"); \ - (void)fd_core_shutdown(); \ - (void)fd_core_wait_shutdown_complete(); \ exit(FAIL); \ } @@ -78,6 +76,7 @@ TRACE_DEBUG(INFO, "Test passed"); \ (void)fd_core_shutdown(); \ (void)fd_core_wait_shutdown_complete(); \ + (void)fd_thr_term(&timeout_thr); \ exit(PASS); \ } @@ -185,6 +184,9 @@ /* Initialize the config */ CHECK( 0, fd_conf_init() ); + /* Initialize the message logging facility */ + fd_msg_log_init(fd_g_config->cnf_dict); + /* Add definitions of the base protocol */ CHECK( 0, fd_dict_base_protocol(fd_g_config->cnf_dict) );