]> sourceware.org Git - lvm2.git/commitdiff
Store any errno and error messages issued while processing each command.
authorAlasdair Kergon <agk@redhat.com>
Wed, 15 Jul 2009 23:57:54 +0000 (23:57 +0000)
committerAlasdair Kergon <agk@redhat.com>
Wed, 15 Jul 2009 23:57:54 +0000 (23:57 +0000)
(Enabled by default while we test it, but in due course we'll only store
the error messages when we need to.)

WHATS_NEW
daemons/clvmd/lvm-functions.c
doc/example_cmdlib.c
lib/commands/toolcontext.c
lib/log/log.c
lib/log/lvm-logging.h
po/pogen.h
tools/lvmcmdline.c

index 3589ebbc31c20aea7efe68144a198dbbdd45c981..7fe3a5b06239181504d1e06cc5a77e8f15ba940e 100644 (file)
--- a/WHATS_NEW
+++ b/WHATS_NEW
@@ -1,5 +1,6 @@
 Version 2.02.50 - 
 ================================
+  Store any errno and error messages issued while processing each command.
   Use log_error macro consistently throughout in place of log_err.
 
 Version 2.02.49 - 15th July 2009
index d559b3a1fc5e87c55bb1c4960915726972024a78..1be07813c9e0c26bc94e87707112dc75cc7d5909 100644 (file)
@@ -714,7 +714,7 @@ static void *get_initial_state()
        return NULL;
 }
 
-static void lvm2_log_fn(int level, const char *file, int line,
+static void lvm2_log_fn(int level, const char *file, int line, int dm_errno,
                        const char *message)
 {
 
@@ -723,7 +723,7 @@ static void lvm2_log_fn(int level, const char *file, int line,
           We need to NULL the function ptr otherwise it will just call
           back into here! */
        init_log_fn(NULL);
-       print_log(level, file, line, "%s", message);
+       print_log(level, file, line, dm_errno, "%s", message);
        init_log_fn(lvm2_log_fn);
 
        /*
index d251fa92cf17b597b04120dc855e0847cd4e13d9..1fff36e29384f928bd4250c495b5ffeaf1a98d8b 100644 (file)
@@ -15,7 +15,8 @@
 #include "lvm2cmd.h"
 
 /* All output gets passed to this function line-by-line */
-void test_log_fn(int level, const char *file, int line, const char *format)
+void test_log_fn(int level, int dm_errno, const char *file, int line,
+                const char *format)
 {
        /* Extract and process output here rather than printing it */
 
index c59ad55c19d7d5ba48b1f59b6015e2230f1350a1..dc7aa406386f9e553c0a6fd8c467f2a733023943 100644 (file)
@@ -190,7 +190,7 @@ static void _init_logging(struct cmd_context *cmd)
 
        /* Tell device-mapper about our logging */
 #ifdef DEVMAPPER_SUPPORT
-       dm_log_init(print_log);
+       dm_log_with_errno_init(print_log);
 #endif
 }
 
@@ -1161,6 +1161,7 @@ struct cmd_context *create_toolcontext(unsigned is_long_lived,
        cmd->current_settings = cmd->default_settings;
 
        cmd->config_valid = 1;
+       reset_lvm_errno(1);  /* FIXME Move to top when cmd returned on error */
        return cmd;
 
       error:
@@ -1288,6 +1289,8 @@ int refresh_toolcontext(struct cmd_context *cmd)
                persistent_filter_dump(cmd->filter);
 
        cmd->config_valid = 1;
+
+       reset_lvm_errno(1);
        return 1;
 }
 
@@ -1317,4 +1320,5 @@ void destroy_toolcontext(struct cmd_context *cmd)
        activation_exit();
        fin_log();
        fin_syslog();
+       reset_lvm_errno(0);
 }
index 1f67b3fedba4deae82b75cdf4cb82a23976579ba..21163f89ead802f90dc02b2f66ac386820df87f7 100644 (file)
@@ -38,6 +38,10 @@ static int _already_logging = 0;
 
 static lvm2_log_fn_t _lvm2_log_fn = NULL;
 
+static int _lvm_errno = 0;
+static int _store_errmsg = 0;
+static char *_lvm_errmsg = NULL;
+
 void init_log_fn(lvm2_log_fn_t log_fn)
 {
        if (log_fn)
@@ -136,13 +140,37 @@ void init_indent(int indent)
        _indent = indent;
 }
 
-void print_log(int level, const char *file, int line, const char *format, ...)
+void reset_lvm_errno(int store_errmsg)
+{
+       _lvm_errno = 0;
+
+       if (_lvm_errmsg) {
+               dm_free(_lvm_errmsg);
+               _lvm_errmsg = NULL;
+       }
+
+       _store_errmsg = store_errmsg;
+}
+
+int lvm_errno(void)
+{
+       return _lvm_errno;
+}
+
+const char *lvm_errmsg(void)
+{
+       return _lvm_errmsg ? : "";
+}
+
+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];
        int bufused, n;
        const char *message;
        const char *trformat;           /* Translated format string */
+       char *newbuf;
        int use_stderr = level & _LOG_STDERR;
 
        level &= ~_LOG_STDERR;
@@ -155,7 +183,10 @@ void print_log(int level, const char *file, int line, const char *format, ...)
 
        trformat = _(format);
 
-       if (_lvm2_log_fn) {
+       if (dm_errno && !_lvm_errno)
+               _lvm_errno = dm_errno;
+
+       if (_lvm2_log_fn || (_store_errmsg && (level == _LOG_ERR))) {
                va_start(ap, format);
                n = vsnprintf(buf2, sizeof(buf2) - 1, trformat, ap);
                va_end(ap);
@@ -168,8 +199,21 @@ void print_log(int level, const char *file, int line, const char *format, ...)
 
                buf2[sizeof(buf2) - 1] = '\0';
                message = &buf2[0];
+       }
 
-               _lvm2_log_fn(level, file, line, message);
+       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);
+               }
+       }
+
+       if (_lvm2_log_fn) {
+               _lvm2_log_fn(level, file, line, 0, message);
 
                return;
        }
