aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorHarald Welte <laforge@gnumonks.org>2017-03-16 23:54:55 +0100
committerHarald Welte <laforge@gnumonks.org>2017-03-16 23:56:01 +0100
commit18a7d819328bed66308289e89c3af99cbca9125b (patch)
treee047524b2cf1918adf6d2a98267ff5a15925b792
parent9ab00721e57a441a5ac26a637621cefa15dc2a73 (diff)
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
-rw-r--r--src/logging.c21
1 files changed, 21 insertions, 0 deletions
diff --git a/src/logging.c b/src/logging.c
index 6a1e9299..d900340b 100644
--- a/src/logging.c
+++ b/src/logging.c
@@ -154,6 +154,15 @@ const char *loglevel_descriptions[LOGLEVEL_DEFS+1] = {
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 log_parse_category(const char *category)
{
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 @@ void log_parse_category_mask(struct log_target* target, const char *_mask)
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 *log_target_create(void)
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 @@ const char *log_vty_command_string(const struct log_info *unused_info)
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 @@ const char *log_vty_command_description(const struct log_info *unused_info)
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 @@ int log_check_level(int subsys, unsigned int level)
{
struct log_target *tar;
+ assert_loginfo();
+
subsys = map_subsys(subsys);
/* TODO: The following could/should be cached (update on config) */