summaryrefslogtreecommitdiff
path: root/lib/log
diff options
context:
space:
mode:
Diffstat (limited to 'lib/log')
-rw-r--r--lib/log/log.c68
-rw-r--r--lib/log/log.h3
2 files changed, 44 insertions, 27 deletions
diff --git a/lib/log/log.c b/lib/log/log.c
index 86b4988..d50cb4a 100644
--- a/lib/log/log.c
+++ b/lib/log/log.c
@@ -16,10 +16,7 @@
#include "lib.h"
#include "device.h"
#include "memlock.h"
-#include "lvm-string.h"
-#include "lvm-file.h"
#include "defaults.h"
-#include "config.h"
#include <stdarg.h>
#include <syslog.h>
@@ -43,6 +40,9 @@ static lvm2_log_fn_t _lvm2_log_fn = NULL;
static int _lvm_errno = 0;
static int _store_errmsg = 0;
static char *_lvm_errmsg = NULL;
+static size_t _lvm_errmsg_size = 0;
+static size_t _lvm_errmsg_len = 0;
+#define MAX_ERRMSG_LEN (512 * 1024) /* Max size of error buffer 512KB */
void init_log_fn(lvm2_log_fn_t log_fn)
{
@@ -107,7 +107,7 @@ void release_log_memory(void)
void fin_log(void)
{
if (_log_direct) {
- dev_close(&_log_dev);
+ (void) dev_close(&_log_dev);
_log_direct = 0;
}
@@ -124,7 +124,7 @@ void fin_log(void)
}
}
-void fin_syslog()
+void fin_syslog(void)
{
if (_syslog)
closelog();
@@ -133,7 +133,7 @@ void fin_syslog()
void init_msg_prefix(const char *prefix)
{
- strncpy(_msg_prefix, prefix, sizeof(_msg_prefix));
+ strncpy(_msg_prefix, prefix, sizeof(_msg_prefix) - 1);
_msg_prefix[sizeof(_msg_prefix) - 1] = '\0';
}
@@ -154,6 +154,7 @@ void reset_lvm_errno(int store_errmsg)
if (_lvm_errmsg) {
dm_free(_lvm_errmsg);
_lvm_errmsg = NULL;
+ _lvm_errmsg_size = _lvm_errmsg_len = 0;
}
_store_errmsg = store_errmsg;
@@ -172,16 +173,17 @@ const char *stored_errmsg(void)
static struct dm_hash_table *_duplicated = NULL;
void reset_log_duplicated(void) {
- if (_duplicated)
+ if (_duplicated) {
dm_hash_destroy(_duplicated);
- _duplicated = NULL;
+ _duplicated = NULL;
+ }
}
void print_log(int level, const char *file, int line, int dm_errno,
const char *format, ...)
{
va_list ap;
- char buf[1024], buf2[4096], locn[4096];
+ char buf[1024], locn[4096];
int bufused, n;
const char *message;
const char *trformat; /* Translated format string */
@@ -189,6 +191,7 @@ void print_log(int level, const char *file, int line, int dm_errno,
int use_stderr = level & _LOG_STDERR;
int log_once = level & _LOG_ONCE;
int fatal_internal_error = 0;
+ size_t msglen;
level &= ~(_LOG_STDERR|_LOG_ONCE);
@@ -216,7 +219,7 @@ void print_log(int level, const char *file, int line, int dm_errno,
(_store_errmsg && (level <= _LOG_ERR)) ||
log_once) {
va_start(ap, format);
- n = vsnprintf(buf2, sizeof(buf2) - 1, trformat, ap);
+ n = vsnprintf(locn, sizeof(locn) - 1, trformat, ap);
va_end(ap);
if (n < 0) {
@@ -225,18 +228,29 @@ void print_log(int level, const char *file, int line, int dm_errno,
goto log_it;
}
- buf2[sizeof(buf2) - 1] = '\0';
- message = &buf2[0];
+ locn[sizeof(locn) - 1] = '\0';
+ message = locn;
}
- if (_store_errmsg && (level <= _LOG_ERR)) {
- if (!_lvm_errmsg)
- _lvm_errmsg = dm_strdup(message);
- else if ((newbuf = dm_realloc(_lvm_errmsg,
- strlen(_lvm_errmsg) +
- strlen(message) + 2))) {
- _lvm_errmsg = strcat(newbuf, "\n");
- _lvm_errmsg = strcat(newbuf, message);
+/* FIXME Avoid pointless use of message buffer when it'll never be read! */
+ if (_store_errmsg && (level <= _LOG_ERR) &&
+ _lvm_errmsg_len < MAX_ERRMSG_LEN) {
+ msglen = strlen(message);
+ if ((_lvm_errmsg_len + msglen + 1) >= _lvm_errmsg_size) {
+ _lvm_errmsg_size = 2 * (_lvm_errmsg_len + msglen + 1);
+ if ((newbuf = dm_realloc(_lvm_errmsg,
+ _lvm_errmsg_size)))
+ _lvm_errmsg = newbuf;
+ else
+ _lvm_errmsg_size = _lvm_errmsg_len;
+ }
+ if (_lvm_errmsg &&
+ (_lvm_errmsg_len + msglen + 2) < _lvm_errmsg_size) {
+ /* prepend '\n' and copy with '\0' but do not count in */
+ if (_lvm_errmsg_len)
+ _lvm_errmsg[_lvm_errmsg_len++] = '\n';
+ memcpy(_lvm_errmsg + _lvm_errmsg_len, message, msglen + 1);
+ _lvm_errmsg_len += msglen;
}
}
@@ -246,7 +260,7 @@ void print_log(int level, const char *file, int line, int dm_errno,
if (_duplicated) {
if (dm_hash_lookup(_duplicated, message))
level = _LOG_NOTICE;
- dm_hash_insert(_duplicated, message, (void*)1);
+ (void) dm_hash_insert(_duplicated, message, (void*)1);
}
}
@@ -260,8 +274,8 @@ void print_log(int level, const char *file, int line, int dm_errno,
log_it:
if (!_log_suppress) {
if (verbose_level() > _LOG_DEBUG)
- dm_snprintf(locn, sizeof(locn), "#%s:%d ",
- file, line);
+ (void) dm_snprintf(locn, sizeof(locn), "#%s:%d ",
+ file, line);
else
locn[0] = '\0';
@@ -336,7 +350,7 @@ void print_log(int level, const char *file, int line, int dm_errno,
if (level > debug_level())
return;
- if (_log_to_file && (_log_while_suspended || !memlock())) {
+ if (_log_to_file && (_log_while_suspended || !critical_section())) {
fprintf(_log_file, "%s:%d %s%s", file, line, log_command_name(),
_msg_prefix);
@@ -348,14 +362,14 @@ void print_log(int level, const char *file, int line, int dm_errno,
fflush(_log_file);
}
- if (_syslog && (_log_while_suspended || !memlock())) {
+ if (_syslog && (_log_while_suspended || !critical_section())) {
va_start(ap, format);
vsyslog(level, trformat, ap);
va_end(ap);
}
/* FIXME This code is unfinished - pre-extend & condense. */
- if (!_already_logging && _log_direct && memlock()) {
+ if (!_already_logging && _log_direct && critical_section()) {
_already_logging = 1;
memset(&buf, ' ', sizeof(buf));
bufused = 0;
@@ -372,8 +386,8 @@ void print_log(int level, const char *file, int line, int dm_errno,
va_end(ap);
bufused += n;
- done:
buf[bufused - 1] = '\n';
+ done:
buf[bufused] = '\n';
buf[sizeof(buf) - 1] = '\n';
/* FIXME real size bufused */
diff --git a/lib/log/log.h b/lib/log/log.h
index 10f34be..f0da64f 100644
--- a/lib/log/log.h
+++ b/lib/log/log.h
@@ -68,6 +68,7 @@
#define log_very_verbose(args...) log_info(args)
#define log_verbose(args...) log_notice(args)
#define log_print(args...) LOG_LINE(_LOG_WARN, args)
+#define log_print_unless_silent(args...) LOG_LINE(silent_mode() ? _LOG_NOTICE : _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)
@@ -76,6 +77,8 @@
/* System call equivalents */
#define log_sys_error(x, y) \
log_err("%s: %s failed: %s", y, x, strerror(errno))
+#define log_sys_error_suppress(s, x, y) \
+ log_err_suppress(s, "%s: %s failed: %s", y, x, strerror(errno))
#define log_sys_very_verbose(x, y) \
log_info("%s: %s failed: %s", y, x, strerror(errno))
#define log_sys_debug(x, y) \