blob: 93e9ef48d08b63880574d336f8d149dbb0568f40 [file] [log] [blame]
Harald Welte3fb0b6f2010-05-19 19:02:52 +02001/* utility routines for printing common objects in the Osmocom world */
2
3/* (C) 2009-2010 by Harald Welte <laforge@gnumonks.org>
4 *
5 * All Rights Reserved
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 */
22
23#include <stdint.h>
24#include <inttypes.h>
Harald Weltefab0ae92012-08-17 12:17:38 +020025#include <string.h>
Holger Hans Peter Freytherb321b932012-09-11 10:39:29 +020026#include <ctype.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020027
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010028#include <osmocom/core/linuxlist.h>
29#include <osmocom/core/talloc.h>
30#include <osmocom/core/timer.h>
31#include <osmocom/core/rate_ctr.h>
Jacob Erlbeck738d9e22015-10-06 15:21:56 +020032#include <osmocom/core/stat_item.h>
Harald Weltefab0ae92012-08-17 12:17:38 +020033#include <osmocom/core/utils.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020034
35#include <osmocom/vty/vty.h>
36
Harald Welte7acb30c2011-08-17 17:13:48 +020037/* \file utils.c */
38
39/*! \addtogroup rate_ctr
40 * @{
41 */
42
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020043struct vty_out_context {
44 struct vty *vty;
45 const char *prefix;
46};
47
48static int rate_ctr_handler(
49 struct rate_ctr_group *ctrg, struct rate_ctr *ctr,
50 const struct rate_ctr_desc *desc, void *vctx_)
51{
52 struct vty_out_context *vctx = vctx_;
53 struct vty *vty = vctx->vty;
54
55 vty_out(vty, " %s%s: %8" PRIu64 " "
56 "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s",
57 vctx->prefix, desc->description, ctr->current,
58 ctr->intv[RATE_CTR_INTV_SEC].rate,
59 ctr->intv[RATE_CTR_INTV_MIN].rate,
60 ctr->intv[RATE_CTR_INTV_HOUR].rate,
61 ctr->intv[RATE_CTR_INTV_DAY].rate,
62 VTY_NEWLINE);
63
64 return 0;
65}
66
Harald Welte7acb30c2011-08-17 17:13:48 +020067/*! \brief print a rate counter group to given VTY
68 * \param[in] vty The VTY to which it should be printed
69 * \param[in] prefix Any additional log prefix ahead of each line
70 * \param[in] ctrg Rate counter group to be printed
71 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020072void vty_out_rate_ctr_group(struct vty *vty, const char *prefix,
73 struct rate_ctr_group *ctrg)
74{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020075 struct vty_out_context vctx = {vty, prefix};
Harald Welte3fb0b6f2010-05-19 19:02:52 +020076
77 vty_out(vty, "%s%s:%s", prefix, ctrg->desc->group_description, VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +020078
79 rate_ctr_for_each_counter(ctrg, rate_ctr_handler, &vctx);
80}
81
82static int stat_item_handler(
83 struct stat_item_group *statg, struct stat_item *item, void *vctx_)
84{
85 struct vty_out_context *vctx = vctx_;
86 struct vty *vty = vctx->vty;
87
88 vty_out(vty, " %s%s: %8" PRIi32 " %s%s",
89 vctx->prefix, item->desc->description,
90 stat_item_get_last(item),
91 item->desc->unit, VTY_NEWLINE);
92
93 return 0;
Harald Welte3fb0b6f2010-05-19 19:02:52 +020094}
Harald Welte7acb30c2011-08-17 17:13:48 +020095
Jacob Erlbeck738d9e22015-10-06 15:21:56 +020096/*! \brief print a stat item group to given VTY
97 * \param[in] vty The VTY to which it should be printed
98 * \param[in] prefix Any additional log prefix ahead of each line
99 * \param[in] statg Stat item group to be printed
100 */
101void vty_out_stat_item_group(struct vty *vty, const char *prefix,
102 struct stat_item_group *statg)
103{
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200104 struct vty_out_context vctx = {vty, prefix};
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200105
106 vty_out(vty, "%s%s:%s", prefix, statg->desc->group_description,
107 VTY_NEWLINE);
Jacob Erlbeckaec583f2015-10-19 15:06:01 +0200108 stat_item_for_each_item(statg, stat_item_handler, &vctx);
Jacob Erlbeck738d9e22015-10-06 15:21:56 +0200109}
110
Harald Weltefab0ae92012-08-17 12:17:38 +0200111/*! \brief Generate a VTY command string from value_string */
112char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
113 const char *prefix, const char *sep,
114 const char *end, int do_lower)
115{
116 int len = 0, offset = 0, ret, rem;
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200117 int size = strlen(prefix) + strlen(end);
118 int sep_len = strlen(sep);
Harald Weltefab0ae92012-08-17 12:17:38 +0200119 const struct value_string *vs;
120 char *str;
121
122 for (vs = vals; vs->value || vs->str; vs++)
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200123 size += strlen(vs->str) + sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200124
125 rem = size;
126 str = talloc_zero_size(ctx, size);
127 if (!str)
128 return NULL;
129
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200130 ret = snprintf(str + offset, rem, "%s", prefix);
Harald Weltefab0ae92012-08-17 12:17:38 +0200131 if (ret < 0)
132 goto err;
133 OSMO_SNPRINTF_RET(ret, rem, offset, len);
134
135 for (vs = vals; vs->value || vs->str; vs++) {
136 if (vs->str) {
137 int j, name_len = strlen(vs->str)+1;
138 char name[name_len];
139
140 for (j = 0; j < name_len; j++)
141 name[j] = do_lower ?
142 tolower(vs->str[j]) : vs->str[j];
143
144 name[name_len-1] = '\0';
145 ret = snprintf(str + offset, rem, "%s%s", name, sep);
146 if (ret < 0)
147 goto err;
148 OSMO_SNPRINTF_RET(ret, rem, offset, len);
149 }
150 }
Jacob Erlbeckcd195fa2013-08-06 14:29:15 +0200151 offset -= sep_len; /* to remove the trailing sep */
152 rem += sep_len;
Harald Weltefab0ae92012-08-17 12:17:38 +0200153
Jacob Erlbeckae15a2c2013-08-06 14:29:14 +0200154 ret = snprintf(str + offset, rem, "%s", end);
Harald Weltefab0ae92012-08-17 12:17:38 +0200155 if (ret < 0)
156 goto err;
157 OSMO_SNPRINTF_RET(ret, rem, offset, len);
158err:
159 str[size-1] = '\0';
160 return str;
161}
162
163
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200164/*! @} */