index d267e2336c27e30e6e5f1407ca7f4b3a15140931..a0933dd10faffbedca27ea3d56a04498372b08cb 100644 (file)
 #ifndef _LVM_LOGGING_H
 #define _LVM_LOGGING_H
 
-void print_log(int level, const char *file, int line, const char *format, ...)
-    __attribute__ ((format(printf, 4, 5)));
+void print_log(int level, const char *file, int line, int dm_errno,
+              const char *format, ...)
+    __attribute__ ((format(printf, 5, 6)));
 
-#define LOG_LINE(l, x...) print_log(l, __FILE__, __LINE__ , ## x)
+#define LOG_LINE(l, x...) print_log(l, __FILE__, __LINE__ , 0, ## x)
 
 #include "log.h"
 
 typedef void (*lvm2_log_fn_t) (int level, const char *file, int line,
-                              const char *message);
+                              int dm_errno, const char *message);
 
 void init_log_fn(lvm2_log_fn_t log_fn);
 
@@ -42,6 +43,9 @@ void init_syslog(int facility);
 void fin_syslog(void);
 
 int error_message_produced(void);
+void reset_lvm_errno(int store_errmsg);
+int lvm_errno(void);
+const char *lvm_errmsg(void);
 
 /* Suppress messages to stdout/stderr (1) or everywhere (2) */
 /* Returns previous setting */
index 6ab8154516c9d284c45f36f1580cea819b20dfd7..66940b97ad5f22e9636b694c0c506038d45dcaff 100644 (file)
@@ -19,8 +19,8 @@
  * different architectures.
  */
 
-#define print_log(level, file, line, format, args...) print_log(format, args)
+#define print_log(level, dm_errno, file, line, format, args...) print_log(format, args)
 #define dm_log(level, file, line, format, args...) dm_log(format, args)
-#define dm_log_with_errno(level, file, line, format, dm_errno, args...) \
-    dm_log(format, args)
+#define dm_log_with_errno(level, dm_errno, file, line, format, args...) \
+    dm_log(level, file, line, format, args)
 
index efd996aeef262d4fa6c5990fc238238ee827ce2e..33e549b1c8df619b4b86dec0cbfef6a7375e063e 100644 (file)
@@ -1054,6 +1054,8 @@ int lvm_run_command(struct cmd_context *cmd, int argc, char **argv)
         */
        dm_pool_empty(cmd->mem);
 
+       reset_lvm_errno(1);
+
        return ret;
 }
 
This page took 0.053364 seconds and 5 git commands to generate.