logging: use LOGL_NOTICE when no loglevel is set

when the API user of libosmocores logging infrastructure does not set a
pre-defined logging level in struct log_info_cat, the result would be an
invalid logging level. In order to avoid problems with that and to spare
all the additional .loglevel = LOGL_NOTICE (API users are advised to use
LOGL_NOTICE as default) lines in the user code lets check the logging
level on startup and set LOGL_NOTICE if there is no logging level set.

Change-Id: Ib9e180261505062505fc4605a98023910f76cde6
Related: OS#2577
This commit is contained in:
Philipp Maier 2020-03-25 11:25:59 +01:00
parent 555bb1b276
commit dcad1c55ba
1 changed files with 17 additions and 9 deletions

View File

@ -1026,6 +1026,7 @@ int log_targets_reopen(void)
int log_init(const struct log_info *inf, void *ctx) int log_init(const struct log_info *inf, void *ctx)
{ {
int i; int i;
struct log_info_cat *cat_ptr;
tall_log_ctx = talloc_named_const(ctx, 1, "logging"); tall_log_ctx = talloc_named_const(ctx, 1, "logging");
if (!tall_log_ctx) if (!tall_log_ctx)
@ -1043,29 +1044,36 @@ int log_init(const struct log_info *inf, void *ctx)
osmo_log_info->num_cat += inf->num_cat; osmo_log_info->num_cat += inf->num_cat;
} }
osmo_log_info->cat = talloc_zero_array(osmo_log_info, cat_ptr = talloc_zero_array(osmo_log_info, struct log_info_cat,
struct log_info_cat, osmo_log_info->num_cat);
osmo_log_info->num_cat); if (!cat_ptr) {
if (!osmo_log_info->cat) {
talloc_free(osmo_log_info); talloc_free(osmo_log_info);
osmo_log_info = NULL; osmo_log_info = NULL;
return -ENOMEM; return -ENOMEM;
} }
if (inf) { /* copy over the user part */ /* copy over the user part and sanitize loglevel */
if (inf) {
for (i = 0; i < inf->num_cat; i++) { for (i = 0; i < inf->num_cat; i++) {
memcpy((struct log_info_cat *) &osmo_log_info->cat[i], memcpy(&cat_ptr[i], &inf->cat[i],
&inf->cat[i], sizeof(struct log_info_cat)); sizeof(struct log_info_cat));
/* Make sure that the loglevel is set to NOTICE in case
* no loglevel has been preset. */
if (!cat_ptr[i].loglevel) {
cat_ptr[i].loglevel = LOGL_NOTICE;
}
} }
} }
/* copy over the library part */ /* copy over the library part */
for (i = 0; i < ARRAY_SIZE(internal_cat); i++) { for (i = 0; i < ARRAY_SIZE(internal_cat); i++) {
unsigned int cn = osmo_log_info->num_cat_user + i; unsigned int cn = osmo_log_info->num_cat_user + i;
memcpy((struct log_info_cat *) &osmo_log_info->cat[cn], memcpy(&cat_ptr[cn], &internal_cat[i], sizeof(struct log_info_cat));
&internal_cat[i], sizeof(struct log_info_cat));
} }
osmo_log_info->cat = cat_ptr;
return 0; return 0;
} }