]> sourceware.org Git - lvm2.git/commitdiff
shell: also collect last command's return code for subsequent 'lastlog' invocation
authorPeter Rajnoha <prajnoha@redhat.com>
Mon, 25 Jul 2016 10:20:22 +0000 (12:20 +0200)
committerPeter Rajnoha <prajnoha@redhat.com>
Tue, 9 Aug 2016 16:24:45 +0000 (18:24 +0200)
Add new log_context=shell and with log_object_type=cmd and
log_object_name=<command_name> for command log report to collect
overall return code from last command (this is reported under
log_type=status).

lib/log/log.c
lib/log/lvm-logging.h
tools/lvm.c

index d021ffe6e991744f74fe0766df32b1da437e5d71..c933154b5a53ee9936c1658c5ab86797029a4110 100644 (file)
@@ -449,6 +449,7 @@ static const char *_get_log_level_name(int use_stderr, int level)
 const char *log_get_report_context_name(log_report_context_t context)
 {
        static const char *log_context_names[LOG_REPORT_CONTEXT_COUNT] = {[LOG_REPORT_CONTEXT_NULL] = "",
+                                                                         [LOG_REPORT_CONTEXT_SHELL] = "shell",
                                                                          [LOG_REPORT_CONTEXT_PROCESSING] = "processing"};
        return log_context_names[context];
 }
@@ -457,6 +458,7 @@ const char *log_get_report_context_name(log_report_context_t context)
 const char *log_get_report_object_type_name(log_report_object_type_t object_type)
 {
        static const char *log_object_type_names[LOG_REPORT_OBJECT_TYPE_COUNT] = {[LOG_REPORT_OBJECT_TYPE_NULL] = "",
+                                                                                 [LOG_REPORT_OBJECT_TYPE_CMD] = "cmd",
                                                                                  [LOG_REPORT_OBJECT_TYPE_ORPHAN] = "orphan",
                                                                                  [LOG_REPORT_OBJECT_TYPE_PV] = "pv",
                                                                                  [LOG_REPORT_OBJECT_TYPE_LABEL] = "label",
index 7ddbf2c31e765f0b93cad72af56e50a4f4a2429d..64c1928ff77335915c14cd8b6ad10f89f2ec82ff 100644 (file)
@@ -77,12 +77,14 @@ void syslog_suppress(int suppress);
 /* Hooks to handle logging through report. */
 typedef enum {
        LOG_REPORT_CONTEXT_NULL,
+       LOG_REPORT_CONTEXT_SHELL,
        LOG_REPORT_CONTEXT_PROCESSING,
        LOG_REPORT_CONTEXT_COUNT
 } log_report_context_t;
 
 typedef enum {
        LOG_REPORT_OBJECT_TYPE_NULL,
+       LOG_REPORT_OBJECT_TYPE_CMD,
        LOG_REPORT_OBJECT_TYPE_ORPHAN,
        LOG_REPORT_OBJECT_TYPE_PV,
        LOG_REPORT_OBJECT_TYPE_LABEL,
@@ -101,6 +103,10 @@ typedef struct log_report {
        const char *object_group_id;
 } log_report_t;
 
+#define LOG_STATUS_NAME "status"
+#define LOG_STATUS_SUCCESS "success"
+#define LOG_STATUS_FAILURE "failure"
+
 log_report_t log_get_report_state(void);
 void log_restore_report_state(log_report_t log_report);
 
index 62cc141900e0627341b90b06c42527a0399af32c..ec8a3522e3052102284479aa6106853b485de3cf 100644 (file)
@@ -181,9 +181,29 @@ static void _write_history(void)
                log_very_verbose("Couldn't write history to %s.", hist_file);
 }
 
+static int _log_shell_command_status(struct cmd_context *cmd, int ret_code)
+{
+       log_report_t log_state;
+
+       if (!cmd->log_rh)
+               return 1;
+
+       log_state = log_get_report_state();
+
+       return report_cmdlog(cmd->log_rh, REPORT_OBJECT_CMDLOG_NAME,
+                            log_get_report_context_name(log_state.context),
+                            log_get_report_object_type_name(log_state.object_type),
+                            log_state.object_name, log_state.object_id,
+                            log_state.object_group, log_state.object_group_id,
+                            ret_code == ECMD_PROCESSED ? REPORT_OBJECT_CMDLOG_SUCCESS
+                                                       : REPORT_OBJECT_CMDLOG_FAILURE,
+                            stored_errno(), ret_code);
+}
+
 int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
 {
-       int argc, ret;
+       log_report_t saved_log_report_state = log_get_report_state();
+       int is_lastlog_cmd, argc, ret;
        char *input = NULL, *args[MAX_ARGS], **argv;
 
        rl_readline_name = "lvm";
@@ -194,7 +214,11 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
        _cmdline = cmdline;
 
        cmd->is_interactive = 1;
+       log_set_report_context(LOG_REPORT_CONTEXT_SHELL);
+       log_set_report_object_type(LOG_REPORT_OBJECT_TYPE_CMD);
+
        while (1) {
+               log_set_report_object_name_and_id(NULL, NULL);
                free(input);
                input = readline("lvm> ");
 
@@ -229,13 +253,17 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
                if (!argc)
                        continue;
 
+               log_set_report_object_name_and_id(argv[0], NULL);
+
                if (!strcmp(argv[0], "quit") || !strcmp(argv[0], "exit")) {
                        remove_history(history_length - 1);
                        log_error("Exiting.");
                        break;
                }
 
-               if (cmd->log_rh && strcmp(argv[0], "lastlog")) {
+               is_lastlog_cmd = !strcmp(argv[0], "lastlog");
+
+               if (cmd->log_rh && !is_lastlog_cmd) {
                        /* drop old log report */
                        dm_report_free(cmd->log_rh);
                        cmd->log_rh = NULL;
@@ -251,7 +279,12 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
                        log_error("Command failed with status code %d.", ret);
                }
                _write_history();
+
+               if (!is_lastlog_cmd)
+                       _log_shell_command_status(cmd, ret);
        }
+
+       log_restore_report_state(saved_log_report_state);
        cmd->is_interactive = 0;
 
        free(input);
This page took 0.043469 seconds and 5 git commands to generate.