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
This commit is contained in:
Harald Welte 2017-03-16 23:54:55 +01:00
parent 9ab00721e5
commit 18a7d81932
1 changed files with 21 additions and 0 deletions

View File

@ -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) */