[PATCH] libosmocore[master]: logging: fail gracefully if log_info() was not called

This is merely a historical archive of years 2008-2021, before the migration to mailman3.

A maintained and still updated list archive can be found at https://lists.osmocom.org/hyperkitty/list/gerrit-log@lists.osmocom.org/.

Harald Welte gerrit-no-reply at lists.osmocom.org
Thu Mar 16 22:59:12 UTC 2017


Review at  https://gerrit.osmocom.org/2109

logging: fail gracefully if log_info() was not called

The logging code crashes if osmo_log_info is not set, which is typically
achieved by calling log_init().  Let's fail with a reasonable assert
and error message if the user forgets that.

Change-Id: If3007860d2efe6ea9aec27e7d7439d44a7cd19c2
---
M src/logging.c
1 file changed, 21 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/09/2109/1

diff --git a/src/logging.c b/src/logging.c
index 6a1e929..d900340 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -154,6 +154,15 @@
 	NULL,
 };
 
+static void assert_loginfo(void)
+{
+	if (!osmo_log_info) {
+		fprintf(stderr, "ERROR: osmo_log_info == NULL! "
+			"You must call log_init() before using logging!\n");
+		OSMO_ASSERT(osmo_log_info);
+	}
+}
+
 /* special magic for negative (library-internal) log subsystem numbers */
 static int subsys_lib2index(int subsys)
 {
@@ -186,6 +195,8 @@
 {
 	int i;
 
+	assert_loginfo();
+
 	for (i = 0; i < osmo_log_info->num_cat; ++i) {
 		if (osmo_log_info->cat[i].name == NULL)
 			continue;
@@ -208,6 +219,8 @@
 	int i = 0;
 	char *mask = strdup(_mask);
 	char *category_token = NULL;
+
+	assert_loginfo();
 
 	/* Disable everything to enable it afterwards */
 	for (i = 0; i < osmo_log_info->num_cat; ++i)
@@ -611,6 +624,8 @@
 	struct log_target *target;
 	unsigned int i;
 
+	assert_loginfo();
+
 	target = talloc_zero(tall_log_ctx, struct log_target);
 	if (!target)
 		return NULL;
@@ -783,6 +798,8 @@
 	int size = strlen("logging level () ()") + 1;
 	char *str;
 
+	assert_loginfo();
+
 	for (i = 0; i < info->num_cat; i++) {
 		if (info->cat[i].name == NULL)
 			continue;
@@ -862,6 +879,8 @@
 	unsigned int size =
 		strlen(LOGGING_STR
 		       "Set the log level for a specified category\n") + 1;
+
+	assert_loginfo();
 
 	for (i = 0; i < info->num_cat; i++) {
 		if (info->cat[i].name == NULL)
@@ -980,6 +999,8 @@
 {
 	struct log_target *tar;
 
+	assert_loginfo();
+
 	subsys = map_subsys(subsys);
 
 	/* TODO: The following could/should be cached (update on config) */

-- 
To view, visit https://gerrit.osmocom.org/2109
To unsubscribe, visit https://gerrit.osmocom.org/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: If3007860d2efe6ea9aec27e7d7439d44a7cd19c2
Gerrit-PatchSet: 1
Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Owner: Harald Welte <laforge at gnumonks.org>



More information about the gerrit-log mailing list