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

[Xen-changelog] [xen-unstable] libxl: Use the caller's logger (xentoollog)



# HG changeset patch
# User Keir Fraser <keir.fraser@xxxxxxxxxx>
# Date 1275035682 -3600
# Node ID 7ca2d912530c27f3c8e4912de01a0c76efa721d7
# Parent  ca77b846772fb19bcf20d11d2f67fc70f444febd
libxl: Use the caller's logger (xentoollog)

We now require callers to provide a xentoollog_logger* for
libxl_ctx_init, and use that for all our own logging and also for
xc_interface_open.

Corresponding change to xl.c.

Signed-off-by: Ian Jackson <ian.jackson@xxxxxxxxxxxxx>
---
 tools/libxl/libxl.c          |   12 ++---------
 tools/libxl/libxl.h          |   10 ++-------
 tools/libxl/libxl_device.c   |    4 +--
 tools/libxl/libxl_exec.c     |    3 +-
 tools/libxl/libxl_internal.c |   44 +++++++++++++++++--------------------------
 tools/libxl/libxl_internal.h |   22 ++++++++++++++++++---
 tools/libxl/libxl_utils.h    |    8 -------
 tools/libxl/xl.c             |   18 ++---------------
 tools/libxl/xl.h             |    4 ++-
 tools/libxl/xl_cmdimpl.c     |    4 +--
 10 files changed, 56 insertions(+), 73 deletions(-)

diff -r ca77b846772f -r 7ca2d912530c tools/libxl/libxl.c
--- a/tools/libxl/libxl.c       Fri May 28 09:31:43 2010 +0100
+++ b/tools/libxl/libxl.c       Fri May 28 09:34:42 2010 +0100
@@ -36,18 +36,19 @@
 
 #define PAGE_TO_MEMKB(pages) ((pages) * 4)
 
