2 * Copyright (c) 2015 Cisco and/or its affiliates.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at:
7 * http://www.apache.org/licenses/LICENSE-2.0
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
16 Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus
18 Permission is hereby granted, free of charge, to any person obtaining
19 a copy of this software and associated documentation files (the
20 "Software"), to deal in the Software without restriction, including
21 without limitation the rights to use, copy, modify, merge, publish,
22 distribute, sublicense, and/or sell copies of the Software, and to
23 permit persons to whom the Software is furnished to do so, subject to
24 the following conditions:
26 The above copyright notice and this permission notice shall be
27 included in all copies or substantial portions of the Software.
29 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
30 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
31 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
32 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
33 LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
34 OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
35 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
38 #include <vppinfra/format.h>
41 u8 * format_vec32 (u8 * s, va_list * va)
43 u32 * v = va_arg (*va, u32 *);
44 char * fmt = va_arg (*va, char *);
46 for (i = 0; i < vec_len (v); i++)
50 s = format (s, fmt, v[i]);
55 u8 * format_vec_uword (u8 * s, va_list * va)
57 uword * v = va_arg (*va, uword *);
58 char * fmt = va_arg (*va, char *);
60 for (i = 0; i < vec_len (v); i++)
64 s = format (s, fmt, v[i]);
69 /* Ascii buffer and length. */
70 u8 * format_ascii_bytes (u8 * s, va_list * va)
72 u8 * v = va_arg (*va, u8 *);
73 uword n_bytes = va_arg (*va, uword);
74 vec_add (s, v, n_bytes);
78 /* Format hex dump. */
79 u8 * format_hex_bytes (u8 * s, va_list * va)
81 u8 * bytes = va_arg (*va, u8 *);
82 int n_bytes = va_arg (*va, int);
85 /* Print short or long form depending on byte count. */
86 uword short_form = n_bytes <= 32;
87 uword indent = format_get_indent (s);
92 for (i = 0; i < n_bytes; i++)
94 if (! short_form && (i % 32) == 0)
95 s = format (s, "%08x: ", i);
97 s = format (s, "%02x", bytes[i]);
99 if (! short_form && ((i + 1) % 32) == 0 && (i + 1) < n_bytes)
100 s = format (s, "\n%U", format_white_space, indent);
106 /* Add variable number of spaces. */
107 u8 * format_white_space (u8 * s, va_list * va)
109 uword n = va_arg (*va, uword);
115 u8 * format_time_interval (u8 * s, va_list * args)
117 u8 * fmt = va_arg (*args, u8 *);
118 f64 t = va_arg (*args, f64);
121 const f64 seconds_per_minute = 60;
122 const f64 seconds_per_hour = 60 * seconds_per_minute;
123 const f64 seconds_per_day = 24 * seconds_per_hour;
124 uword days, hours, minutes, secs, msecs, usecs;
126 days = t / seconds_per_day;
127 t -= days * seconds_per_day;
129 hours = t / seconds_per_hour;
130 t -= hours * seconds_per_hour;
132 minutes = t / seconds_per_minute;
133 t -= minutes * seconds_per_minute;
141 for (f = fmt; *f; f++)
144 char * what_fmt = "%d";
178 s = format (s, what_fmt, what);
184 /* Unparse memory size e.g. 100, 100k, 100m, 100g. */
185 u8 * format_memory_size (u8 * s, va_list * va)
187 uword size = va_arg (*va, uword);
190 l = size > 0 ? min_log2 (size) : 0;
200 u = (uword) 1 << log_u;
202 s = format (s, "%.2f", (f64) size / (f64) u);
204 s = format (s, "%d", size >> log_u);
207 s = format (s, "%c", " kmg"[log_u / 10]);
212 /* Parse memory size e.g. 100, 100k, 100m, 100g. */
213 uword unformat_memory_size (unformat_input_t * input, va_list * va)
215 uword amount, shift, c;
216 uword * result = va_arg (*va, uword *);
218 if (! unformat (input, "%wd%_", &amount))
221 c = unformat_get_input (input);
224 case 'k': case 'K': shift = 10; break;
225 case 'm': case 'M': shift = 20; break;
226 case 'g': case 'G': shift = 30; break;
229 unformat_put_input (input);
233 *result = amount << shift;
237 /* Format c identifier: e.g. a_name -> "a name".
238 Words for both vector names and null terminated c strings. */
239 u8 * format_c_identifier (u8 * s, va_list * va)
241 u8 * id = va_arg (*va, u8 *);
245 if (clib_mem_is_vec (id))
249 for (i = 0; id[i] != 0 && i < l; i++)