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

[Xen-changelog] [xen-unstable] libxenlight: move logging macros to the public header



# HG changeset patch
# User Keir Fraser <keir.fraser@xxxxxxxxxx>
# Date 1259158790 0
# Node ID c0e32941ee69cee97155f58b786c1dc265e54e55
# Parent  0e64f0b7d049264ea9eaeeabf12d611e424376ef
libxenlight: move logging macros to the public header

This patch moves the logging macros to the public header so that they
can be reused by the client of the library.  It also refactors the
code to create the qemu logfile into a generic function that can be
reused to create generic xen logfiles under /var/log/xen.  Finally xl
is changed to log to file when running in background.

Signed-off-by: Stefano Stabellini <stefano.stabellini@xxxxxxxxxxxxx>
---
 tools/libxl/libxl.c          |   23 ++++-------------------
 tools/libxl/libxl.h          |   22 +++++++++++++++++++++-
 tools/libxl/libxl_internal.h |   21 ---------------------
 tools/libxl/libxl_utils.c    |   27 +++++++++++++++++++++++++++
 tools/libxl/libxl_utils.h    |    1 +
 tools/libxl/xl.c             |   22 +++++++++++++++++++++-
 6 files changed, 74 insertions(+), 42 deletions(-)

diff -r 0e64f0b7d049 -r c0e32941ee69 tools/libxl/libxl.c
--- a/tools/libxl/libxl.c       Wed Nov 25 14:19:20 2009 +0000
+++ b/tools/libxl/libxl.c       Wed Nov 25 14:19:50 2009 +0000
@@ -19,7 +19,6 @@
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <sys/stat.h>
 #include <sys/types.h>
 #include <fcntl.h>
 #include <sys/select.h>
@@ -765,10 +764,9 @@ int libxl_create_device_model(struct lib
                               libxl_device_nic *vifs, int num_vifs,
                               libxl_device_model_starting **starting_r)
 {
-    char *path, *logfile, *logfile_new;
-    struct stat stat_buf;
+    char *path, *logfile;
     int logfile_w, null;
-    int i, rc;
+    int rc;
     char **args;
     struct libxl_spawn_starting buf_spawn, *for_spawn;
 
@@ -789,22 +787,9 @@ int libxl_create_device_model(struct lib
     path = libxl_sprintf(ctx, "/local/domain/0/device-model/%d", info->domid);
     xs_mkdir(ctx->xsh, XBT_NULL, path);
 
-    logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", 
info->dom_name);
-    if (stat(logfile, &stat_buf) == 0) {
-        /* file exists, rotate */
-        logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.10", 
info->dom_name);
-        unlink(logfile);
-        for (i = 9; i > 0; i--) {
-            logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", 
info->dom_name, i);
-            logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.%d", 
info->dom_name, i + 1);
-            rename(logfile, logfile_new);
-        }
-        logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", 
info->dom_name);
-        logfile_new = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log.1", 
info->dom_name);
-        rename(logfile, logfile_new);
-    }
-    logfile = libxl_sprintf(ctx, "/var/log/xen/qemu-dm-%s.log", 
info->dom_name);
+    libxl_create_logfile(ctx, libxl_sprintf(ctx, "qemu-dm-%s", 
info->dom_name), &logfile);
     logfile_w = open(logfile, O_WRONLY|O_CREAT, 0644);
