1
0
mirror of git://sourceware.org/git/lvm2.git synced 2024-12-21 13:34:40 +03:00

libdm: use dm_log_with_errno always

Instead of compiling 2 log call for 2 different logging functions,
and runtime decide which version to use - use only 'newer' function
and when user sets his own OLD dm_log logging translate it runtime
for old arg list set.

The positive part is - we get shorter generated library,
on the negative part this translation means, we always have evaluate
all args and print the message into local on stack buffer, before
we can pass this buffer to the users' logging function with proper
expected parameters (and such function may later decide to discard
logging based on message level so whole printing was unnecessary).
This commit is contained in:
Zdenek Kabelac 2016-11-03 17:15:07 +01:00
parent 221d8ff2a4
commit ee13f265f0
3 changed files with 57 additions and 16 deletions

View File

@ -1,5 +1,6 @@
Version 1.02.136 - 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. Make log messages from dm and lvm library differnt from dmeventd.
Notice and Info messages are again logged from dmeventd and its plugins. 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. Dmeventd now also respects DM_ABORT_ON_INTERNAL_ERRORS as libdm based tool.

View File

@ -161,14 +161,59 @@ static void _default_log(int level, const char *file,
dm_log_fn dm_log = _default_log; dm_log_fn dm_log = _default_log;
dm_log_with_errno_fn dm_log_with_errno = _default_log_with_errno; 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) void dm_log_init(dm_log_fn fn)
{ {
if (fn) if (fn) {
dm_log = fn; dm_log = fn;
else dm_log_with_errno = _log_to_default_log;
} else {
dm_log = _default_log; 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) 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) 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; dm_log_with_errno = fn;
else } else {
dm_log = _default_log;
dm_log_with_errno = _default_log_with_errno; dm_log_with_errno = _default_log_with_errno;
}
dm_log = _default_log;
} }
void dm_log_init_verbose(int level) void dm_log_init_verbose(int level)

View File

@ -1,6 +1,6 @@
/* /*
* Copyright (C) 2001-2004 Sistina Software, Inc. All rights reserved. * 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. * This file is part of LVM2.
* *
@ -18,16 +18,10 @@
#include "libdevmapper.h" #include "libdevmapper.h"
extern dm_log_fn dm_log;
extern dm_log_with_errno_fn dm_log_with_errno; extern dm_log_with_errno_fn dm_log_with_errno;
#define LOG_MESG(l, f, ln, e, x...) \ #define LOG_MESG(l, f, ln, e, x...) \
do { \ dm_log_with_errno(l, f, ln, e, ## x)
if (dm_log_is_non_default()) \
dm_log(l, f, ln, ## x); \
else \
dm_log_with_errno(l, f, ln, e, ## x); \
} while (0)
#define LOG_LINE(l, x...) LOG_MESG(l, __FILE__, __LINE__, 0, ## 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) #define LOG_LINE_WITH_ERRNO(l, e, x...) LOG_MESG(l, __FILE__, __LINE__, e, ## x)