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>
43 format_vec32 (u8 * s, va_list * va)
45 u32 *v = va_arg (*va, u32 *);
46 char *fmt = va_arg (*va, char *);
48 for (i = 0; i < vec_len (v); i++)
52 s = format (s, fmt, v[i]);
58 format_vec_uword (u8 * s, va_list * va)
60 uword *v = va_arg (*va, uword *);
61 char *fmt = va_arg (*va, char *);
63 for (i = 0; i < vec_len (v); i++)
67 s = format (s, fmt, v[i]);
72 /* Ascii buffer and length. */
74 format_ascii_bytes (u8 * s, va_list * va)
76 u8 *v = va_arg (*va, u8 *);
77 uword n_bytes = va_arg (*va, uword);
78 vec_add (s, v, n_bytes);
82 /* Format hex dump. */
84 format_hex_bytes (u8 * s, va_list * va)
86 u8 *bytes = va_arg (*va, u8 *);
87 int n_bytes = va_arg (*va, int);
90 /* Print short or long form depending on byte count. */
91 uword short_form = n_bytes <= 32;
92 u32 indent = format_get_indent (s);
97 for (i = 0; i < n_bytes; i++)
99 if (!short_form && (i % 32) == 0)
100 s = format (s, "%08x: ", i);
102 s = format (s, "%02x", bytes[i]);
104 if (!short_form && ((i + 1) % 32) == 0 && (i + 1) < n_bytes)
105 s = format (s, "\n%U", format_white_space, indent);
112 format_hex_bytes_no_wrap (u8 * s, va_list * va)
114 u8 *bytes = va_arg (*va, u8 *);
115 int n_bytes = va_arg (*va, int);
121 for (i = 0; i < n_bytes; i++)
122 s = format (s, "%02x", bytes[i]);
127 /* Add variable number of spaces. */
129 format_white_space (u8 * s, va_list * va)
131 u32 n = va_arg (*va, u32);
138 format_duration (u8 *s, va_list *args)
140 f64 t = va_arg (*args, f64);
143 const f64 seconds_per_minute = 60;
144 const f64 seconds_per_hour = 60 * seconds_per_minute;
145 const f64 seconds_per_day = 24 * seconds_per_hour;
146 uword days, hours, minutes, secs, msecs, usecs;
148 days = t / seconds_per_day;
149 t -= days * seconds_per_day;
151 hours = t / seconds_per_hour;
152 t -= hours * seconds_per_hour;
154 minutes = t / seconds_per_minute;
155 t -= minutes * seconds_per_minute;
163 usecs = usecs % 1000;
168 s = format (s, "%ddays ", days);
170 s = format (s, "%dh ", hours);
172 s = format (s, "%dmin ", minutes);
174 s = format (s, "%ds ", secs);
176 s = format (s, "%dms ", msecs);
178 s = format (s, "%dus", usecs);
184 format_time_interval (u8 * s, va_list * args)
186 u8 *fmt = va_arg (*args, u8 *);
187 f64 t = va_arg (*args, f64);
190 const f64 seconds_per_minute = 60;
191 const f64 seconds_per_hour = 60 * seconds_per_minute;
192 const f64 seconds_per_day = 24 * seconds_per_hour;
193 uword days, hours, minutes, secs, msecs, usecs;
195 days = t / seconds_per_day;
196 t -= days * seconds_per_day;
198 hours = t / seconds_per_hour;
199 t -= hours * seconds_per_hour;
201 minutes = t / seconds_per_minute;
202 t -= minutes * seconds_per_minute;
210 for (f = fmt; *f; f++)
213 char *what_fmt = "%d";
247 s = format (s, what_fmt, what);
253 /* Format base 10 e.g. 100, 100K, 100M, 100G */
255 format_base10 (u8 *s, va_list *va)
257 u64 size = va_arg (*va, u64);
260 s = format (s, "%d", size);
261 else if (size < 1000000)
262 s = format (s, "%.2fK", (f64) size / 1000.);
263 else if (size < 1000000000)
264 s = format (s, "%.2fM", (f64) size / 1000000.);
266 s = format (s, "%.2fG", (f64) size / 1000000000.);
271 /* Unparse memory size e.g. 100, 100k, 100m, 100g. */
273 format_memory_size (u8 * s, va_list * va)
275 uword size = va_arg (*va, uword);
278 l = size > 0 ? min_log2 (size) : 0;
288 u = (uword) 1 << log_u;
290 s = format (s, "%.2f", (f64) size / (f64) u);
292 s = format (s, "%d", size >> log_u);
295 s = format (s, "%c", " kmg"[log_u / 10]);
300 /* Parse memory size e.g. 100, 100k, 100m, 100g. */
302 unformat_memory_size (unformat_input_t * input, va_list * va)
304 uword amount, shift, c;
305 uword *result = va_arg (*va, uword *);
307 if (!unformat (input, "%wd%_", &amount))
310 c = unformat_get_input (input);
327 unformat_put_input (input);
331 *result = amount << shift;
335 /* Unparse memory page size e.g. 4K, 2M */
337 format_log2_page_size (u8 * s, va_list * va)
339 clib_mem_page_sz_t log2_page_sz = va_arg (*va, clib_mem_page_sz_t);
341 if (log2_page_sz == CLIB_MEM_PAGE_SZ_UNKNOWN)
342 return format (s, "unknown");
344 if (log2_page_sz == CLIB_MEM_PAGE_SZ_DEFAULT)
345 return format (s, "default");
347 if (log2_page_sz == CLIB_MEM_PAGE_SZ_DEFAULT_HUGE)
348 return format (s, "default-hugepage");
350 if (log2_page_sz >= 30)
351 return format (s, "%uG", 1 << (log2_page_sz - 30));
353 if (log2_page_sz >= 20)
354 return format (s, "%uM", 1 << (log2_page_sz - 20));
356 if (log2_page_sz >= 10)
357 return format (s, "%uK", 1 << (log2_page_sz - 10));
359 return format (s, "%u", 1 << log2_page_sz);
362 /* Parse memory page size e.g. 4K, 2M */
364 unformat_log2_page_size (unformat_input_t * input, va_list * va)
367 clib_mem_page_sz_t *result = va_arg (*va, clib_mem_page_sz_t *);
369 if (unformat (input, "default-hugepage"))
370 *result = CLIB_MEM_PAGE_SZ_DEFAULT_HUGE;
371 else if (unformat (input, "default"))
372 *result = CLIB_MEM_PAGE_SZ_DEFAULT;
373 else if (unformat (input, "%wdk", &amount))
374 *result = min_log2 (amount) + 10;
375 else if (unformat (input, "%wdK", &amount))
376 *result = min_log2 (amount) + 10;
377 else if (unformat (input, "%wdm", &amount))
378 *result = min_log2 (amount) + 20;
379 else if (unformat (input, "%wdM", &amount))
380 *result = min_log2 (amount) + 20;
381 else if (unformat (input, "%wdg", &amount))
382 *result = min_log2 (amount) + 30;
383 else if (unformat (input, "%wdG", &amount))
384 *result = min_log2 (amount) + 30;
390 /* Format c identifier: e.g. a_name -> "a name".
391 Works for both vector names and null terminated c strings. */
393 format_c_identifier (u8 * s, va_list * va)
395 u8 *id = va_arg (*va, u8 *);
401 for (i = 0; i < l && id[i] != 0; i++)
414 format_hexdump (u8 * s, va_list * args)
416 u8 *data = va_arg (*args, u8 *);
417 uword len = va_arg (*args, uword);
419 const int line_len = 16;
422 u32 indent = format_get_indent (s);
427 for (i = 0; i < len; i++)
429 line_hex = format (line_hex, "%02x ", data[i]);
430 line_str = format (line_str, "%c", isprint (data[i]) ? data[i] : '.');
431 if (!((i + 1) % line_len))
433 s = format (s, "%U%05x: %v[%v]",
434 format_white_space, index ? indent : 0,
435 index, line_hex, line_str);
437 s = format (s, "\n");
439 vec_reset_length (line_hex);
440 vec_reset_length (line_str);
444 while (i++ % line_len)
445 line_hex = format (line_hex, " ");
447 if (vec_len (line_hex))
448 s = format (s, "%U%05x: %v[%v]",
449 format_white_space, index ? indent : 0,
450 index, line_hex, line_str);
459 format_hexdump_u16 (u8 *s, va_list *args)
461 u16 *data = va_arg (*args, u16 *);
462 u32 len = va_arg (*args, u32);
463 u32 indent = format_get_indent (s);
468 for (int i = 0; i < len; i++)
472 s = format (s, "%s%U%05x: ", i ? "\n" : "", format_white_space,
473 i ? indent : 0, i * 2);
475 s = format (s, " %04lx", data[i]);
481 format_hexdump_u32 (u8 *s, va_list *args)
483 u32 *data = va_arg (*args, u32 *);
484 u32 len = va_arg (*args, u32);
485 u32 indent = format_get_indent (s);
490 for (int i = 0; i < len; i++)
494 s = format (s, "%s%U%05x: ", i ? "\n" : "", format_white_space,
495 i ? indent : 0, i * 4);
497 s = format (s, " %08lx", data[i]);
503 format_hexdump_u64 (u8 *s, va_list *args)
505 u64 *data = va_arg (*args, u64 *);
506 u32 len = va_arg (*args, u32);
507 u32 indent = format_get_indent (s);
512 for (int i = 0; i < len; i++)
516 s = format (s, "%s%U%05x: ", i ? "\n" : "", format_white_space,
517 i ? indent : 0, i * 8);
519 s = format (s, " %016lx", data[i]);
525 format_uword_bitmap (u8 *s, va_list *args)
527 uword *bitmap = va_arg (*args, uword *);
528 int n_uword = va_arg (*args, int);
529 uword indent = format_get_indent (s);
531 s = format (s, "%6s", "");
533 for (int i = uword_bits - 4; i >= 0; i -= 4)
534 s = format (s, "%5d", i);
538 for (int j = n_uword - 1; j >= 0; j--)
540 s = format (s, "%U0x%04x ", format_white_space, indent,
542 for (int i = uword_bits - 1; i >= 0; i--)
544 vec_add1 (s, (1ULL << i) & bitmap[j] ? '1' : '.');
548 s = format (s, uword_bits == 64 ? "0x%016lx" : "0x%08lx", bitmap[j]);
557 * fd.io coding-style-patch-verification: ON
560 * eval: (c-set-style "gnu")