From c14ce8a1257fbbc43c4196b186a7677209c6b12e Mon Sep 17 00:00:00 2001 From: Keir Fraser Date: Mon, 23 Nov 2009 06:59:06 +0000 Subject: libxenlight: Clean up logging arrangements * Introduce new variants of the logging functions which include errno values (converted using strerror) in the messages passed to the application's logging callback. * Use the new errno-including logging functions everywhere where appropriate. In general, xc_... functions return errno values or 0; xs_... functions return 0 or -1 (or some such) setting errno. * When libxl_xs_get_dompath fails, do not treat it as an allocation error. It isn't: it usually means xenstored failed. * Remove many spurious \n's from log messages. (The applications log callback is expected to add a \n if it wants to do that, so libxl's logging functions should be passed strings without \n.) Signed-off-by: Ian Jackson --- tools/libxl/libxl_internal.c | 39 +++++++++++++++++++++++++++++++++------ 1 file changed, 33 insertions(+), 6 deletions(-) (limited to 'tools/libxl/libxl_internal.c') diff --git a/tools/libxl/libxl_internal.c b/tools/libxl/libxl_internal.c index 407cf2230e..7ba0c9790f 100644 --- a/tools/libxl/libxl_internal.c +++ b/tools/libxl/libxl_internal.c @@ -146,14 +146,41 @@ char *libxl_dirname(struct libxl_ctx *ctx, const char *s) return ptr; } -void xl_log(struct libxl_ctx *ctx, int loglevel, const char *file, int line, const char *func, char *fmt, ...) +void xl_logv(struct libxl_ctx *ctx, int loglevel, int errnoval, + const char *file, int line, const char *func, + char *fmt, va_list ap) { - va_list ap; + char *enomem = "[out of memory formatting log message]"; char *s; - va_start(ap, fmt); - vasprintf(&s, fmt, ap); - va_end(ap); + int rc; + + 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; + } + x: ctx->log_callback(ctx->log_userdata, loglevel, file, line, func, s); - free(s); + if (s != enomem) + free(s); +} + +void xl_log(struct libxl_ctx *ctx, int loglevel, 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); + va_end(ap); } -- cgit v1.2.3