comparison libfdproto/messages.c @ 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
comparison
equal deleted inserted replaced
688:8c3dc8584dab 689:933d098fc75d
2322 CHECK_POSIX_DO(pthread_rwlock_unlock(&fd_disp_lock), /* ignore */ ); 2322 CHECK_POSIX_DO(pthread_rwlock_unlock(&fd_disp_lock), /* ignore */ );
2323 return ret; 2323 return ret;
2324 } 2324 }
2325 2325
2326 2326
2327 /***************************************************************************************************************/
2328 /* messages logging facility */
2329 #include <stdarg.h>
2330
2331 static struct {
2332 struct {
2333 enum fd_msg_log_method meth;
2334 const char * metharg;
2335 } causes[FD_MSG_LOG_MAX + 1];
2336 pthread_mutex_t lock;
2337 int init;
2338 struct dictionary *dict;
2339 } ml_conf = { .lock = PTHREAD_MUTEX_INITIALIZER, .init = 0 };
2340
2341 void ml_conf_init(struct dictionary *dict)
2342 {
2343 memset(&ml_conf.causes, 0, sizeof(ml_conf.causes));
2344 ml_conf.init = 1;
2345 ml_conf.dict = dict;
2346 }
2347
2348 /* Set a configuration property */
2349 int fd_msg_log_config(enum fd_msg_log_cause cause, enum fd_msg_log_method method, const char * arg)
2350 {
2351 /* Check the parameters are valid */
2352 TRACE_ENTRY("%d %d %p", cause, method, arg);
2353 CHECK_PARAMS( (cause >= 0) && (cause <= FD_MSG_LOG_MAX) );
2354 CHECK_PARAMS( (method >= FD_MSG_LOGTO_DEBUGONLY) && (method <= FD_MSG_LOGTO_DIR) );
2355 CHECK_PARAMS( (method == FD_MSG_LOGTO_DEBUGONLY) || (arg != NULL) );
2356
2357 /* Lock the configuration */
2358 CHECK_POSIX( pthread_mutex_lock(&ml_conf.lock) );
2359 if (!ml_conf.init) {
2360 ASSERT(0);
2361 }
2362
2363 /* Now set the parameter */
2364 ml_conf.causes[cause].meth = method;
2365 ml_conf.causes[cause].metharg = arg;
2366
2367 if (method) {
2368 TRACE_DEBUG(INFO, "Logging %s messages set to %s '%s'",
2369 (cause == FD_MSG_LOG_DROPPED) ? "DROPPED" :
2370 (cause == FD_MSG_LOG_RECEIVED) ? "RECEIVED" :
2371 (cause == FD_MSG_LOG_SENT) ? "SENT" :
2372 "???",
2373 (method == FD_MSG_LOGTO_FILE) ? "file" :
2374 (method == FD_MSG_LOGTO_DIR) ? "directory" :
2375 "???",
2376 arg);
2377 }
2378
2379 CHECK_POSIX( pthread_mutex_unlock(&ml_conf.lock) );
2380
2381 /* Done */
2382 return 0;
2383 }
2384
2385 /* Do not log anything within this one, since log lock is held */
2386 static void fd_cleanup_mutex_silent( void * mutex )
2387 {
2388 (void)pthread_mutex_unlock((pthread_mutex_t *)mutex);
2389 }
2390
2391 /* Really log the message */
2392 void fd_msg_log( enum fd_msg_log_cause cause, struct msg * msg, const char * prefix_format, ... )
2393 {
2394 va_list ap;
2395 enum fd_msg_log_method meth;
2396 const char * metharg;
2397 FILE * fstr;
2398
2399 TRACE_ENTRY("%d %p %p", cause, msg, prefix_format);
2400 CHECK_PARAMS_DO( (cause >= 0) && (cause <= FD_MSG_LOG_MAX),
2401 {
2402 TRACE_DEBUG(INFO, "Invalid cause received (%d)! Message was:", cause);
2403 fd_msg_dump_walk(INFO, msg);
2404 } );
2405
2406 /* First retrieve the config for this message */
2407 CHECK_POSIX_DO( pthread_mutex_lock(&ml_conf.lock), );
2408 if (!ml_conf.init) {
2409 ASSERT(0);
2410 }
2411 meth = ml_conf.causes[cause].meth;
2412 metharg = ml_conf.causes[cause].metharg;
2413 CHECK_POSIX_DO( pthread_mutex_unlock(&ml_conf.lock), );
2414
2415 /* Okay, now we will create the file descriptor */
2416 switch (meth) {
2417 case FD_MSG_LOGTO_DEBUGONLY:
2418 fstr = fd_g_debug_fstr;
2419 break;
2420
2421 case FD_MSG_LOGTO_FILE:
2422 TODO("Log to arg file");
2423 TODO("Log a note to debug stream");
2424 break;
2425 case FD_MSG_LOGTO_DIR:
2426 TODO("Log to arg directory in a new file");
2427 TODO("Log a note to debug stream");
2428 break;
2429 }
2430
2431 /* For file methods, let's parse the message so it looks better */
2432 if ((meth != FD_MSG_LOGTO_DEBUGONLY) && ml_conf.dict) {
2433 CHECK_FCT_DO( fd_msg_parse_dict( msg, ml_conf.dict, NULL ), );
2434 }
2435
2436 /* Then dump the prefix message to this stream, & to debug stream */
2437 (void)pthread_mutex_lock(&fd_log_lock);
2438 pthread_cleanup_push(fd_cleanup_mutex_silent, &fd_log_lock);
2439 va_start(ap, prefix_format);
2440 vfprintf( fstr, prefix_format, ap);
2441 va_end(ap);
2442 fflush(fstr);
2443 pthread_cleanup_pop(0);
2444 (void)pthread_mutex_unlock(&fd_log_lock);
2445 fd_log_debug_fstr(fstr, "\n\n");
2446
2447 /* And now the message itself */
2448 fd_msg_dump_fstr(msg, fstr);
2449
2450 /* And finally close the stream if needed */
2451 TODO("close?");
2452 }
2453
"Welcome to our mercurial repository"