+    free(logfile);
     null = open("/dev/null", O_RDONLY);
 
     if (starting_r) {
diff -r 0e64f0b7d049 -r c0e32941ee69 tools/libxl/libxl.h
--- a/tools/libxl/libxl.h       Wed Nov 25 14:19:20 2009 +0000
+++ b/tools/libxl/libxl.h       Wed Nov 25 14:19:50 2009 +0000
@@ -23,9 +23,10 @@
 
 #include "xen_uuid.h"
 
+#define XL_LOGGING_ENABLED
+
 typedef void (*libxl_log_callback)(void *userdata, int loglevel, const char 
*file,
                                    int line, const char *func, char *s);
-
 struct libxl_dominfo {
     xen_uuid_t uuid[16];
     uint32_t domid;
@@ -224,6 +225,25 @@ typedef struct  {
 #define ERROR_NOMEM (-1032)
 #define ERROR_INVAL (-1245)
 
+/* 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, ...);
+
+#ifdef XL_LOGGING_ENABLED
+#define XL_LOG(ctx, loglevel, _f, _a...)   xl_log(ctx, loglevel, -1, __FILE__, 
__LINE__, __func__, _f, ##_a)
+#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)   xl_log(ctx, loglevel, errno, 
__FILE__, __LINE__, __func__, _f, ##_a)
+#define XL_LOG_ERRNOVAL(ctx, errnoval, loglevel, _f, _a...)   xl_log(ctx, 
loglevel, errnoval, __FILE__, __LINE__, __func__, _f, ##_a)
+#else
+#define XL_LOG(ctx, loglevel, _f, _a...)
+#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)
+#define XL_LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)
+#endif
+
+#define XL_LOG_DEBUG 3
+#define XL_LOG_INFO 2
+#define XL_LOG_WARNING 1
+#define XL_LOG_ERROR 0
+
 /* context functions */
 int libxl_ctx_init(struct libxl_ctx *ctx);
 int libxl_ctx_free(struct libxl_ctx *ctx);
diff -r 0e64f0b7d049 -r c0e32941ee69 tools/libxl/libxl_internal.h
--- a/tools/libxl/libxl_internal.h      Wed Nov 25 14:19:20 2009 +0000
+++ b/tools/libxl/libxl_internal.h      Wed Nov 25 14:19:50 2009 +0000
@@ -32,27 +32,6 @@
 #define LIBXL_XENCONSOLE_PROTOCOL "vt100"
 
 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
-
-
-#define XL_LOGGING_ENABLED
-
-#ifdef XL_LOGGING_ENABLED
-#define XL_LOG(ctx, loglevel, _f, _a...)   xl_log(ctx, loglevel, -1, __FILE__, 
__LINE__, __func__, _f, ##_a)
-#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)   xl_log(ctx, loglevel, errno, 
__FILE__, __LINE__, __func__, _f, ##_a)
-#define XL_LOG_ERRNOVAL(ctx, errnoval, loglevel, _f, _a...)   xl_log(ctx, 
loglevel, errnoval, __FILE__, __LINE__, __func__, _f, ##_a)
-#else
-#define XL_LOG(ctx, loglevel, _f, _a...)
-#define XL_LOG_ERRNO(ctx, loglevel, _f, _a...)
-#define XL_LOG_ERRNOVAL(ctx, loglevel, errnoval, _f, _a...)
-#endif
-
-#define XL_LOG_DEBUG 3
-#define XL_LOG_INFO 2
-#define XL_LOG_WARNING 1
-#define XL_LOG_ERROR 0
-
-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, ...);
 
 typedef enum {
     DEVICE_VIF,
diff -r 0e64f0b7d049 -r c0e32941ee69 tools/libxl/libxl_utils.c
--- a/tools/libxl/libxl_utils.c Wed Nov 25 14:19:20 2009 +0000
+++ b/tools/libxl/libxl_utils.c Wed Nov 25 14:19:50 2009 +0000
@@ -23,6 +23,9 @@
 #include <xenctrl.h>
 #include <ctype.h>
 #include <errno.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <unistd.h>
 
 #include "libxl_utils.h"
 #include "libxl_internal.h"
@@ -187,3 +190,27 @@ int libxl_is_stubdom(struct libxl_ctx *c
         return 0;
 }
 
+int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name)
+{
+    struct stat stat_buf;
+    char *logfile, *logfile_new;
+    int i;
+
+    logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name);
+    if (stat(logfile, &stat_buf) == 0) {
+        /* file exists, rotate */
+        logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.10", name);
+        unlink(logfile);
+        for (i = 9; i > 0; i--) {
+            logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i);
+            logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.%d", name, i 
+ 1);
+            rename(logfile, logfile_new);
+        }
+        logfile = libxl_sprintf(ctx, "/var/log/xen/%s.log", name);
+        logfile_new = libxl_sprintf(ctx, "/var/log/xen/%s.log.1", name);
+        rename(logfile, logfile_new);
+    }
+    *full_name = strdup(logfile);
+    return 0;
+}
+
diff -r 0e64f0b7d049 -r c0e32941ee69 tools/libxl/libxl_utils.h
--- a/tools/libxl/libxl_utils.h Wed Nov 25 14:19:20 2009 +0000
+++ b/tools/libxl/libxl_utils.h Wed Nov 25 14:19:50 2009 +0000
@@ -31,6 +31,7 @@ int libxl_param_to_domid(struct libxl_ct
 int libxl_param_to_domid(struct libxl_ctx *ctx, char *p, uint32_t *domid);
 int libxl_get_stubdom_id(struct libxl_ctx *ctx, int guest_domid);
 int libxl_is_stubdom(struct libxl_ctx *ctx, int domid);
+int libxl_create_logfile(struct libxl_ctx *ctx, char *name, char **full_name);
 
 #endif
 
diff -r 0e64f0b7d049 -r c0e32941ee69 tools/libxl/xl.c
--- a/tools/libxl/xl.c  Wed Nov 25 14:19:20 2009 +0000
+++ b/tools/libxl/xl.c  Wed Nov 25 14:19:50 2009 +0000
@@ -35,9 +35,14 @@
 #include "libxl.h"
 #include "libxl_utils.h"
 
+int logfile = 2;
+
 void log_callback(void *userdata, int loglevel, const char *file, int line, 
const char *func, char *s)
 {
-    fprintf(stderr, "[%d] %s:%d:%s: %s\n", loglevel, file, line, func, s);
+    char str[1024];
+
+    snprintf(str, sizeof(str), "[%d] %s:%d:%s: %s\n", loglevel, file, line, 
func, s);
+    write(logfile, str, strlen(str));
 }
 
 static void printf_info(libxl_domain_create_info *c_info,
@@ -640,9 +645,18 @@ start:
     libxl_domain_unpause(&ctx, domid);
 
     if (need_daemon) {
+        char *fullname, *name;
+
+        asprintf(&name, "xl-%s", info1.name);
+        libxl_create_logfile(&ctx, name, &fullname);
+        logfile = open(fullname, O_WRONLY|O_CREAT, 0644);
+        free(fullname);
+        free(name);
+
         daemon(0, 0);
         need_daemon = 0;
     }
+    XL_LOG(&ctx, XL_LOG_DEBUG, "Waiting for domain %s (domid %d) to die", 
info1.name, domid);
     
     libxl_wait_for_domain_death(&ctx, domid, &fd);
     while (1) {
@@ -658,17 +672,23 @@ start:
         if (!ret)
             continue;
         if (libxl_is_domain_dead(&ctx, domid, &info)) {
+            XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d is dead", domid);
             if (info.crashed || info.dying || (info.shutdown && 
(info.shutdown_reason != SHUTDOWN_suspend))) {
+                XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d needs to be clean: 
destroying the domain", domid);
                 libxl_domain_destroy(&ctx, domid, 0);
                 if (info.shutdown && (info.shutdown_reason == 
SHUTDOWN_reboot)) {
                     libxl_ctx_free(&ctx);
+                    XL_LOG(&ctx, XL_LOG_DEBUG, "Done. Rebooting now");
                     goto start;
                 }
+                XL_LOG(&ctx, XL_LOG_DEBUG, "Done. Exiting now");
             }
+            XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d does not need to be clean, 
exiting now", domid);
             exit(0);
         }
     }
 
+    close(logfile);
     for (i = 0; i < num_vifs; i++) {
         free(vifs[i].smac);
         free(vifs[i].ifname);

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