diff --git a/tools/libxl/libxl.h b/tools/libxl/libxl.h index 1e50911..2d82829 100644 --- a/tools/libxl/libxl.h +++ b/tools/libxl/libxl.h @@ -23,8 +23,6 @@ #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 { @@ -230,25 +228,6 @@ 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 --git a/tools/libxl/libxl_internal.h b/tools/libxl/libxl_internal.h index 23d6ee4..39eb2dc 100644 --- a/tools/libxl/libxl_internal.h +++ b/tools/libxl/libxl_internal.h @@ -36,6 +36,28 @@ #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 + +/* 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, ...); + + typedef enum { DEVICE_VIF, DEVICE_VBD, diff --git a/tools/libxl/xl.c b/tools/libxl/xl.c index a8a21e3..6577457 100644 --- a/tools/libxl/xl.c +++ b/tools/libxl/xl.c @@ -46,6 +46,21 @@ void log_callback(void *userdata, int loglevel, const char *file, int line, cons write(logfile, str, strlen(str)); } +#define LOG(_f, _a...) dolog(__FILE__, __LINE__, __func__, _f, ##_a) + +void dolog(const char *file, int line, const char *func, char *fmt, ...) +{ + va_list ap; + char *s; + int rc; + + va_start(ap, fmt); + rc = vasprintf(&s, fmt, ap); + va_end(ap); + if (rc >= 0) + write(logfile, s, rc); +} + static void init_create_info(libxl_domain_create_info *c_info) { memset(c_info, '\0', sizeof(*c_info)); @@ -784,7 +799,7 @@ start: daemon(0, 0); need_daemon = 0; } - XL_LOG(&ctx, XL_LOG_DEBUG, "Waiting for domain %s (domid %d) to die", info1.name, domid); + LOG("Waiting for domain %s (domid %d) to die", info1.name, domid); w1 = (libxl_waiter*) malloc(sizeof(libxl_waiter) * num_disks); w2 = (libxl_waiter*) malloc(sizeof(libxl_waiter)); libxl_wait_for_disk_ejects(&ctx, domid, disks, num_disks, w1); @@ -808,9 +823,9 @@ start: switch (event.type) { case DOMAIN_DEATH: if (libxl_event_get_domain_death_info(&ctx, domid, &event, &info)) { - XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d is dead", domid); + LOG("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); + LOG("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_free_waiter(w1); @@ -818,12 +833,12 @@ start: free(w1); free(w2); libxl_ctx_free(&ctx); - XL_LOG(&ctx, XL_LOG_DEBUG, "Done. Rebooting now"); + LOG("Done. Rebooting now"); goto start; } - XL_LOG(&ctx, XL_LOG_DEBUG, "Done. Exiting now"); + LOG("Done. Exiting now"); } - XL_LOG(&ctx, XL_LOG_DEBUG, "Domain %d does not need to be clean, exiting now", domid); + LOG("Domain %d does not need to be clean, exiting now", domid); exit(0); } break;