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

lvm: shell: extend log report to cover whole lvm shell's main loop

When lvm commands are executed in lvm shell, we cover the whole lvm
command execution within this shell now. That means, all messages logged
and status caught during each command execution is now recorded in the
log report, including overall command's return code.
This commit is contained in:
Peter Rajnoha 2016-08-04 13:42:57 +02:00
parent 7d1125e5b7
commit f21afddeb7
4 changed files with 71 additions and 37 deletions

View File

@ -2218,11 +2218,6 @@ void destroy_toolcontext(struct cmd_context *cmd)
if (cmd->cft_def_hash)
dm_hash_destroy(cmd->cft_def_hash);
if (cmd->cmd_report.log_rh) {
dm_report_free(cmd->cmd_report.log_rh);
cmd->cmd_report.log_rh = NULL;
}
if (cmd->libmem)
dm_pool_destroy(cmd->libmem);

View File

@ -200,6 +200,12 @@ static int _log_shell_command_status(struct cmd_context *cmd, int ret_code)
stored_errno(), ret_code);
}
static void _discard_log_report_content(struct cmd_context *cmd)
{
if (cmd->cmd_report.log_rh)
dm_report_destroy_rows(cmd->cmd_report.log_rh);
}
int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
{
log_report_t saved_log_report_state = log_get_report_state();
@ -210,65 +216,76 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
rl_attempted_completion_function = (rl_completion_func_t *) _completion;
_read_history(cmd);
_cmdline = cmdline;
cmd->is_interactive = 1;
if (!report_format_init(cmd))
return_ECMD_FAILED;
log_set_report_context(LOG_REPORT_CONTEXT_SHELL);
log_set_report_object_type(LOG_REPORT_OBJECT_TYPE_CMD);
while (1) {
log_set_report(cmd->cmd_report.log_rh);
log_set_report_object_name_and_id(NULL, NULL);
free(input);
input = readline("lvm> ");
/* EOF */
if (!input) {
_discard_log_report_content(cmd);
/* readline sends prompt to stdout */
printf("\n");
break;
}
/* empty line */
if (!*input)
if (!*input) {
_discard_log_report_content(cmd);
continue;
}
add_history(input);
argv = args;
if (lvm_split(input, &argc, argv, MAX_ARGS) == MAX_ARGS) {
_discard_log_report_content(cmd);
log_error("Too many arguments, sorry.");
continue;
}
if (!argc)
if (!argc) {
_discard_log_report_content(cmd);
continue;
}
if (!strcmp(argv[0], "lvm")) {
argv++;
argc--;
}
if (!argc)
if (!argc) {
_discard_log_report_content(cmd);
continue;
}
log_set_report_object_name_and_id(argv[0], NULL);
is_lastlog_cmd = !strcmp(argv[0], "lastlog");
if (!is_lastlog_cmd)
_discard_log_report_content(cmd);
if (!strcmp(argv[0], "quit") || !strcmp(argv[0], "exit")) {
_discard_log_report_content(cmd);
remove_history(history_length - 1);
log_error("Exiting.");
break;
}
is_lastlog_cmd = !strcmp(argv[0], "lastlog");
if (cmd->cmd_report.log_rh && !is_lastlog_cmd) {
/* drop old log report */
dm_report_free(cmd->cmd_report.log_rh);
cmd->cmd_report.log_rh = NULL;
}
ret = lvm_run_command(cmd, argc, argv);
if (ret == ENO_SUCH_CMD)
log_error("No such command '%s'. Try 'help'.",
@ -282,12 +299,35 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
if (!is_lastlog_cmd)
_log_shell_command_status(cmd, ret);
log_set_report(NULL);
dm_report_group_output_and_pop_all(cmd->cmd_report.report_group);
if (cmd->cmd_report.log_rh &&
!(dm_report_group_push(cmd->cmd_report.report_group,
cmd->cmd_report.log_rh,
(void *) cmd->cmd_report.log_name))) {
log_set_report(NULL);
log_error("Failed to add log report.");
break;
}
}
log_restore_report_state(saved_log_report_state);
cmd->is_interactive = 0;
free(input);
if (cmd->cmd_report.report_group) {
dm_report_group_destroy(cmd->cmd_report.report_group);
cmd->cmd_report.report_group = NULL;
}
if (cmd->cmd_report.log_rh) {
dm_report_free(cmd->cmd_report.log_rh);
cmd->cmd_report.report_group = NULL;
}
return 0;
}

View File

@ -1387,6 +1387,11 @@ static int _report(struct cmd_context *cmd, int argc, char **argv, report_type_t
} else
r = _do_report(cmd, handle, &args, single_args);
if (!args.log_only && !dm_report_group_pop(cmd->cmd_report.report_group)) {
log_error("Failed to finalize main report section in report group.");
r = ECMD_FAILED;
}
destroy_processing_handle(cmd, handle);
return r;
}
@ -1508,32 +1513,22 @@ bad:
return 0;
}
int lastlog(struct cmd_context *cmd, int argc, char **argv)
int lastlog(struct cmd_context *cmd, int argc __attribute((unused)), char **argv __attribute__((unused)))
{
struct dm_report_group *report_group = NULL;
const char *selection;
int r = ECMD_FAILED;
if (!cmd->cmd_report.log_rh) {
log_error("No log report stored.");
goto out;
return ECMD_FAILED;
}
if (!_do_report_get_selection(cmd, CMDLOG, 1, NULL, &selection))
goto_out;
return_ECMD_FAILED;
if (!dm_report_set_selection(cmd->cmd_report.log_rh, selection)) {
log_error("Failed to set selection for log report.");
goto out;
return ECMD_FAILED;
}
if (!dm_report_output(cmd->cmd_report.log_rh) ||
!dm_report_group_pop(report_group))
goto_out;
r = ECMD_PROCESSED;
out:
if (!dm_report_group_destroy(report_group))
stack;
return r;
return ECMD_PROCESSED;
}

View File

@ -1798,12 +1798,16 @@ void destroy_processing_handle(struct cmd_context *cmd, struct processing_handle
log_restore_report_state(cmd->cmd_report.saved_log_report_state);
if (!cmd->is_interactive) {
if (!dm_report_group_destroy(cmd->cmd_report.report_group))
stack;
if (!cmd->is_interactive && cmd->cmd_report.log_rh) {
cmd->cmd_report.report_group = NULL;
if (cmd->cmd_report.log_rh) {
dm_report_free(cmd->cmd_report.log_rh);
cmd->cmd_report.log_rh = NULL;
}
}
/*
* TODO: think about better alternatives:
* handle mempool, dm_alloc for handle memory...