-int libxl_ctx_init(struct libxl_ctx *ctx, int version)
+int libxl_ctx_init(struct libxl_ctx *ctx, int version, xentoollog_logger *lg)
 {
     if (version != LIBXL_VERSION)
         return ERROR_VERSION;
     memset(ctx, 0, sizeof(struct libxl_ctx));
+    ctx->lg = lg;
     ctx->alloc_maxsize = 256;
     ctx->alloc_ptrs = calloc(ctx->alloc_maxsize, sizeof(void *));
     if (!ctx->alloc_ptrs)
         return ERROR_NOMEM;
     memset(&ctx->version_info, 0, sizeof(libxl_version_info));
 
-    ctx->xch = xc_interface_open(0,0,0);
+    ctx->xch = xc_interface_open(lg,lg,0);
     if (!ctx->xch) {
         free(ctx->alloc_ptrs);
         return ERROR_FAIL;
@@ -68,13 +69,6 @@ int libxl_ctx_free(struct libxl_ctx *ctx
     free(ctx->alloc_ptrs);
     xc_interface_close(ctx->xch);
     if (ctx->xsh) xs_daemon_close(ctx->xsh); 
-    return 0;
-}
-
-int libxl_ctx_set_log(struct libxl_ctx *ctx, libxl_log_callback log_callback, 
void *log_data)
-{
-    ctx->log_callback = log_callback;
-    ctx->log_userdata = log_data;
     return 0;
 }
 
diff -r ca77b846772f -r 7ca2d912530c tools/libxl/libxl.h
--- a/tools/libxl/libxl.h       Fri May 28 09:31:43 2010 +0100
+++ b/tools/libxl/libxl.h       Fri May 28 09:34:42 2010 +0100
@@ -22,8 +22,6 @@
 #include <xs.h>
 #include <sys/wait.h> /* for pid_t */
 
-typedef void (*libxl_log_callback)(void *userdata, int loglevel, const char 
*file,
-                                   int line, const char *func, char *s);
 struct libxl_dominfo {
     uint8_t uuid[16];
     uint32_t domid;
@@ -61,11 +59,9 @@ typedef struct {
 } libxl_version_info;
 
 struct libxl_ctx {
+    xentoollog_logger *lg;
     xc_interface *xch;
     struct xs_handle *xsh;
-    /* errors/debug buf */
-    void *log_userdata;
-    libxl_log_callback log_callback;
 
     /* mini-GC */
     int alloc_maxsize;
@@ -275,9 +271,9 @@ enum {
 #define LIBXL_VERSION 0
 
 /* context functions */
-int libxl_ctx_init(struct libxl_ctx *ctx, int version);
+int libxl_ctx_init(struct libxl_ctx *ctx, int version, xentoollog_logger*);
 int libxl_ctx_free(struct libxl_ctx *ctx);
-int libxl_ctx_set_log(struct libxl_ctx *ctx, libxl_log_callback log_callback, 
void *log_data);
+int libxl_ctx_set_log(struct libxl_ctx *ctx, xentoollog_logger*);
 int libxl_ctx_postfork(struct libxl_ctx *ctx);
 
 /* domain related functions */
diff -r ca77b846772f -r 7ca2d912530c tools/libxl/libxl_device.c
--- a/tools/libxl/libxl_device.c        Fri May 28 09:31:43 2010 +0100
+++ b/tools/libxl/libxl_device.c        Fri May 28 09:34:42 2010 +0100
@@ -291,7 +291,7 @@ int libxl_devices_destroy(struct libxl_c
     flexarray_t *toremove;
     struct libxl_ctx clone;
 
-    if (libxl_ctx_init(&clone, LIBXL_VERSION)) {
+    if (libxl_ctx_init(&clone, LIBXL_VERSION, ctx->lg)) {
         return -1;
     }
 
@@ -354,7 +354,7 @@ int libxl_device_del(struct libxl_ctx *c
     int rc;
     struct libxl_ctx clone;
 
-    if (libxl_ctx_init(&clone, LIBXL_VERSION)) {
+    if (libxl_ctx_init(&clone, LIBXL_VERSION, ctx->lg)) {
         return -1;
     }
 
diff -r ca77b846772f -r 7ca2d912530c tools/libxl/libxl_exec.c
--- a/tools/libxl/libxl_exec.c  Fri May 28 09:31:43 2010 +0100
+++ b/tools/libxl/libxl_exec.c  Fri May 28 09:34:42 2010 +0100
@@ -57,7 +57,8 @@ void libxl_exec(int stdinfd, int stdoutf
     _exit(-1);
 }
 
-void libxl_report_child_exitstatus(struct libxl_ctx *ctx, int level,
+void libxl_report_child_exitstatus(struct libxl_ctx *ctx,
+                                   xentoollog_level level,
                                    const char *what, pid_t pid, int status)
 {
 
diff -r ca77b846772f -r 7ca2d912530c tools/libxl/libxl_internal.c
--- a/tools/libxl/libxl_internal.c      Fri May 28 09:31:43 2010 +0100
+++ b/tools/libxl/libxl_internal.c      Fri May 28 09:34:42 2010 +0100
@@ -150,48 +150,40 @@ char *libxl_dirname(struct libxl_ctx *ct
     return ptr;
 }
 
-void xl_logv(struct libxl_ctx *ctx, int loglevel, int errnoval,
+void xl_logv(struct libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
              const char *file, int line, const char *func,
              char *fmt, va_list ap)
 {
     char *enomem = "[out of memory formatting log message]";
-    char *s;
+    char *base = NULL;
     int rc, esave;
-
-    if (!ctx->log_callback)
-        return;
+    char fileline[256];
 
     esave = errno;
-    
-    rc = vasprintf(&s, fmt, ap);
-    if (rc<0) { s = enomem; goto x; }
 
-    if (errnoval >= 0) {
-        char *errstr, *snew;
-        errstr = strerror(errnoval);
-        if (errstr)
-            rc = asprintf(&snew, "%s: %s", s, errstr);
-        else
-            rc = asprintf(&snew, "%s: unknown error number %d", s, errnoval);
-        free(s);
-        if (rc<0) { s = enomem; goto x; }
-        s = snew;
-    }
+    rc = vasprintf(&base, fmt, ap);
+    if (rc<0) { base = enomem; goto x; }
+
+    fileline[0] = 0;
+    if (file) snprintf(fileline, sizeof(fileline), "%s:%d",file,line);
+    fileline[sizeof(fileline)-1] = 0;
 
  x:
-    ctx->log_callback(ctx->log_userdata, loglevel, file, line, func, s);
-    if (s != enomem)
-        free(s);
+    xtl_log(ctx->lg, msglevel, errnoval, "libxl",
+            "%s%s%s%s" "%s",
+            fileline, func&&file?":":"", func?func:"", func||file?" ":"",
+            base);
+    if (base != enomem) free(base);
     errno = esave;
 }
 
-void xl_log(struct libxl_ctx *ctx, int loglevel, int errnoval,
-            const char *file, int line,
-            const char *func, char *fmt, ...)
+void xl_log(struct libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
+            const char *file, int line, const char *func,
+            char *fmt, ...)
 {
     va_list ap;
     va_start(ap, fmt);
-    xl_logv(ctx, loglevel, errnoval, file, line, func, fmt, ap);
+    xl_logv(ctx, msglevel, errnoval, file, line, func, fmt, ap);
     va_end(ap);
 }
 
diff -r ca77b846772f -r 7ca2d912530c tools/libxl/libxl_internal.h
--- a/tools/libxl/libxl_internal.h      Fri May 28 09:31:43 2010 +0100
+++ b/tools/libxl/libxl_internal.h      Fri May 28 09:34:42 2010 +0100
@@ -23,6 +23,7 @@
 
 #include <xs.h>
 #include <xenctrl.h>
+#include "xentoollog.h"
 
 #include "flexarray.h"
 #include "libxl_utils.h"
@@ -50,9 +51,19 @@
   /* all of these macros preserve errno (saving and restoring) */
 
 /* logging */
-void xl_logv(struct libxl_ctx *ctx, int errnoval, int loglevel, const char 
*file, int line, const char *func, char *fmt, va_list al);
-void xl_log(struct libxl_ctx *ctx, int errnoval, int loglevel, const char 
*file, int line, const char *func, char *fmt, ...);
-  /* these functions preserve errno (saving and restoring) */
+void xl_logv(struct libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
+             const char *file /* may be 0 */, int line /* ignored if !file */,
+             const char *func /* may be 0 */,
+             char *fmt, va_list al)
+     __attribute__((format(printf,7,0)));
+
+void xl_log(struct libxl_ctx *ctx, xentoollog_level msglevel, int errnoval,
+            const char *file /* may be 0 */, int line /* ignored if !file */,
+            const char *func /* may be 0 */,
+            char *fmt, ...)
+     __attribute__((format(printf,7,8)));
+
+     /* these functions preserve errno (saving and restoring) */
 
 
 typedef enum {
@@ -215,5 +226,10 @@ const char *libxl_xen_config_dir_path(vo
 const char *libxl_xen_config_dir_path(void);
 const char *libxl_xen_script_dir_path(void);
 
+#define XL_LOG_DEBUG   XTL_DEBUG
+#define XL_LOG_INFO    XTL_INFO
+#define XL_LOG_WARNING XTL_WARN
+#define XL_LOG_ERROR   XTL_ERROR
+
 #endif
 
diff -r ca77b846772f -r 7ca2d912530c tools/libxl/libxl_utils.h
--- a/tools/libxl/libxl_utils.h Fri May 28 09:31:43 2010 +0100
+++ b/tools/libxl/libxl_utils.h Fri May 28 09:34:42 2010 +0100
@@ -50,11 +50,10 @@ int libxl_pipe(struct libxl_ctx *ctx, in
 int libxl_pipe(struct libxl_ctx *ctx, int pipes[2]);
   /* Just like fork(2), pipe(2), but log errors. */
 
-void libxl_report_child_exitstatus(struct libxl_ctx *ctx, int level,
+void libxl_report_child_exitstatus(struct libxl_ctx *ctx, xentoollog_level,
                                    const char *what, pid_t pid, int status);
     /* treats all exit statuses as errors; if that's not what you want,
      * check status yourself first */
-
 
 int libxl_mac_to_device_nic(struct libxl_ctx *ctx, uint32_t domid,
                             const char *mac, libxl_device_nic *nic);
@@ -64,11 +63,6 @@ int libxl_devid_to_device_disk(struct li
 int libxl_devid_to_device_disk(struct libxl_ctx *ctx, uint32_t domid,
                                const char *devid, libxl_device_disk *disk);
 
-/* log levels: */
-#define XL_LOG_DEBUG 3
-#define XL_LOG_INFO 2
-#define XL_LOG_WARNING 1
-#define XL_LOG_ERROR 0
 
 #endif
 
diff -r ca77b846772f -r 7ca2d912530c tools/libxl/xl.c
--- a/tools/libxl/xl.c  Fri May 28 09:31:43 2010 +0100
+++ b/tools/libxl/xl.c  Fri May 28 09:34:42 2010 +0100
@@ -31,16 +31,7 @@
 #include "libxl_utils.h"
 #include "xl.h"
 
-void log_callback(
-    void *userdata, int loglevel, const char *file,
-    int line, const char *func, char *s)
-{
-    char str[1024];
-
-    snprintf(str, sizeof(str), "[%d] %s:%d:%s: %s\n",
-             loglevel, file, line, func, s);
-    libxl_write_exactly(NULL, logfile, str, strlen(str), NULL, NULL);
-}
+xentoollog_logger *logger;
 
 int main(int argc, char **argv)
 {
@@ -51,13 +42,10 @@ int main(int argc, char **argv)
         exit(1);
     }
 
-    if (libxl_ctx_init(&ctx, LIBXL_VERSION)) {
+    logger = xtl_createlogger_stdiostream(stderr, XTL_PROGRESS,  0);
+    if (libxl_ctx_init(&ctx, LIBXL_VERSION, logger)) {
         fprintf(stderr, "cannot init xl context\n");
         exit(1);
-    }
-    if (libxl_ctx_set_log(&ctx, log_callback, NULL)) {
-        fprintf(stderr, "cannot set xl log callback\n");
-        exit(-ERROR_FAIL);
     }
 
     srand(time(0));
diff -r ca77b846772f -r 7ca2d912530c tools/libxl/xl.h
--- a/tools/libxl/xl.h  Fri May 28 09:31:43 2010 +0100
+++ b/tools/libxl/xl.h  Fri May 28 09:34:42 2010 +0100
@@ -14,6 +14,8 @@
 
 #ifndef XL_H
 #define XL_H
+
+#include "xentoollog.h"
 
 struct cmd_spec {
     char *cmd_name;
@@ -75,6 +77,6 @@ extern int cmdtable_len;
 extern int cmdtable_len;
 
 extern struct libxl_ctx ctx;
-extern int logfile;
+extern xentoollog_logger *logger;
 
 #endif /* XL_H */
diff -r ca77b846772f -r 7ca2d912530c tools/libxl/xl_cmdimpl.c
--- a/tools/libxl/xl_cmdimpl.c  Fri May 28 09:31:43 2010 +0100
+++ b/tools/libxl/xl_cmdimpl.c  Fri May 28 09:34:42 2010 +0100
@@ -1088,7 +1088,7 @@ start:
                 }
             }
             if (status) {
-                libxl_report_child_exitstatus(&ctx, XL_LOG_ERROR,
+                libxl_report_child_exitstatus(&ctx, XTL_ERROR,
                            "daemonizing child", child1, status);
                 ret = ERROR_FAIL;
                 goto error_out;
@@ -1810,7 +1810,7 @@ static void migration_child_report(pid_t
 
         if (child == migration_child) {
             if (status)
-                libxl_report_child_exitstatus(&ctx, XL_LOG_INFO,
+                libxl_report_child_exitstatus(&ctx, XTL_INFO,
                                               "migration target process",
                                               migration_child, status);
             break;

_______________________________________________
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®.