From 9d3faa47531b1f78edd5933eba2543c26badcb4e Mon Sep 17 00:00:00 2001 From: Petr Rockai Date: Wed, 5 May 2010 22:37:52 +0000 Subject: [PATCH] Suppress duplicate error messages about read failures and missing devices. --- lib/commands/toolcontext.c | 2 ++ lib/device/dev-io.c | 12 ++++++------ lib/format_text/import_vsn1.c | 2 +- lib/log/log.c | 25 +++++++++++++++++++++++-- lib/log/log.h | 3 +++ lib/log/lvm-logging.h | 1 + tools/lvmcmdline.c | 1 + 7 files changed, 37 insertions(+), 9 deletions(-) diff --git a/lib/commands/toolcontext.c b/lib/commands/toolcontext.c index 70f5231e5..69c79e7c5 100644 --- a/lib/commands/toolcontext.c +++ b/lib/commands/toolcontext.c @@ -194,6 +194,7 @@ static void _init_logging(struct cmd_context *cmd) #ifdef DEVMAPPER_SUPPORT dm_log_with_errno_init(print_log); #endif + reset_log_duplicated(); } static int _process_config(struct cmd_context *cmd) @@ -1361,6 +1362,7 @@ void destroy_toolcontext(struct cmd_context *cmd) release_log_memory(); activation_exit(); + reset_log_duplicated(); fin_log(); fin_syslog(); reset_lvm_errno(0); diff --git a/lib/device/dev-io.c b/lib/device/dev-io.c index 5684b690e..1522224de 100644 --- a/lib/device/dev-io.c +++ b/lib/device/dev-io.c @@ -95,12 +95,12 @@ static int _io(struct device_area *where, void *buffer, int should_write) while ((n < 0) && ((errno == EINTR) || (errno == EAGAIN))); if (n < 0) - log_error("%s: %s failed after %" PRIu64 " of %" PRIu64 - " at %" PRIu64 ": %s", dev_name(where->dev), - should_write ? "write" : "read", - (uint64_t) total, - (uint64_t) where->size, - (uint64_t) where->start, strerror(errno)); + log_error_once("%s: %s failed after %" PRIu64 " of %" PRIu64 + " at %" PRIu64 ": %s", dev_name(where->dev), + should_write ? "write" : "read", + (uint64_t) total, + (uint64_t) where->size, + (uint64_t) where->start, strerror(errno)); if (n <= 0) break; diff --git a/lib/format_text/import_vsn1.c b/lib/format_text/import_vsn1.c index ddc1ae062..316adb555 100644 --- a/lib/format_text/import_vsn1.c +++ b/lib/format_text/import_vsn1.c @@ -198,7 +198,7 @@ static int _read_pv(struct format_instance *fid, struct dm_pool *mem, if (!id_write_format(&pv->id, buffer, sizeof(buffer))) buffer[0] = '\0'; if (report_missing_devices) - log_error("Couldn't find device with uuid %s.", buffer); + log_error_once("Couldn't find device with uuid %s.", buffer); else log_very_verbose("Couldn't find device with uuid %s.", buffer); } diff --git a/lib/log/log.c b/lib/log/log.c index dc3696b13..5c4958636 100644 --- a/lib/log/log.c +++ b/lib/log/log.c @@ -169,6 +169,14 @@ const char *stored_errmsg(void) return _lvm_errmsg ? : ""; } +static struct dm_hash_table *_duplicated = NULL; + +void reset_log_duplicated(void) { + if (_duplicated) + dm_hash_destroy(_duplicated); + _duplicated = NULL; +} + void print_log(int level, const char *file, int line, int dm_errno, const char *format, ...) { @@ -179,9 +187,10 @@ void print_log(int level, const char *file, int line, int dm_errno, const char *trformat; /* Translated format string */ char *newbuf; int use_stderr = level & _LOG_STDERR; + int log_once = level & _LOG_ONCE; int fatal_internal_error = 0; - level &= ~_LOG_STDERR; + level &= ~(_LOG_STDERR|_LOG_ONCE); if (_abort_on_internal_errors && !strncmp(format, INTERNAL_ERROR, @@ -203,7 +212,9 @@ void print_log(int level, const char *file, int line, int dm_errno, if (dm_errno && !_lvm_errno) _lvm_errno = dm_errno; - if (_lvm2_log_fn || (_store_errmsg && (level <= _LOG_ERR))) { + if (_lvm2_log_fn || + (_store_errmsg && (level <= _LOG_ERR)) || + log_once) { va_start(ap, format); n = vsnprintf(buf2, sizeof(buf2) - 1, trformat, ap); va_end(ap); @@ -229,6 +240,16 @@ void print_log(int level, const char *file, int line, int dm_errno, } } + if (log_once) { + if (!_duplicated) + _duplicated = dm_hash_create(128); + if (_duplicated) { + if (dm_hash_lookup(_duplicated, message)) + level = _LOG_NOTICE; + dm_hash_insert(_duplicated, message, (void*)1); + } + } + if (_lvm2_log_fn) { _lvm2_log_fn(level, file, line, 0, message); if (fatal_internal_error) diff --git a/lib/log/log.h b/lib/log/log.h index 4c3b476c2..db09630d6 100644 --- a/lib/log/log.h +++ b/lib/log/log.h @@ -47,6 +47,7 @@ #define _LOG_STDERR 128 /* force things to go to stderr, even if loglevel would make them go to stdout */ +#define _LOG_ONCE 256 /* downgrade to NOTICE if this has been already logged */ #define _LOG_DEBUG 7 #define _LOG_INFO 6 #define _LOG_NOTICE 5 @@ -62,6 +63,7 @@ #define log_warn_suppress(s, x...) LOG_LINE(s ? _LOG_NOTICE : _LOG_WARN | _LOG_STDERR, x) #define log_err(x...) LOG_LINE_WITH_ERRNO(_LOG_ERR, EUNCLASSIFIED, x) #define log_err_suppress(s, x...) LOG_LINE_WITH_ERRNO(s ? _LOG_NOTICE : _LOG_ERR, EUNCLASSIFIED, x) +#define log_err_once(x...) LOG_LINE_WITH_ERRNO(_LOG_ERR | _LOG_ONCE, EUNCLASSIFIED, x) #define log_fatal(x...) LOG_LINE_WITH_ERRNO(_LOG_FATAL, EUNCLASSIFIED, x) #define stack log_debug("") /* Backtrace on error */ @@ -70,6 +72,7 @@ #define log_print(args...) LOG_LINE(_LOG_WARN, args) #define log_error(args...) log_err(args) #define log_error_suppress(s, args...) log_err_suppress(s, args) +#define log_error_once(args...) log_err_once(args) #define log_errno(args...) LOG_LINE_WITH_ERRNO(_LOG_ERR, args) /* System call equivalents */ diff --git a/lib/log/lvm-logging.h b/lib/log/lvm-logging.h index 838dc1f2e..1c0a58069 100644 --- a/lib/log/lvm-logging.h +++ b/lib/log/lvm-logging.h @@ -43,6 +43,7 @@ void init_abort_on_internal_errors(int fatal); void fin_log(void); void release_log_memory(void); +void reset_log_duplicated(void); void init_syslog(int facility); void fin_syslog(void); diff --git a/tools/lvmcmdline.c b/tools/lvmcmdline.c index 033ef74df..22a5eef94 100644 --- a/tools/lvmcmdline.c +++ b/tools/lvmcmdline.c @@ -1087,6 +1087,7 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv) dm_pool_empty(cmd->mem); reset_lvm_errno(1); + reset_log_duplicated(); return ret; } -- 2.43.5