diff --git a/WHATS_NEW_DM b/WHATS_NEW_DM index d2d44acf4..a4dd6eb82 100644 --- a/WHATS_NEW_DM +++ b/WHATS_NEW_DM @@ -1,5 +1,6 @@ Version 1.02.136 - ====================================== + Use dm_log_with_errno and translate runtime to dm_log only when needed. Make log messages from dm and lvm library differnt from dmeventd. Notice and Info messages are again logged from dmeventd and its plugins. Dmeventd now also respects DM_ABORT_ON_INTERNAL_ERRORS as libdm based tool. diff --git a/libdm/libdm-common.c b/libdm/libdm-common.c index 17f55ab46..476f01845 100644 --- a/libdm/libdm-common.c +++ b/libdm/libdm-common.c @@ -161,14 +161,59 @@ static void _default_log(int level, const char *file, dm_log_fn dm_log = _default_log; dm_log_with_errno_fn dm_log_with_errno = _default_log_with_errno; +/* + * Wrapper function to reformat new messages to and + * old style logging which had not used errno parameter + * + * As we cannot simply pass '...' to old function we + * need to process arg list locally and just pass '%s' + buffer + */ +__attribute__((format(printf, 5, 6))) +static void _log_to_default_log(int level, + const char *file, int line, int dm_errno_or_class, + const char *f, ...) +{ + va_list ap; + char buf[2 * PATH_MAX + 256]; /* big enough for most messages */ + + va_start(ap, f); + vsnprintf(buf, sizeof(buf), f, ap); + va_end(ap); + + dm_log(level, file, line, "%s", buf); +} + +/* + * Wrapper function take 'old' style message without errno + * and log it via new logging function with errno arg + * + * This minor case may happen if new libdm is used with old + * recompiled tool that would decided to use new logging, + * but still would like to use old binary plugins. + */ +__attribute__((format(printf, 4, 5))) +static void _log_to_default_log_with_errno(int level, + const char *file, int line, const char *f, ...) +{ + va_list ap; + char buf[2 * PATH_MAX + 256]; /* big enough for most messages */ + + va_start(ap, f); + vsnprintf(buf, sizeof(buf), f, ap); + va_end(ap); + + dm_log_with_errno(level, file, line, 0, "%s", buf); +} + void dm_log_init(dm_log_fn fn) { - if (fn) + if (fn) { dm_log = fn; - else + dm_log_with_errno = _log_to_default_log; + } else { dm_log = _default_log; - - dm_log_with_errno = _default_log_with_errno; + dm_log_with_errno = _default_log_with_errno; + } } int dm_log_is_non_default(void) @@ -178,12 +223,13 @@ int dm_log_is_non_default(void) void dm_log_with_errno_init(dm_log_with_errno_fn fn) { - if (fn) + if (fn) { + dm_log = _log_to_default_log_with_errno; dm_log_with_errno = fn; - else + } else { + dm_log = _default_log; dm_log_with_errno = _default_log_with_errno; - - dm_log = _default_log; + } } void dm_log_init_verbose(int level) diff --git a/libdm/misc/dm-logging.h b/libdm/misc/dm-logging.h index 4088bfe60..083664d6c 100644 --- a/libdm/misc/dm-logging.h +++ b/libdm/misc/dm-logging.h @@ -1,6 +1,6 @@ /* * Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. - * Copyright (C) 2004-2007 Red Hat, Inc. All rights reserved. + * Copyright (C) 2004-2016 Red Hat, Inc. All rights reserved. * * This file is part of LVM2. * @@ -18,16 +18,10 @@ #include "libdevmapper.h" -extern dm_log_fn dm_log; extern dm_log_with_errno_fn dm_log_with_errno; #define LOG_MESG(l, f, ln, e, x...) \ - do { \ - if (dm_log_is_non_default()) \ - dm_log(l, f, ln, ## x); \ - else \ - dm_log_with_errno(l, f, ln, e, ## x); \ - } while (0) + dm_log_with_errno(l, f, ln, e, ## x) #define LOG_LINE(l, x...) LOG_MESG(l, __FILE__, __LINE__, 0, ## x) #define LOG_LINE_WITH_ERRNO(l, e, x...) LOG_MESG(l, __FILE__, __LINE__, e, ## x)