Stefano Stabellini
2009-Nov-25 14:12 UTC
[Xen-devel] [PATCH] libxenlight: move logging macros to the public header
Hi all, 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@eu.citrix.com> --- diff -r c2849e35c142 tools/libxl/libxl.c --- a/tools/libxl/libxl.c Mon Nov 23 17:53:58 2009 +0000 +++ b/tools/libxl/libxl.c Mon Nov 23 18:34:32 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 @@ 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 @@ 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 c2849e35c142 tools/libxl/libxl.h --- a/tools/libxl/libxl.h Mon Nov 23 17:53:58 2009 +0000 +++ b/tools/libxl/libxl.h Mon Nov 23 18:34:32 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 @@ #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 c2849e35c142 tools/libxl/libxl_internal.h --- a/tools/libxl/libxl_internal.h Mon Nov 23 17:53:58 2009 +0000 +++ b/tools/libxl/libxl_internal.h Mon Nov 23 18:34:32 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 c2849e35c142 tools/libxl/libxl_utils.c --- a/tools/libxl/libxl_utils.c Mon Nov 23 17:53:58 2009 +0000 +++ b/tools/libxl/libxl_utils.c Mon Nov 23 18:34:32 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 @@ 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 c2849e35c142 tools/libxl/libxl_utils.h --- a/tools/libxl/libxl_utils.h Mon Nov 23 17:53:58 2009 +0000 +++ b/tools/libxl/libxl_utils.h Mon Nov 23 18:34:32 2009 +0000 @@ -31,6 +31,7 @@ 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 c2849e35c142 tools/libxl/xl.c --- a/tools/libxl/xl.c Mon Nov 23 17:53:58 2009 +0000 +++ b/tools/libxl/xl.c Mon Nov 23 18:34:32 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 @@ 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 @@ 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-devel mailing list Xen-devel@lists.xensource.com http://lists.xensource.com/xen-devel