fixeria has uploaded this change for review. ( 
https://gerrit.osmocom.org/c/libosmocore/+/24576 )


Change subject: stat,rate_ctr: allow setting group name using a format string
......................................................................

stat,rate_ctr: allow setting group name using a format string

Change-Id: I6e813476cfb6a0ad275c4a51e9f065eeca8cb406
---
M include/osmocom/core/rate_ctr.h
M include/osmocom/core/stat_item.h
M src/rate_ctr.c
M src/stat_item.c
4 files changed, 50 insertions(+), 0 deletions(-)



  git pull ssh://gerrit.osmocom.org:29418/libosmocore refs/changes/76/24576/1

diff --git a/include/osmocom/core/rate_ctr.h b/include/osmocom/core/rate_ctr.h
index d944cc0..40b432c 100644
--- a/include/osmocom/core/rate_ctr.h
+++ b/include/osmocom/core/rate_ctr.h
@@ -76,6 +76,7 @@
        grp->idx = idx;
 }
 void rate_ctr_group_set_name(struct rate_ctr_group *grp, const char *name);
+void rate_ctr_group_set_name_fmt(struct rate_ctr_group *grp, const char *fmt, 
...);

 struct rate_ctr *rate_ctr_group_get_ctr(struct rate_ctr_group *grp, unsigned 
int idx);

diff --git a/include/osmocom/core/stat_item.h b/include/osmocom/core/stat_item.h
index fbe0433..377c762 100644
--- a/include/osmocom/core/stat_item.h
+++ b/include/osmocom/core/stat_item.h
@@ -83,6 +83,7 @@
 }
 struct osmo_stat_item *osmo_stat_item_group_get_item(struct 
osmo_stat_item_group *grp, unsigned int idx);
 void osmo_stat_item_group_set_name(struct osmo_stat_item_group *statg, const 
char *name);
+void osmo_stat_item_group_set_name_fmt(struct osmo_stat_item_group *statg, 
const char *fmt, ...);
 void osmo_stat_item_group_free(struct osmo_stat_item_group *statg);

 void osmo_stat_item_inc(struct osmo_stat_item *item, int32_t value);
diff --git a/src/rate_ctr.c b/src/rate_ctr.c
index 4d99699..b860b86 100644
--- a/src/rate_ctr.c
+++ b/src/rate_ctr.c
@@ -283,6 +283,30 @@
        osmo_talloc_replace_string(grp, &grp->name, name);
 }

+/*! Similar to rate_ctr_group_set_name(), but accepts a format string.
+ *  \param[in] grp Rate counter group.
+ *  \param[in] fmt Format string for the name.
+ *  \param[in] ... Variable argument list for the format string.
+ */
+void rate_ctr_group_set_name_fmt(struct rate_ctr_group *grp,
+                                const char *fmt, ...)
+{
+       char *name = NULL;
+
+       if (grp->name != NULL)
+               talloc_free(grp->name);
+
+       if (fmt != NULL) {
+               va_list ap;
+
+               va_start(ap, fmt);
+               name = talloc_vasprintf(grp, fmt, ap);
+               va_end(ap);
+       }
+
+       grp->name = name;
+}
+
 /*! Add a number to the counter */
 void rate_ctr_add(struct rate_ctr *ctr, int inc)
 {
diff --git a/src/stat_item.c b/src/stat_item.c
index 55aa951..c81c704 100644
--- a/src/stat_item.c
+++ b/src/stat_item.c
@@ -187,6 +187,30 @@
        osmo_talloc_replace_string(statg, &statg->name, name);
 }

+/*! Similar to osmo_stat_item_group_set_name(), but accepts a format string.
+ *  \param[in] statg Rate counter group.
+ *  \param[in]   fmt Format string for the name.
+ *  \param[in]   ... Variable argument list for the format string.
+ */
+void osmo_stat_item_group_set_name_fmt(struct osmo_stat_item_group *statg,
+                                      const char *fmt, ...)
+{
+       char *name = NULL;
+
+       if (statg->name != NULL)
+               talloc_free(statg->name);
+
+       if (fmt != NULL) {
+               va_list ap;
+
+               va_start(ap, fmt);
+               name = talloc_vasprintf(statg, fmt, ap);
+               va_end(ap);
+       }
+
+       statg->name = name;
+}
+
 /*! Increase the stat_item to the given value.
  *  This function adds a new value for the given stat_item at the end of
  *  the FIFO.

--
To view, visit https://gerrit.osmocom.org/c/libosmocore/+/24576
To unsubscribe, or for help writing mail filters, visit 
https://gerrit.osmocom.org/settings

Gerrit-Project: libosmocore
Gerrit-Branch: master
Gerrit-Change-Id: I6e813476cfb6a0ad275c4a51e9f065eeca8cb406
Gerrit-Change-Number: 24576
Gerrit-PatchSet: 1
Gerrit-Owner: fixeria <vyanits...@sysmocom.de>
Gerrit-MessageType: newchange

Reply via email to