aboutsummaryrefslogtreecommitdiffstats
path: root/tools
diff options
context:
space:
mode:
authorKeir Fraser <keir.fraser@citrix.com>2010-05-28 09:34:42 +0100
committerKeir Fraser <keir.fraser@citrix.com>2010-05-28 09:34:42 +0100
commit1fe4b0e07c2ed1d521dfabc6f3f564932fcf3224 (patch)
tree921083546a4c686cc7d475b9c1e53d1052b0660d /tools
parent4046aa9cbed80adad144103ab29e0ac9d171dbf4 (diff)
downloadxen-1fe4b0e07c2ed1d521dfabc6f3f564932fcf3224.tar.gz
xen-1fe4b0e07c2ed1d521dfabc6f3f564932fcf3224.tar.bz2
xen-1fe4b0e07c2ed1d521dfabc6f3f564932fcf3224.zip
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@eu.citrix.com>
Diffstat (limited to 'tools')
-rw-r--r--tools/libxl/libxl.c12
-rw-r--r--tools/libxl/libxl.h10
-rw-r--r--tools/libxl/libxl_device.c4
-rw-r--r--tools/libxl/libxl_exec.c3
-rw-r--r--tools/libxl/libxl_internal.c46
-rw-r--r--tools/libxl/libxl_internal.h22
-rw-r--r--tools/libxl/libxl_utils.h8
-rw-r--r--tools/libxl/xl.c18
-rw-r--r--tools/libxl/xl.h4
-rw-r--r--tools/libxl/xl_cmdimpl.c4
10 files changed, 57 insertions, 74 deletions
diff --git a/tools/libxl/libxl.c b/tools/libxl/libxl.c
index 375306f196..2e5bfc7126 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 1c4750e1e0..1532d9c8af 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 ca2008070d..8a5959fbd8 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 52dda97f5b..83c14d2720 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 421e1e7490..fd23023a74 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 2f1f591f25..fd5b7e7698 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 bd3debf0b0..4a4ba5e8f7 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 77eaa7b087..8b106741de 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 450a12ff2b..8b3644cb01 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 c7f6f8fe2d..50e986a371 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;
@@ -1810,7 +1810,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;