Skip to contentSkip to conversation

master - log: log warnings and errors via report if set; add log_set_report* fns

PATCH Peter Rajnoha 1 participant 1 message Jun 20, 2016

lore
Share & tools
lore
#0 lore raw
Gitweb:        http://git.fedorahosted.org/git/?p=lvm2.git;a=commitdiff;h=7d4a15e53a19dcc073b7037638d811eba3a85edf
Commit:        7d4a15e53a19dcc073b7037638d811eba3a85edf
Parent:        8cfc385491484e71d66e5991e09c3d0a47570a13
Author:        Peter Rajnoha <[email protected]>
AuthorDate:    Tue May 3 11:46:52 2016 +0200
Committer:     Peter Rajnoha <[email protected]>
CommitterDate: Mon Jun 20 11:33:42 2016 +0200

log: log warnings and errors via report if set; add log_set_report* fns

This patch adds structures and functions to reroute error and warning logs to log report, if it's set.

There are 5 new functions:
- log_set_report
Set log report where logging will be rerouted.

- log_set_report_context
Set context globally so any report_cmdlog call will use it.

- log_set_report_object_type
Set object type globally so any report_cmdlog call will use it.

- log_set_report_object_name_and_id
Set object ID and name globally so any report_cmdlog call will use it.

- log_set_report_object_group_and_group_id
Set object group ID and name globally so any report_cmdlog call will use it.

These functions will be called during LVM command processing so any logs which are rerouted to log report contain proper information about current processing state.

---
 WHATS_NEW             |    1 +
 lib/log/log.c         |  101 ++++++++++++++++++++++++++++++++++++++++++++++++-
 lib/log/lvm-logging.h |   37 ++++++++++++++++++
 lib/report/report.h   |    3 +-
 tools/reporter.c      |    8 +++-
 tools/toollib.c       |    8 +++-
 tools/toollib.h       |    1 +
 7 files changed, 154 insertions(+), 5 deletions(-)
WHATS_NEW +1 -0
index edbc0f3..88e785a 100644
@@ -1,5 +1,6 @@
 Version 2.02.158 - 
 =================================
+  Log warnings and errors via report during cmd processing if this is enabled.
   Make it possible to iterate over internal 'orphan' VGs in process_each_vg fn.
   Make -S|--select option groupable that allows this option to be repeated.
   Make -O|--sort option groupable that allows this option to be repeated.
lib/log/log.c +100 -1

146 lines not on this page · raw message

lib/log/lvm-logging.h +37 -0
index 56e95ff..5789c0b 100644
@@ -65,4 +65,41 @@ int log_suppress(int suppress);
 /* Suppress messages to syslog */
 void syslog_suppress(int suppress);
 
+/* Hooks to handle logging through report. */
+typedef enum {
+	LOG_REPORT_CONTEXT_NULL,
+	LOG_REPORT_CONTEXT_PROCESSING,
+	LOG_REPORT_CONTEXT_COUNT
+} log_report_context_t;
+
+typedef enum {
+	LOG_REPORT_OBJECT_TYPE_NULL,
+	LOG_REPORT_OBJECT_TYPE_PV,
+	LOG_REPORT_OBJECT_TYPE_VG,
+	LOG_REPORT_OBJECT_TYPE_LV,
+	LOG_REPORT_OBJECT_TYPE_COUNT
+} log_report_object_type_t;
+
+typedef struct log_report {
+	struct dm_report *report;
+	log_report_context_t context;
+	log_report_object_type_t object_type;
+	const char *object_name;
+	const char *object_id;
+	const char *object_group;
+	const char *object_group_id;
+} log_report_t;
+
+log_report_t log_get_report_state(void);
+void log_restore_report_state(log_report_t log_report);
+
+void log_set_report(struct dm_report *report);
+void log_set_report_context(log_report_context_t context);
+void log_set_report_object_type(log_report_object_type_t object_type);
+void log_set_report_object_group_and_group_id(const char *group, const char *group_id);
+void log_set_report_object_name_and_id(const char *name, const char *id);
+
+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);
+
 #endif
lib/report/report.h +2 -1
index f5df51e..37e1061 100644
@@ -82,7 +82,8 @@ typedef int (*field_report_fn) (struct report_handle * dh, struct field * field,
 				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);
+		       struct dm_report_group **report_group, struct dm_report **log_rh,
+		       log_report_t *saved_log_report_state);
 
 void *report_init(struct cmd_context *cmd, const char *format, const char *keys,
 		  report_type_t *report_type, const char *separator,
tools/reporter.c +7 -1
index 0509c88..d376947 100644
@@ -1418,7 +1418,8 @@ int devtypes(struct cmd_context *cmd, int argc, char **argv)
 #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)
+		       struct dm_report_group **report_group, struct dm_report **log_rh,
+		       log_report_t *saved_log_report_state)
 {
 	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);
@@ -1483,6 +1484,11 @@ int report_format_init(struct cmd_context *cmd, dm_report_group_type_t *report_g
 	*report_group = new_report_group;
 	if (tmp_log_rh)
 		*log_rh = tmp_log_rh;
+
+	if (saved_log_report_state) {
+		*saved_log_report_state = log_get_report_state();
+		log_set_report(*log_rh);
+	}
 	return 1;
 bad:
 	if (!dm_report_group_destroy(new_report_group))
tools/toollib.c +6 -2
index 925157d..fe162d1 100644
@@ -1729,11 +1729,12 @@ struct processing_handle *init_processing_handle(struct cmd_context *cmd, struct
 
 	if (!parent_handle) {
 		if (!report_format_init(cmd, &handle->report_group_type, &handle->report_group,
-					&handle->log_rh)) {
+					&handle->log_rh, &handle->saved_log_report_state)) {
 			dm_pool_free(cmd->mem, handle);
 			return NULL;
 		}
-	}
+	} else
+		handle->saved_log_report_state = log_get_report_state();
 
 	return handle;
 }
@@ -1767,6 +1768,9 @@ void destroy_processing_handle(struct cmd_context *cmd, struct processing_handle
 	if (handle) {
 		if (handle->selection_handle && handle->selection_handle->selection_rh)
 			dm_report_free(handle->selection_handle->selection_rh);
+
+		log_restore_report_state(handle->saved_log_report_state);
+
 		if (!dm_report_group_destroy(handle->report_group))
 			stack;
 		if (handle->log_rh)
tools/toollib.h +1 -0
index 179efe5..1144793 100644
@@ -76,6 +76,7 @@ struct processing_handle {
 	dm_report_group_type_t report_group_type;
 	struct dm_report_group *report_group;
 	struct dm_report *log_rh;
+	log_report_t saved_log_report_state;
 	void *custom_handle;
 };