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
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;
@@ -209,6 +220,8 @@
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)
target->categories[i].enabled = 0;
@@ -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;
@@ -863,6 +880,8 @@
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)
continue;
@@ -980,6 +999,8 @@
{
struct log_target *tar;
+ assert_loginfo();
+
subsys = map_subsys(subsys);
/* TODO: The following could/should be cached (update on config) */