thruputlog.c (1917B)
1 /************************************************************************* 2 * This file is part of Crosroads 1.23, a load balancer and fail over 3 * utility for TCP. Copyright (c) Karel Kubat, distributed under GPL. 4 * Visit http://crossroads.e-tunity.com for information. 5 *************************************************************************/ 6 #include "crossroads.h" 7 8 void thruputlog (unsigned char const *buf, int len, CopyDirection dir) { 9 struct timeval tv; 10 static double d_start = 0; 11 double d_now; 12 int i; 13 FILE *f; 14 15 if (current_backend < 0 || 16 ! activeservice->backend[current_backend].thruputfile) 17 return; 18 19 /* Initialize timer if necessary. Get current time. */ 20 if (! d_start) { 21 if (gettimeofday (&tv, 0)) 22 error ("Failed to get the time of day: %s\n", 23 strerror(errno)); 24 d_start = tv.tv_sec * 1000000 + tv.tv_usec; 25 } 26 if (gettimeofday (&tv, 0)) 27 error ("Failed to get the time of day: %s\n", strerror(errno)); 28 d_now = tv.tv_sec * 1000000 + tv.tv_usec; 29 30 /* Get a handle on the reporting log. */ 31 if ( (! (f = fopen (activeservice->backend[current_backend].thruputfile, 32 "a"))) && 33 (! (f = fopen (activeservice->backend[current_backend].thruputfile, 34 "w"))) ) { 35 warning ("Cannot write %s: %s", 36 activeservice->backend[current_backend].thruputfile, 37 strerror(errno)); 38 return; 39 } 40 41 #if defined HAVE_FLOCK 42 flock (fileno(f), LOCK_EX); 43 #elif defined HAVE_LOCKF 44 lockf (fileno(f), F_LOCK, 0); 45 #endif 46 47 /* Report the activity. */ 48 fprintf (f, "%7.7d %15f %c ", 49 getpid(), 50 (d_now - d_start) / 1000000, 51 dir == dir_client_to_server ? 'C' : 'B'); 52 for (i = 0; i < 100 && i < len; i++) 53 fputc (isprint (buf[i]) ? buf[i] : '.', f); 54 fputc ('\n', f); 55 56 #if defined HAVE_FLOCK 57 flock (fileno(f), LOCK_UN); 58 #elif defined HAVE_LOCKF 59 lockf (fileno(f), F_ULOCK, 0); 60 #endif 61 62 fclose (f); 63 }