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

toollib: add report_format_init fn to create report group and to create/add log report handle

Add new --reportformat option and new report_format_init function that
checks this option and creates new report group accordingly, also
preparing log report handle and adding it to the report group just
created.
This commit is contained in:
Peter Rajnoha 2016-05-02 14:22:02 +02:00
parent c36d4632a6
commit 79a74e9aae
4 changed files with 71 additions and 0 deletions

View File

@ -1,5 +1,6 @@
Version 2.02.158 - Version 2.02.158 -
================================= =================================
Recognize --reportformat {basic|json} option to select report output format.
Add log/command_log_{sort,cols} to lvm.conf to configure command log report. Add log/command_log_{sort,cols} to lvm.conf to configure command log report.
Add log_object_{type,name,id,group,group_id} fields to cmd log. Add log_object_{type,name,id,group,group_id} fields to cmd log.
Add log_{seq_num,type,context,message,errno,ret_code} fields to cmd log. Add log_{seq_num,type,context,message,errno,ret_code} fields to cmd log.

View File

@ -80,6 +80,9 @@ 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,
struct dm_report_group **report_group, struct dm_report **log_rh);
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,
int aligned, int buffered, int headings, int field_prefixes, int aligned, int buffered, int headings, int field_prefixes,

View File

@ -96,6 +96,7 @@ arg(refresh_ARG, '\0', "refresh", NULL, 0)
arg(removemissing_ARG, '\0', "removemissing", NULL, 0) arg(removemissing_ARG, '\0', "removemissing", NULL, 0)
arg(repair_ARG, '\0', "repair", NULL, 0) arg(repair_ARG, '\0', "repair", NULL, 0)
arg(replace_ARG, '\0', "replace", string_arg, ARG_GROUPABLE) arg(replace_ARG, '\0', "replace", string_arg, ARG_GROUPABLE)
arg(reportformat_ARG, '\0', "reportformat", string_arg, 0)
arg(restorefile_ARG, '\0', "restorefile", string_arg, 0) arg(restorefile_ARG, '\0', "restorefile", string_arg, 0)
arg(restoremissing_ARG, '\0', "restoremissing", NULL, 0) arg(restoremissing_ARG, '\0', "restoremissing", NULL, 0)
arg(resync_ARG, '\0', "resync", NULL, 0) arg(resync_ARG, '\0', "resync", NULL, 0)

View File

@ -20,6 +20,7 @@
struct report_args { struct report_args {
int argc; int argc;
char **argv; char **argv;
dm_report_group_type_t report_group_type;
report_type_t report_type; report_type_t report_type;
int args_are_pvs; int args_are_pvs;
int aligned; int aligned;
@ -1061,3 +1062,68 @@ int devtypes(struct cmd_context *cmd, int argc, char **argv)
{ {
return _report(cmd, argc, argv, DEVTYPES); return _report(cmd, argc, argv, DEVTYPES);
} }
#define REPORT_FORMAT_NAME_BASIC "basic"
#define REPORT_FORMAT_NAME_JSON "json"
int report_format_init(struct cmd_context *cmd, dm_report_group_type_t *report_group_type,
struct dm_report_group **report_group, struct dm_report **log_rh)
{
static char log_report_name[] = "log";
const char *format_str = arg_str_value(cmd, reportformat_ARG, NULL);
struct report_args args = {0};
struct dm_report_group *new_report_group;
struct dm_report *tmp_log_rh = NULL;
if (!format_str) {
args.report_group_type = DM_REPORT_GROUP_SINGLE;
} else if (!strcmp(format_str, REPORT_FORMAT_NAME_BASIC)) {
args.report_group_type = DM_REPORT_GROUP_BASIC;
} else if (!strcmp(format_str, REPORT_FORMAT_NAME_JSON)) {
args.report_group_type = DM_REPORT_GROUP_JSON;
} else {
log_error("%s: unknown report format.", format_str);
log_error("Supported report formats: %s, %s.",
REPORT_FORMAT_NAME_BASIC,
REPORT_FORMAT_NAME_JSON);
return 0;
}
if (report_group_type)
*report_group_type = args.report_group_type;
if (!(new_report_group = dm_report_group_create(args.report_group_type, NULL))) {
log_error("Failed to create report group.");
return 0;
}
if (!*log_rh) {
args.report_type = CMDLOG;
if (!_config_report(cmd, &args))
goto_bad;
if (!(tmp_log_rh = report_init(NULL, args.options, args.keys, &args.report_type,
args.separator, args.aligned, args.buffered, args.headings,
args.field_prefixes, args.quoted, args.columns_as_rows,
args.selection))) {
log_error("Failed to create log report.");
goto bad;
}
}
if (!(dm_report_group_push(new_report_group, tmp_log_rh ? : *log_rh, log_report_name))) {
log_error("Failed to add log report to report group.");
goto bad;
}
*report_group = new_report_group;
if (tmp_log_rh)
*log_rh = tmp_log_rh;
return 1;
bad:
if (!dm_report_group_destroy(new_report_group))
stack;
if (tmp_log_rh)
dm_report_free(tmp_log_rh);
return 0;
}