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

[Xen-devel] [PATCH 10/14] 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 |   46 +++++++++++++++++------------------------
 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, 57 insertions(+), 74 deletions(-)

diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 6adb9e5..574229a 100644
--- a/tools/libxl/libxl.c
+++ b/tools/libxl/libxl.c
@@ -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;
@@ -71,13 +72,6 @@ int libxl_ctx_free(struct libxl_ctx *ctx)
     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;
-}
-
 
/******************************************************************************/
 
 int libxl_domain_make(struct libxl_ctx *ctx, libxl_domain_create_info *info,
diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h
index 1c4750e..1532d9c 100644
--- a/tools/libxl/libxl.h
+++ b/tools/libxl/libxl.h
@@ -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 --git a/tools/libxl/libxl_device.c b/tools/libxl/libxl_device.c
index ca20080..8a5959f 100644
--- a/tools/libxl/libxl_device.c
+++ b/tools/libxl/libxl_device.c
@@ -291,7 +291,7 @@ int libxl_devices_destroy(struct libxl_ctx *ctx, uint32_t 
domid, int force)
     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 *ctx, libxl_device 
*dev, int wait)
     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 --git a/tools/libxl/libxl_exec.c b/tools/libxl/libxl_exec.c
index 52dda97..83c14d2 100644
--- a/tools/libxl/libxl_exec.c
+++ b/tools/libxl/libxl_exec.c
@@ -57,7 +57,8 @@ void libxl_exec(int stdinfd, int stdoutfd, int stderrfd, char 
*arg0, char **args
     _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 --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c
index 421e1e7..fd23023 100644
--- a/tools/libxl/libxl_internal.c
+++ b/tools/libxl/libxl_internal.c
@@ -150,48 +150,40 @@ char *libxl_dirname(struct libxl_ctx *ctx, const char *s)
     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 --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h
index 2f1f591..fd5b7e7 100644
--- a/tools/libxl/libxl_internal.h
+++ b/tools/libxl/libxl_internal.h
@@ -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_xenfirmwaredir_path(void);
 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 --git a/tools/libxl/libxl_utils.h b/tools/libxl/libxl_utils.h
index bd3debf..4a4ba5e 100644
--- a/tools/libxl/libxl_utils.h
+++ b/tools/libxl/libxl_utils.h
@@ -50,12 +50,11 @@ pid_t libxl_fork(struct libxl_ctx *ctx);
 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);
 int libxl_devid_to_device_nic(struct libxl_ctx *ctx, uint32_t domid,
@@ -64,11 +63,6 @@ int libxl_devid_to_device_nic(struct libxl_ctx *ctx, 
uint32_t domid,
 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 --git a/tools/libxl/xl.c b/tools/libxl/xl.c
index 77eaa7b..8b10674 100644
--- a/tools/libxl/xl.c
+++ b/tools/libxl/xl.c
@@ -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,14 +42,11 @@ 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 --git a/tools/libxl/xl.h b/tools/libxl/xl.h
index 450a12f..8b3644c 100644
--- a/tools/libxl/xl.h
+++ b/tools/libxl/xl.h
@@ -15,6 +15,8 @@
 #ifndef XL_H
 #define XL_H
 
+#include "xentoollog.h"
+
 struct cmd_spec {
     char *cmd_name;
     int (*cmd_impl)(int argc, char **argv);
@@ -75,6 +77,6 @@ extern struct cmd_spec cmd_table[];
 extern int cmdtable_len;
 
 extern struct libxl_ctx ctx;
-extern int logfile;
+extern xentoollog_logger *logger;
 
 #endif /* XL_H */
diff --git a/tools/libxl/xl_cmdimpl.c b/tools/libxl/xl_cmdimpl.c
index c79f42d..abe4c9d 100644
--- a/tools/libxl/xl_cmdimpl.c
+++ b/tools/libxl/xl_cmdimpl.c
@@ -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;
@@ -1804,7 +1804,7 @@ static void migration_child_report(pid_t migration_child, 
int recv_fd) {
 
         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;
-- 
1.5.6.5


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


 


Rackspace

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