blob: abf0ac4941046172806474b46da4eda48dcf09dc [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>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020026
Pablo Neira Ayuso83419342011-03-22 16:36:13 +010027#include <osmocom/core/linuxlist.h>
28#include <osmocom/core/talloc.h>
29#include <osmocom/core/timer.h>
30#include <osmocom/core/rate_ctr.h>
Harald Weltefab0ae92012-08-17 12:17:38 +020031#include <osmocom/core/utils.h>
Harald Welte3fb0b6f2010-05-19 19:02:52 +020032
33#include <osmocom/vty/vty.h>
34
Harald Welte7acb30c2011-08-17 17:13:48 +020035/* \file utils.c */
36
37/*! \addtogroup rate_ctr
38 * @{
39 */
40
41/*! \brief print a rate counter group to given VTY
42 * \param[in] vty The VTY to which it should be printed
43 * \param[in] prefix Any additional log prefix ahead of each line
44 * \param[in] ctrg Rate counter group to be printed
45 */
Harald Welte3fb0b6f2010-05-19 19:02:52 +020046void vty_out_rate_ctr_group(struct vty *vty, const char *prefix,
47 struct rate_ctr_group *ctrg)
48{
49 unsigned int i;
50
51 vty_out(vty, "%s%s:%s", prefix, ctrg->desc->group_description, VTY_NEWLINE);
52 for (i = 0; i < ctrg->desc->num_ctr; i++) {
53 struct rate_ctr *ctr = &ctrg->ctr[i];
54 vty_out(vty, " %s%s: %8" PRIu64 " "
55 "(%" PRIu64 "/s %" PRIu64 "/m %" PRIu64 "/h %" PRIu64 "/d)%s",
56 prefix, ctrg->desc->ctr_desc[i].description, ctr->current,
57 ctr->intv[RATE_CTR_INTV_SEC].rate,
58 ctr->intv[RATE_CTR_INTV_MIN].rate,
59 ctr->intv[RATE_CTR_INTV_HOUR].rate,
60 ctr->intv[RATE_CTR_INTV_DAY].rate,
61 VTY_NEWLINE);
62 };
63}
Harald Welte7acb30c2011-08-17 17:13:48 +020064
Harald Weltefab0ae92012-08-17 12:17:38 +020065/*! \brief Generate a VTY command string from value_string */
66char *vty_cmd_string_from_valstr(void *ctx, const struct value_string *vals,
67 const char *prefix, const char *sep,
68 const char *end, int do_lower)
69{
70 int len = 0, offset = 0, ret, rem;
71 int size = strlen(prefix);
72 const struct value_string *vs;
73 char *str;
74
75 for (vs = vals; vs->value || vs->str; vs++)
76 size += strlen(vs->str) + 1;
77
78 rem = size;
79 str = talloc_zero_size(ctx, size);
80 if (!str)
81 return NULL;
82
83 ret = snprintf(str + offset, rem, prefix);
84 if (ret < 0)
85 goto err;
86 OSMO_SNPRINTF_RET(ret, rem, offset, len);
87
88 for (vs = vals; vs->value || vs->str; vs++) {
89 if (vs->str) {
90 int j, name_len = strlen(vs->str)+1;
91 char name[name_len];
92
93 for (j = 0; j < name_len; j++)
94 name[j] = do_lower ?
95 tolower(vs->str[j]) : vs->str[j];
96
97 name[name_len-1] = '\0';
98 ret = snprintf(str + offset, rem, "%s%s", name, sep);
99 if (ret < 0)
100 goto err;
101 OSMO_SNPRINTF_RET(ret, rem, offset, len);
102 }
103 }
104 offset--; /* to remove the trailing | */
105 rem++;
106
107 ret = snprintf(str + offset, rem, end);
108 if (ret < 0)
109 goto err;
110 OSMO_SNPRINTF_RET(ret, rem, offset, len);
111err:
112 str[size-1] = '\0';
113 return str;
114}
115
116
Sylvain Munautdca7d2c2012-04-18 21:53:23 +0200117/*! @} */