[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Xen-changelog] use format printf style to write to tracefd instead of using write syscall.



# HG changeset patch
# User vhanquez@xxxxxxxxxxxxxxxxxxxxxxx
# Node ID c70109e64c9e6d7f73c82e45047cecde783e76a5
# Parent  45f3b9cfbd83c68be431b7be4ed57376d1db7d51
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@xxxxxxxxxxxxx>

diff -r 45f3b9cfbd83 -r c70109e64c9e tools/xenstore/xenstored_core.c
--- a/tools/xenstore/xenstored_core.c   Tue Jan 24 10:55:45 2006
+++ b/tools/xenstore/xenstored_core.c   Tue Jan 24 11:50:38 2006
@@ -174,69 +174,27 @@
        }
 }
 
-static void trace_io(const struct connection *conn,
-                    const char *prefix,
-                    const struct buffered_data *data)
-{
-       char string[64];
-       unsigned int i;
-       time_t now;
-       struct tm *tm;
+void trace(const char *fmt, ...)
+{
+       va_list arglist;
+       char *str;
+       char sbuf[1024];
+       int ret;
 
        if (tracefd < 0)
                return;
 
-       now = time(NULL);
-       tm = localtime(&now);
-
-       write(tracefd, prefix, strlen(prefix));
-       sprintf(string, " %p %02d:%02d:%02d ", conn, tm->tm_hour, tm->tm_min,
-               tm->tm_sec);
-       write(tracefd, string, strlen(string));
-       write(tracefd, sockmsg_string(data->hdr.msg.type),
-             strlen(sockmsg_string(data->hdr.msg.type)));
-       write(tracefd, " (", 2);
-       for (i = 0; i < data->hdr.msg.len; i++) {
-               if (data->buffer[i] == '\0')
-                       write(tracefd, " ", 1);
-               else
-                       write(tracefd, data->buffer + i, 1);
-       }
-       write(tracefd, ")\n", 2);
-}
-
-void trace_create(const void *data, const char *type)
-{
-       char string[64];
-       if (tracefd < 0)
+       /* try to use a static buffer */
+       va_start(arglist, fmt);
+       ret = vsnprintf(sbuf, 1024, fmt, arglist);
+       va_end(arglist);
+
+       if (ret <= 1024) {
+               write(tracefd, sbuf, ret);
                return;
-
-       write(tracefd, "CREATE ", strlen("CREATE "));
-       write(tracefd, type, strlen(type));
-       sprintf(string, " %p\n", data);
-       write(tracefd, string, strlen(string));
-}
-
-void trace_destroy(const void *data, const char *type)
-{
-       char string[64];
-       if (tracefd < 0)
-               return;
-
-       write(tracefd, "DESTROY ", strlen("DESTROY "));
-       write(tracefd, type, strlen(type));
-       sprintf(string, " %p\n", data);
-       write(tracefd, string, strlen(string));
-}
-
-void trace(const char *fmt, ...)
-{
-       va_list arglist;
-       char *str;
-
-       if (tracefd < 0)
-               return;
-
+       }
+
+       /* fail back to dynamic allocation */
        va_start(arglist, fmt);
        str = talloc_vasprintf(NULL, fmt, arglist);
        va_end(arglist);
@@ -244,6 +202,38 @@
        talloc_free(str);
 }
 
+static void trace_io(const struct connection *conn,
+                    const char *prefix,
+                    const struct buffered_data *data)
+{
+       unsigned int i;
+       time_t now;
+       struct tm *tm;
+
+       if (tracefd < 0)
+               return;
+
+       now = time(NULL);
+       tm = localtime(&now);
+
+       trace("%s %p %02d:%02d:%02d %s (", prefix, conn,
+             tm->tm_hour, tm->tm_min, tm->tm_sec,
+             sockmsg_string(data->hdr.msg.type));
+       
+       for (i = 0; i < data->hdr.msg.len; i++)
+               trace("%c", (data->buffer[i] != '\0') ? data->buffer[i] : ' ');
+       trace(")\n");
+}
+
+void trace_create(const void *data, const char *type)
+{
+       trace("CREATE %s %p\n", type, data);
+}
+
+void trace_destroy(const void *data, const char *type)
+{
+       trace("DESTROY %s %p\n", type, data);
+}
 
 /**
  * Signal handler for SIGHUP, which requests that the trace log is reopened
@@ -268,7 +258,7 @@
                if (tracefd < 0)
                        perror("Could not open tracefile");
                else
-                       write(tracefd, "\n***\n", strlen("\n***\n"));
+                       trace("\n***\n");
        }
 }
 

_______________________________________________
Xen-changelog mailing list
Xen-changelog@xxxxxxxxxxxxxxxxxxx
http://lists.xensource.com/xen-changelog


 


Rackspace

Lists.xenproject.org is hosted with RackSpace, monitoring our
servers 24x7x365 and backed by RackSpace's Fanatical Support®.