#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;
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,
#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;
} 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;
#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 */
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;
}
int rc;
struct libxl_ctx clone;
- if (libxl_ctx_init(&clone, LIBXL_VERSION)) {
+ if (libxl_ctx_init(&clone, LIBXL_VERSION, ctx->lg)) {
return -1;
}
_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)
{
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);
}
#include <xs.h>
#include <xenctrl.h>
+#include "xentoollog.h"
#include "flexarray.h"
#include "libxl_utils.h"
/* 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 {
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
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,
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
#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)
{
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));
#ifndef XL_H
#define XL_H
+#include "xentoollog.h"
+
struct cmd_spec {
char *cmd_name;
int (*cmd_impl)(int argc, char **argv);
extern int cmdtable_len;
extern struct libxl_ctx ctx;
-extern int logfile;
+extern xentoollog_logger *logger;
#endif /* XL_H */
}
}
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;
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;