pespin has uploaded this change for review. ( https://gerrit.osmocom.org/c/libosmocore/+/41879?usp=email )
Change subject: logging: Add APIs to set/get log_{target,context} fields ......................................................................
logging: Add APIs to set/get log_{target,context} fields
Change-Id: Ie48e7e635feb91509b9c034394df4fb16cb931a3 --- M TODO-RELEASE M include/osmocom/core/logging.h M src/core/libosmocore.map M src/core/logging.c 4 files changed, 115 insertions(+), 5 deletions(-)
git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/79/41879/1
diff --git a/TODO-RELEASE b/TODO-RELEASE index 3c98f06..b7d1585 100644 --- a/TODO-RELEASE +++ b/TODO-RELEASE @@ -10,3 +10,4 @@ core added osmo_sock_set_nonblock(), osmo_sock_get_nonblock() core added gsmtap_source_set_nonblock(), gsmtap_source_using_wq() core added osmo_iofd_set_name_f() +core added log_{get,set}_filter(_data)(), log_get_context() diff --git a/include/osmocom/core/logging.h b/include/osmocom/core/logging.h index 44263fb..286f646 100644 --- a/include/osmocom/core/logging.h +++ b/include/osmocom/core/logging.h @@ -225,7 +225,7 @@
/*! Log context information, passed to filter */ struct log_context { - void *ctx[LOG_MAX_CTX+1]; + void *ctx[LOG_MAX_CTX+1] OSMO_DEPRECATED_OUTSIDE("Accessing struct log_context members directly is deprecated"); };
/*! Compatibility with older libosmocore versions */ @@ -298,13 +298,15 @@ };
/*! structure representing a logging target */ +#define OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE_LOG_TARGET \ + OSMO_DEPRECATED_OUTSIDE("Accessing struct log_target members directly is deprecated") struct log_target { struct llist_head entry; /*!< linked list */
/*! Internal data for filtering */ - int filter_map; + int filter_map OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE_LOG_TARGET; /*! Internal data for filtering */ - void *filter_data[LOG_MAX_FILTERS+1]; + void *filter_data[LOG_MAX_FILTERS+1] OSMO_DEPRECATED_OUTSIDE_LIBOSMOCORE_LOG_TARGET;
/*! logging categories */ struct log_category *categories; @@ -406,10 +408,16 @@
/* context management */ void log_reset_context(void); -int log_set_context(uint8_t ctx, void *value); +int log_set_context(uint8_t ctx_nr, void *value); +void *log_get_context(const struct log_context *ctx, uint8_t ctx_nr);
/* filter on the targets */ void log_set_all_filter(struct log_target *target, int); +bool log_get_filter(const struct log_target *target, int log_filter_index); +int log_set_filter(struct log_target *target, int log_filter_index, bool enable); +void *log_get_filter_data(const struct log_target *target, int log_filter_index); +int log_set_filter_data(struct log_target *target, int log_filter_index, void *data); + int log_cache_enable(void); void log_cache_update(int mapped_subsys, uint8_t enabled, uint8_t level); void log_set_use_color(struct log_target *target, int); diff --git a/src/core/libosmocore.map b/src/core/libosmocore.map index 2818f8a..3ec322c 100644 --- a/src/core/libosmocore.map +++ b/src/core/libosmocore.map @@ -75,8 +75,13 @@ log_parse_level; logp_stub; log_reset_context; +log_get_context; +log_get_filter; +log_get_filter_data; log_set_all_filter; log_set_category_filter; +log_set_filter; +log_set_filter_data; log_set_context; log_set_log_level; log_set_print_category; diff --git a/src/core/logging.c b/src/core/logging.c index 9e5ae9f..ac10d00 100644 --- a/src/core/logging.c +++ b/src/core/logging.c @@ -886,7 +886,42 @@ return 0; }
-/*! Enable the \ref LOG_FLT_ALL log filter +/*! Get the logging context + * \param[in] ctx logging context + * \param[in] ctx_nr logging context number + * \returns value set for the context + * + * A logging context is something like the subscriber identity to which + * the currently processed message relates, or the BTS through which it + * was received. The main use of this API is during + * (struct log_info)->filter_fn(ctx, log_tgt). + */ +void *log_get_context(const struct log_context *ctx, uint8_t ctx_nr) +{ + if (ctx_nr > LOG_MAX_CTX) + return NULL; + return ctx->ctx[ctx_nr]; +} + +/*! Query whether a log filter is enabled or disabled + * \param[in] target Log target to be queried + * \param[in] log_filter_index the index of the log filter to query + * \returns whether the filtering is enabled (true) or disabled (false) + */ +bool log_get_filter(const struct log_target *target, int log_filter_index) +{ + /* TODO: in the future we can support app-specified log filters here, + * similar to how we do dynamic cateogries defined by apps. */ + if (log_filter_index < 0) + return false; + + if (log_filter_index >= LOG_MAX_FILTERS) + return false; + + return target->filter_map & (1 << log_filter_index); +} + +/*! Enable/disable the \ref LOG_FLT_ALL log filter * \param[in] target Log target to be affected * \param[in] all enable (1) or disable (0) the ALL filter * @@ -902,6 +937,67 @@ target->filter_map &= ~(1 << LOG_FLT_ALL); }
+/*! Enable/disable a log filter + * \param[in] target Log target to be affected + * \param[in] log_filter_index the index of the log filter to set + * \param[in] enable enable (true) or disable (false) the ALL filter + * \returns 0 on success, negative on error. + */ +int log_set_filter(struct log_target *target, int log_filter_index, bool enable) +{ + /* TODO: in the future we can support app-specified log filters here, + * similar to how we do dynamic cateogries defined by apps. */ + if (log_filter_index < 0) + return -EINVAL; + + if (log_filter_index >= LOG_MAX_FILTERS) + return -EINVAL; + + if (enable) + target->filter_map |= (1 << log_filter_index); + else + target->filter_map &= ~(1 << log_filter_index); + return 0; +} + +/*! Obtain data associated to a log filter + * \param[in] target Log target to be queried + * \param[in] log_filter_index the index of the log filter to query + * \returns data associated to the log filter + */ +void *log_get_filter_data(const struct log_target *target, int log_filter_index) +{ + /* TODO: in the future we can support app-specified log filters here, + * similar to how we do dynamic cateogries defined by apps. */ + if (log_filter_index < 0) + return NULL; + + if (log_filter_index >= LOG_MAX_FILTERS) + return NULL; + + return target->filter_data[log_filter_index]; +} + +/*! Set data associated to a log filter + * \param[in] target Log target to be affected + * \param[in] log_filter_index the index of the log filter to set associate data for + * \param[in] data The user provided data pointer to associate to the log filter + * \returns 0 on success, negative on error. + */ +int log_set_filter_data(struct log_target *target, int log_filter_index, void *data) +{ + /* TODO: in the future we can support app-specified log filters here, + * similar to how we do dynamic cateogries defined by apps. */ + if (log_filter_index < 0) + return -EINVAL; + + if (log_filter_index >= LOG_MAX_FILTERS) + return -EINVAL; + + target->filter_data[log_filter_index] = data; + return 0; +} + /*! Enable or disable the use of colored output * \param[in] target Log target to be affected * \param[in] use_color Use color (1) or don't use color (0)