xref: /dpdk/drivers/net/ntnic/ntlog/ntlog.c (revision 3489b87b497ed477257f5ed5b112c27c1407a68d)
1 /*
2  * SPDX-License-Identifier: BSD-3-Clause
3  * Copyright(c) 2023 Napatech A/S
4  */
5 
6 #include "ntlog.h"
7 
8 #include <stdarg.h>
9 #include <stddef.h>
10 #include <string.h>
11 #include <stdlib.h>
12 #include <stdbool.h>
13 
14 #include <rte_log.h>
15 #include <rte_string_fns.h>
16 
17 #define NTLOG_HELPER_STR_SIZE_MAX (1024)
18 
19 char *ntlog_helper_str_alloc(const char *sinit)
20 {
21 	char *s = malloc(NTLOG_HELPER_STR_SIZE_MAX);
22 
23 	if (!s)
24 		return NULL;
25 
26 	if (sinit)
27 		snprintf(s, NTLOG_HELPER_STR_SIZE_MAX, "%s", sinit);
28 
29 	else
30 		s[0] = '\0';
31 
32 	return s;
33 }
34 
35 __rte_format_printf(2, 0)
36 void ntlog_helper_str_add(char *s, const char *format, ...)
37 {
38 	if (!s)
39 		return;
40 
41 	va_list args;
42 	va_start(args, format);
43 	int len = strlen(s);
44 	vsnprintf(&s[len], (NTLOG_HELPER_STR_SIZE_MAX - 1 - len), format, args);
45 	va_end(args);
46 }
47 
48 void ntlog_helper_str_free(char *s)
49 {
50 	free(s);
51 }
52