ia64/xen-unstable

changeset 8641:c70109e64c9e

use format printf style to write to tracefd instead of using write syscall.
add a static buffer to trace() instead of always allocating from the heap.

Signed-off-by: Vincent Hanquez <vincent@xensource.com>
author vhanquez@kneesa.uk.xensource.com
date Tue Jan 24 11:50:38 2006 +0000 (2006-01-24)
parents 45f3b9cfbd83
children 6afcef051257
files tools/xenstore/xenstored_core.c
line diff
     1.1 --- a/tools/xenstore/xenstored_core.c	Tue Jan 24 10:55:45 2006 +0000
     1.2 +++ b/tools/xenstore/xenstored_core.c	Tue Jan 24 11:50:38 2006 +0000
     1.3 @@ -174,11 +174,38 @@ static char *sockmsg_string(enum xsd_soc
     1.4  	}
     1.5  }
     1.6  
     1.7 +void trace(const char *fmt, ...)
     1.8 +{
     1.9 +	va_list arglist;
    1.10 +	char *str;
    1.11 +	char sbuf[1024];
    1.12 +	int ret;
    1.13 +
    1.14 +	if (tracefd < 0)
    1.15 +		return;
    1.16 +
    1.17 +	/* try to use a static buffer */
    1.18 +	va_start(arglist, fmt);
    1.19 +	ret = vsnprintf(sbuf, 1024, fmt, arglist);
    1.20 +	va_end(arglist);
    1.21 +
    1.22 +	if (ret <= 1024) {
    1.23 +		write(tracefd, sbuf, ret);
    1.24 +		return;
    1.25 +	}
    1.26 +
    1.27 +	/* fail back to dynamic allocation */
    1.28 +	va_start(arglist, fmt);
    1.29 +	str = talloc_vasprintf(NULL, fmt, arglist);
    1.30 +	va_end(arglist);
    1.31 +	write(tracefd, str, strlen(str));
    1.32 +	talloc_free(str);
    1.33 +}
    1.34 +
    1.35  static void trace_io(const struct connection *conn,
    1.36  		     const char *prefix,
    1.37  		     const struct buffered_data *data)
    1.38  {
    1.39 -	char string[64];
    1.40  	unsigned int i;
    1.41  	time_t now;
    1.42  	struct tm *tm;
    1.43 @@ -189,62 +216,25 @@ static void trace_io(const struct connec
    1.44  	now = time(NULL);
    1.45  	tm = localtime(&now);
    1.46  
    1.47 -	write(tracefd, prefix, strlen(prefix));
    1.48 -	sprintf(string, " %p %02d:%02d:%02d ", conn, tm->tm_hour, tm->tm_min,
    1.49 -		tm->tm_sec);
    1.50 -	write(tracefd, string, strlen(string));
    1.51 -	write(tracefd, sockmsg_string(data->hdr.msg.type),
    1.52 -	      strlen(sockmsg_string(data->hdr.msg.type)));
    1.53 -	write(tracefd, " (", 2);
    1.54 -	for (i = 0; i < data->hdr.msg.len; i++) {
    1.55 -		if (data->buffer[i] == '\0')
    1.56 -			write(tracefd, " ", 1);
    1.57 -		else
    1.58 -			write(tracefd, data->buffer + i, 1);
    1.59 -	}
    1.60 -	write(tracefd, ")\n", 2);
    1.61 +	trace("%s %p %02d:%02d:%02d %s (", prefix, conn,
    1.62 +	      tm->tm_hour, tm->tm_min, tm->tm_sec,
    1.63 +	      sockmsg_string(data->hdr.msg.type));
    1.64 +	
    1.65 +	for (i = 0; i < data->hdr.msg.len; i++)
    1.66 +		trace("%c", (data->buffer[i] != '\0') ? data->buffer[i] : ' ');
    1.67 +	trace(")\n");
    1.68  }
    1.69  
    1.70  void trace_create(const void *data, const char *type)
    1.71  {
    1.72 -	char string[64];
    1.73 -	if (tracefd < 0)
    1.74 -		return;
    1.75 -
    1.76 -	write(tracefd, "CREATE ", strlen("CREATE "));
    1.77 -	write(tracefd, type, strlen(type));
    1.78 -	sprintf(string, " %p\n", data);
    1.79 -	write(tracefd, string, strlen(string));
    1.80 +	trace("CREATE %s %p\n", type, data);
    1.81  }
    1.82  
    1.83  void trace_destroy(const void *data, const char *type)
    1.84  {
    1.85 -	char string[64];
    1.86 -	if (tracefd < 0)
    1.87 -		return;
    1.88 -
    1.89 -	write(tracefd, "DESTROY ", strlen("DESTROY "));
    1.90 -	write(tracefd, type, strlen(type));
    1.91 -	sprintf(string, " %p\n", data);
    1.92 -	write(tracefd, string, strlen(string));
    1.93 +	trace("DESTROY %s %p\n", type, data);
    1.94  }
    1.95  
    1.96 -void trace(const char *fmt, ...)
    1.97 -{
    1.98 -	va_list arglist;
    1.99 -	char *str;
   1.100 -
   1.101 -	if (tracefd < 0)
   1.102 -		return;
   1.103 -
   1.104 -	va_start(arglist, fmt);
   1.105 -	str = talloc_vasprintf(NULL, fmt, arglist);
   1.106 -	va_end(arglist);
   1.107 -	write(tracefd, str, strlen(str));
   1.108 -	talloc_free(str);
   1.109 -}
   1.110 -
   1.111 -
   1.112  /**
   1.113   * Signal handler for SIGHUP, which requests that the trace log is reopened
   1.114   * (in the main loop).  A single byte is written to reopen_log_pipe, to awaken
   1.115 @@ -268,7 +258,7 @@ static void reopen_log()
   1.116  		if (tracefd < 0)
   1.117  			perror("Could not open tracefile");
   1.118  		else
   1.119 -			write(tracefd, "\n***\n", strlen("\n***\n"));
   1.120 +			trace("\n***\n");
   1.121  	}
   1.122  }
   1.123