5 * Copyright (C) 2012 Intel Corporation. All rights reserved.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #include "src/shared/util.h"
34 void util_debug(util_debug_func_t function, void *user_data,
35 const char *format, ...)
40 if (!function || !format)
44 vsnprintf(str, sizeof(str), format, ap);
47 function(str, user_data);
50 void util_hexdump(const char dir, const unsigned char *buf, size_t len,
51 util_debug_func_t function, void *user_data)
53 static const char hexdigits[] = "0123456789abcdef";
57 if (!function || !len)
62 for (i = 0; i < len; i++) {
63 str[((i % 16) * 3) + 1] = ' ';
64 str[((i % 16) * 3) + 2] = hexdigits[buf[i] >> 4];
65 str[((i % 16) * 3) + 3] = hexdigits[buf[i] & 0xf];
66 str[(i % 16) + 51] = isprint(buf[i]) ? buf[i] : '.';
68 if ((i + 1) % 16 == 0) {
72 function(str, user_data);
79 for (j = (i % 16); j < 16; j++) {
80 str[(j * 3) + 1] = ' ';
81 str[(j * 3) + 2] = ' ';
82 str[(j * 3) + 3] = ' ';
88 function(str, user_data);
92 void util_iso8601_to_timeval(char *str, struct timeval *time)
98 p = strptime(str, "%FT%T", &tm);
103 /* backwards compatibility */
104 if (*p != '.' || p[strlen(p) - 1] != 'Z')
116 char *util_timeval_to_iso8601(struct timeval *time)
123 if (!localtime_r(&t, &tm))
125 if (!strftime(buf, sizeof(buf), "%FT%TZ", &tm))
128 return g_strdup(buf);