mirror of
git://sourceware.org/git/lvm2.git
synced 2024-12-21 13:34:40 +03:00
refactor: move report grouping and log reporting handles from processing_handle to cmd_context
With patches that will follow, this will make it possible to widen log report coverage when commands are executed from lvm shell so the amount of messages that may end up in stderr/stdout instead of log report are minimized.
This commit is contained in:
parent
ef69934746
commit
1fde4bf4d0
@ -2218,8 +2218,10 @@ void destroy_toolcontext(struct cmd_context *cmd)
|
|||||||
if (cmd->cft_def_hash)
|
if (cmd->cft_def_hash)
|
||||||
dm_hash_destroy(cmd->cft_def_hash);
|
dm_hash_destroy(cmd->cft_def_hash);
|
||||||
|
|
||||||
if (cmd->log_rh)
|
if (cmd->cmd_report.log_rh) {
|
||||||
dm_report_free(cmd->log_rh);
|
dm_report_free(cmd->cmd_report.log_rh);
|
||||||
|
cmd->cmd_report.log_rh = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (cmd->libmem)
|
if (cmd->libmem)
|
||||||
dm_pool_destroy(cmd->libmem);
|
dm_pool_destroy(cmd->libmem);
|
||||||
|
@ -66,6 +66,15 @@ struct cmd_context_initialized_parts {
|
|||||||
unsigned connections:1;
|
unsigned connections:1;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
struct cmd_report {
|
||||||
|
int log_only;
|
||||||
|
dm_report_group_type_t report_group_type;
|
||||||
|
struct dm_report_group *report_group;
|
||||||
|
struct dm_report *log_rh;
|
||||||
|
const char *log_name;
|
||||||
|
log_report_t saved_log_report_state;
|
||||||
|
};
|
||||||
|
|
||||||
/* FIXME Split into tool & library contexts */
|
/* FIXME Split into tool & library contexts */
|
||||||
/* command-instance-related variables needed by library */
|
/* command-instance-related variables needed by library */
|
||||||
struct cmd_context {
|
struct cmd_context {
|
||||||
@ -186,9 +195,9 @@ struct cmd_context {
|
|||||||
char proc_dir[PATH_MAX];
|
char proc_dir[PATH_MAX];
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Command log reporting.
|
* Reporting.
|
||||||
*/
|
*/
|
||||||
struct dm_report *log_rh; /* keep log report of last cmd for further queries if cmd line is interactive (e.g. lvm shell) */
|
struct cmd_report cmd_report;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Buffers.
|
* Buffers.
|
||||||
|
@ -81,9 +81,7 @@ struct processing_handle;
|
|||||||
typedef int (*field_report_fn) (struct report_handle * dh, struct field * field,
|
typedef int (*field_report_fn) (struct report_handle * dh, struct field * field,
|
||||||
const void *data);
|
const void *data);
|
||||||
|
|
||||||
int report_format_init(struct cmd_context *cmd, dm_report_group_type_t *report_group_type,
|
int report_format_init(struct cmd_context *cmd);
|
||||||
struct dm_report_group **report_group, struct dm_report **log_rh,
|
|
||||||
int *log_only, log_report_t *saved_log_report_state);
|
|
||||||
|
|
||||||
void *report_init(struct cmd_context *cmd, const char *format, const char *keys,
|
void *report_init(struct cmd_context *cmd, const char *format, const char *keys,
|
||||||
report_type_t *report_type, const char *separator,
|
report_type_t *report_type, const char *separator,
|
||||||
|
10
tools/lvm.c
10
tools/lvm.c
@ -185,12 +185,12 @@ static int _log_shell_command_status(struct cmd_context *cmd, int ret_code)
|
|||||||
{
|
{
|
||||||
log_report_t log_state;
|
log_report_t log_state;
|
||||||
|
|
||||||
if (!cmd->log_rh)
|
if (!cmd->cmd_report.log_rh)
|
||||||
return 1;
|
return 1;
|
||||||
|
|
||||||
log_state = log_get_report_state();
|
log_state = log_get_report_state();
|
||||||
|
|
||||||
return report_cmdlog(cmd->log_rh, REPORT_OBJECT_CMDLOG_NAME,
|
return report_cmdlog(cmd->cmd_report.log_rh, REPORT_OBJECT_CMDLOG_NAME,
|
||||||
log_get_report_context_name(log_state.context),
|
log_get_report_context_name(log_state.context),
|
||||||
log_get_report_object_type_name(log_state.object_type),
|
log_get_report_object_type_name(log_state.object_type),
|
||||||
log_state.object_name, log_state.object_id,
|
log_state.object_name, log_state.object_id,
|
||||||
@ -263,10 +263,10 @@ int lvm_shell(struct cmd_context *cmd, struct cmdline_context *cmdline)
|
|||||||
|
|
||||||
is_lastlog_cmd = !strcmp(argv[0], "lastlog");
|
is_lastlog_cmd = !strcmp(argv[0], "lastlog");
|
||||||
|
|
||||||
if (cmd->log_rh && !is_lastlog_cmd) {
|
if (cmd->cmd_report.log_rh && !is_lastlog_cmd) {
|
||||||
/* drop old log report */
|
/* drop old log report */
|
||||||
dm_report_free(cmd->log_rh);
|
dm_report_free(cmd->cmd_report.log_rh);
|
||||||
cmd->log_rh = NULL;
|
cmd->cmd_report.log_rh = NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
ret = lvm_run_command(cmd, argc, argv);
|
ret = lvm_run_command(cmd, argc, argv);
|
||||||
|
@ -1055,7 +1055,7 @@ static int _do_report(struct cmd_context *cmd, struct processing_handle *handle,
|
|||||||
goto_out;
|
goto_out;
|
||||||
|
|
||||||
if (!(args->log_only && (single_args->report_type != CMDLOG))) {
|
if (!(args->log_only && (single_args->report_type != CMDLOG))) {
|
||||||
if (!dm_report_group_push(handle->report_group, report_handle, (void *) single_args->report_name))
|
if (!dm_report_group_push(cmd->cmd_report.report_group, report_handle, (void *) single_args->report_name))
|
||||||
goto_out;
|
goto_out;
|
||||||
report_in_group = 1;
|
report_in_group = 1;
|
||||||
}
|
}
|
||||||
@ -1174,7 +1174,7 @@ static int _do_report(struct cmd_context *cmd, struct processing_handle *handle,
|
|||||||
unlock_vg(cmd, NULL, VG_GLOBAL);
|
unlock_vg(cmd, NULL, VG_GLOBAL);
|
||||||
out:
|
out:
|
||||||
if (report_handle) {
|
if (report_handle) {
|
||||||
if (report_in_group && !dm_report_group_pop(handle->report_group))
|
if (report_in_group && !dm_report_group_pop(cmd->cmd_report.report_group))
|
||||||
stack;
|
stack;
|
||||||
dm_report_free(report_handle);
|
dm_report_free(report_handle);
|
||||||
}
|
}
|
||||||
@ -1197,7 +1197,7 @@ static int _full_report_single(struct cmd_context *cmd,
|
|||||||
|
|
||||||
args->full_report_vg = vg;
|
args->full_report_vg = vg;
|
||||||
|
|
||||||
if (!args->log_only && !dm_report_group_push(handle->report_group, NULL, NULL))
|
if (!args->log_only && !dm_report_group_push(cmd->cmd_report.report_group, NULL, NULL))
|
||||||
goto out;
|
goto out;
|
||||||
|
|
||||||
if (orphan) {
|
if (orphan) {
|
||||||
@ -1213,7 +1213,7 @@ static int _full_report_single(struct cmd_context *cmd,
|
|||||||
stack;
|
stack;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!args->log_only && !dm_report_group_pop(handle->report_group))
|
if (!args->log_only && !dm_report_group_pop(cmd->cmd_report.report_group))
|
||||||
goto_out;
|
goto_out;
|
||||||
out:
|
out:
|
||||||
args->full_report_vg = NULL;
|
args->full_report_vg = NULL;
|
||||||
@ -1367,15 +1367,15 @@ static int _report(struct cmd_context *cmd, int argc, char **argv, report_type_t
|
|||||||
handle->internal_report_for_select = 0;
|
handle->internal_report_for_select = 0;
|
||||||
handle->include_historical_lvs = cmd->include_historical_lvs;
|
handle->include_historical_lvs = cmd->include_historical_lvs;
|
||||||
|
|
||||||
args.report_group_type = handle->report_group_type;
|
args.report_group_type = cmd->cmd_report.report_group_type;
|
||||||
args.log_only = handle->log_only;
|
args.log_only = cmd->cmd_report.log_only;
|
||||||
|
|
||||||
if (!_config_report(cmd, &args, single_args)) {
|
if (!_config_report(cmd, &args, single_args)) {
|
||||||
destroy_processing_handle(cmd, handle);
|
destroy_processing_handle(cmd, handle);
|
||||||
return_ECMD_FAILED;
|
return_ECMD_FAILED;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!args.log_only && !dm_report_group_push(handle->report_group, NULL, report_name)) {
|
if (!args.log_only && !dm_report_group_push(cmd->cmd_report.report_group, NULL, report_name)) {
|
||||||
log_error("Failed to add main report section to report group.");
|
log_error("Failed to add main report section to report group.");
|
||||||
destroy_processing_handle(cmd, handle);
|
destroy_processing_handle(cmd, handle);
|
||||||
return ECMD_FAILED;
|
return ECMD_FAILED;
|
||||||
@ -1433,9 +1433,7 @@ int devtypes(struct cmd_context *cmd, int argc, char **argv)
|
|||||||
#define REPORT_FORMAT_NAME_BASIC "basic"
|
#define REPORT_FORMAT_NAME_BASIC "basic"
|
||||||
#define REPORT_FORMAT_NAME_JSON "json"
|
#define REPORT_FORMAT_NAME_JSON "json"
|
||||||
|
|
||||||
int report_format_init(struct cmd_context *cmd, dm_report_group_type_t *report_group_type,
|
int report_format_init(struct cmd_context *cmd)
|
||||||
struct dm_report_group **report_group, struct dm_report **log_rh,
|
|
||||||
int *log_only, log_report_t *saved_log_report_state)
|
|
||||||
{
|
{
|
||||||
int config_set = find_config_tree_node(cmd, report_output_format_CFG, NULL) != NULL;
|
int config_set = find_config_tree_node(cmd, report_output_format_CFG, NULL) != NULL;
|
||||||
const char *config_format_str = find_config_tree_str(cmd, report_output_format_CFG, NULL);
|
const char *config_format_str = find_config_tree_str(cmd, report_output_format_CFG, NULL);
|
||||||
@ -1446,7 +1444,7 @@ int report_format_init(struct cmd_context *cmd, dm_report_group_type_t *report_g
|
|||||||
struct dm_report_group *new_report_group;
|
struct dm_report_group *new_report_group;
|
||||||
struct dm_report *tmp_log_rh = NULL;
|
struct dm_report *tmp_log_rh = NULL;
|
||||||
|
|
||||||
args.log_only = arg_is_set(cmd, logonly_ARG) || *log_rh;
|
args.log_only = arg_is_set(cmd, logonly_ARG);
|
||||||
report_command_log = args.log_only || find_config_tree_bool(cmd, log_report_command_log_CFG, NULL);
|
report_command_log = args.log_only || find_config_tree_bool(cmd, log_report_command_log_CFG, NULL);
|
||||||
|
|
||||||
if (!format_str || !strcmp(format_str, REPORT_FORMAT_NAME_BASIC)) {
|
if (!format_str || !strcmp(format_str, REPORT_FORMAT_NAME_BASIC)) {
|
||||||
@ -1462,10 +1460,8 @@ int report_format_init(struct cmd_context *cmd, dm_report_group_type_t *report_g
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (report_group_type)
|
cmd->cmd_report.report_group_type = args.report_group_type;
|
||||||
*report_group_type = args.report_group_type;
|
cmd->cmd_report.log_only = args.log_only;
|
||||||
if (log_only)
|
|
||||||
*log_only = args.log_only;
|
|
||||||
|
|
||||||
if (!(new_report_group = dm_report_group_create(args.report_group_type, NULL))) {
|
if (!(new_report_group = dm_report_group_create(args.report_group_type, NULL))) {
|
||||||
log_error("Failed to create report group.");
|
log_error("Failed to create report group.");
|
||||||
@ -1476,7 +1472,6 @@ int report_format_init(struct cmd_context *cmd, dm_report_group_type_t *report_g
|
|||||||
single_args = &args.single_args[REPORT_IDX_LOG];
|
single_args = &args.single_args[REPORT_IDX_LOG];
|
||||||
single_args->report_type = CMDLOG;
|
single_args->report_type = CMDLOG;
|
||||||
|
|
||||||
if (!*log_rh) {
|
|
||||||
if (!_config_report(cmd, &args, single_args))
|
if (!_config_report(cmd, &args, single_args))
|
||||||
goto_bad;
|
goto_bad;
|
||||||
|
|
||||||
@ -1487,29 +1482,23 @@ int report_format_init(struct cmd_context *cmd, dm_report_group_type_t *report_g
|
|||||||
log_error("Failed to create log report.");
|
log_error("Failed to create log report.");
|
||||||
goto bad;
|
goto bad;
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
/*
|
if (!(dm_report_group_push(new_report_group, tmp_log_rh, (void *) single_args->report_name))) {
|
||||||
* We reusing existing log report handle.
|
log_error("Failed to add log report to report group.");
|
||||||
* Just get report's name and prefix now.
|
goto bad;
|
||||||
*/
|
|
||||||
if (!_set_report_prefix_and_name(&args, single_args))
|
|
||||||
goto_bad;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!(dm_report_group_push(new_report_group, tmp_log_rh ? : *log_rh, (void *) single_args->report_name))) {
|
cmd->cmd_report.log_rh = tmp_log_rh;
|
||||||
log_error("Failed to add log report to report group.");
|
if (!(cmd->cmd_report.log_name = dm_pool_strdup(cmd->libmem, single_args->report_name))) {
|
||||||
|
log_error("Failed to set log report name for command context.");
|
||||||
goto bad;
|
goto bad;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*report_group = new_report_group;
|
cmd->cmd_report.report_group = new_report_group;
|
||||||
if (tmp_log_rh)
|
cmd->cmd_report.saved_log_report_state = log_get_report_state();
|
||||||
*log_rh = tmp_log_rh;
|
log_set_report(cmd->cmd_report.log_rh);
|
||||||
|
|
||||||
if (saved_log_report_state) {
|
|
||||||
*saved_log_report_state = log_get_report_state();
|
|
||||||
log_set_report(*log_rh);
|
|
||||||
}
|
|
||||||
return 1;
|
return 1;
|
||||||
bad:
|
bad:
|
||||||
if (!dm_report_group_destroy(new_report_group))
|
if (!dm_report_group_destroy(new_report_group))
|
||||||
@ -1525,23 +1514,20 @@ int lastlog(struct cmd_context *cmd, int argc, char **argv)
|
|||||||
const char *selection;
|
const char *selection;
|
||||||
int r = ECMD_FAILED;
|
int r = ECMD_FAILED;
|
||||||
|
|
||||||
if (!cmd->log_rh) {
|
if (!cmd->cmd_report.log_rh) {
|
||||||
log_error("No log report stored.");
|
log_error("No log report stored.");
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!report_format_init(cmd, NULL, &report_group, &cmd->log_rh, NULL, NULL))
|
|
||||||
goto_out;
|
|
||||||
|
|
||||||
if (!_do_report_get_selection(cmd, CMDLOG, 1, NULL, &selection))
|
if (!_do_report_get_selection(cmd, CMDLOG, 1, NULL, &selection))
|
||||||
goto_out;
|
goto_out;
|
||||||
|
|
||||||
if (!dm_report_set_selection(cmd->log_rh, selection)) {
|
if (!dm_report_set_selection(cmd->cmd_report.log_rh, selection)) {
|
||||||
log_error("Failed to set selection for log report.");
|
log_error("Failed to set selection for log report.");
|
||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!dm_report_output(cmd->log_rh) ||
|
if (!dm_report_output(cmd->cmd_report.log_rh) ||
|
||||||
!dm_report_group_pop(report_group))
|
!dm_report_group_pop(report_group))
|
||||||
goto_out;
|
goto_out;
|
||||||
|
|
||||||
|
@ -1754,15 +1754,13 @@ struct processing_handle *init_processing_handle(struct cmd_context *cmd, struct
|
|||||||
handle->internal_report_for_select = arg_is_set(cmd, select_ARG);
|
handle->internal_report_for_select = arg_is_set(cmd, select_ARG);
|
||||||
handle->include_historical_lvs = cmd->include_historical_lvs;
|
handle->include_historical_lvs = cmd->include_historical_lvs;
|
||||||
|
|
||||||
if (!parent_handle) {
|
if (!parent_handle && !cmd->cmd_report.report_group) {
|
||||||
if (!report_format_init(cmd, &handle->report_group_type, &handle->report_group,
|
if (!report_format_init(cmd)) {
|
||||||
&handle->log_rh, &handle->log_only,
|
|
||||||
&handle->saved_log_report_state)) {
|
|
||||||
dm_pool_free(cmd->mem, handle);
|
dm_pool_free(cmd->mem, handle);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
} else
|
} else
|
||||||
handle->saved_log_report_state = log_get_report_state();
|
cmd->cmd_report.saved_log_report_state = log_get_report_state();
|
||||||
|
|
||||||
log_set_report_context(LOG_REPORT_CONTEXT_PROCESSING);
|
log_set_report_context(LOG_REPORT_CONTEXT_PROCESSING);
|
||||||
return handle;
|
return handle;
|
||||||
@ -1798,19 +1796,13 @@ void destroy_processing_handle(struct cmd_context *cmd, struct processing_handle
|
|||||||
if (handle->selection_handle && handle->selection_handle->selection_rh)
|
if (handle->selection_handle && handle->selection_handle->selection_rh)
|
||||||
dm_report_free(handle->selection_handle->selection_rh);
|
dm_report_free(handle->selection_handle->selection_rh);
|
||||||
|
|
||||||
log_restore_report_state(handle->saved_log_report_state);
|
log_restore_report_state(cmd->cmd_report.saved_log_report_state);
|
||||||
|
|
||||||
if (!dm_report_group_destroy(handle->report_group))
|
if (!dm_report_group_destroy(cmd->cmd_report.report_group))
|
||||||
stack;
|
stack;
|
||||||
if (handle->log_rh) {
|
if (!cmd->is_interactive && cmd->cmd_report.log_rh) {
|
||||||
if (cmd->is_interactive) {
|
dm_report_free(cmd->cmd_report.log_rh);
|
||||||
/*
|
cmd->cmd_report.log_rh = NULL;
|
||||||
* Keep log report if we're interactive so
|
|
||||||
* we can do further queries on this report.
|
|
||||||
*/
|
|
||||||
cmd->log_rh = handle->log_rh;
|
|
||||||
} else
|
|
||||||
dm_report_free(handle->log_rh);
|
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* TODO: think about better alternatives:
|
* TODO: think about better alternatives:
|
||||||
|
@ -73,11 +73,6 @@ struct processing_handle {
|
|||||||
int internal_report_for_select;
|
int internal_report_for_select;
|
||||||
int include_historical_lvs;
|
int include_historical_lvs;
|
||||||
struct selection_handle *selection_handle;
|
struct selection_handle *selection_handle;
|
||||||
dm_report_group_type_t report_group_type;
|
|
||||||
struct dm_report_group *report_group;
|
|
||||||
struct dm_report *log_rh;
|
|
||||||
int log_only;
|
|
||||||
log_report_t saved_log_report_state;
|
|
||||||
void *custom_handle;
|
void *custom_handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user