]>
git.proxmox.com Git - ceph.git/blob - ceph/src/spdk/dpdk/lib/librte_telemetry/telemetry_json.h
1 /* SPDX-License-Identifier: BSD-3-Clause
2 * Copyright(c) 2020 Intel Corporation
5 #ifndef _RTE_TELEMETRY_JSON_H_
6 #define _RTE_TELEMETRY_JSON_H_
11 #include <rte_common.h>
15 * Internal Telemetry Utility functions
17 * This file contains small inline functions to make it easier for applications
18 * to build up valid JSON responses to telemetry requests.
24 * Copies a value into a buffer if the buffer has enough available space.
25 * Nothing written to buffer if an overflow ocurs.
26 * This function is not for use for values larger than 1k.
28 __rte_format_printf(3, 4)
30 __json_snprintf(char *buf
, const int len
, const char *format
, ...)
37 ret
= vsnprintf(tmp
, sizeof(tmp
), format
, ap
);
39 if (ret
> 0 && ret
< (int)sizeof(tmp
) && ret
< len
) {
43 return 0; /* nothing written or modified */
46 /* Copies an empty array into the provided buffer. */
48 rte_tel_json_empty_array(char *buf
, const int len
, const int used
)
50 return used
+ __json_snprintf(buf
+ used
, len
- used
, "[]");
53 /* Copies an empty object into the provided buffer. */
55 rte_tel_json_empty_obj(char *buf
, const int len
, const int used
)
57 return used
+ __json_snprintf(buf
+ used
, len
- used
, "{}");
60 /* Copies a string into the provided buffer, in JSON format. */
62 rte_tel_json_str(char *buf
, const int len
, const int used
, const char *str
)
64 return used
+ __json_snprintf(buf
+ used
, len
- used
, "\"%s\"", str
);
67 /* Appends a string into the JSON array in the provided buffer. */
69 rte_tel_json_add_array_string(char *buf
, const int len
, const int used
,
72 int ret
, end
= used
- 1; /* strip off final delimiter */
73 if (used
<= 2) /* assume empty, since minimum is '[]' */
74 return __json_snprintf(buf
, len
, "[\"%s\"]", str
);
76 ret
= __json_snprintf(buf
+ end
, len
- end
, ",\"%s\"]", str
);
77 return ret
== 0 ? used
: end
+ ret
;
80 /* Appends an integer into the JSON array in the provided buffer. */
82 rte_tel_json_add_array_int(char *buf
, const int len
, const int used
, int val
)
84 int ret
, end
= used
- 1; /* strip off final delimiter */
85 if (used
<= 2) /* assume empty, since minimum is '[]' */
86 return __json_snprintf(buf
, len
, "[%d]", val
);
88 ret
= __json_snprintf(buf
+ end
, len
- end
, ",%d]", val
);
89 return ret
== 0 ? used
: end
+ ret
;
92 /* Appends a uint64_t into the JSON array in the provided buffer. */
94 rte_tel_json_add_array_u64(char *buf
, const int len
, const int used
,
97 int ret
, end
= used
- 1; /* strip off final delimiter */
98 if (used
<= 2) /* assume empty, since minimum is '[]' */
99 return __json_snprintf(buf
, len
, "[%"PRIu64
"]", val
);
101 ret
= __json_snprintf(buf
+ end
, len
- end
, ",%"PRIu64
"]", val
);
102 return ret
== 0 ? used
: end
+ ret
;
106 * Add a new element with uint64_t value to the JSON object stored in the
110 rte_tel_json_add_obj_u64(char *buf
, const int len
, const int used
,
111 const char *name
, uint64_t val
)
113 int ret
, end
= used
- 1;
114 if (used
<= 2) /* assume empty, since minimum is '{}' */
115 return __json_snprintf(buf
, len
, "{\"%s\":%"PRIu64
"}", name
,
118 ret
= __json_snprintf(buf
+ end
, len
- end
, ",\"%s\":%"PRIu64
"}",
120 return ret
== 0 ? used
: end
+ ret
;
124 * Add a new element with int value to the JSON object stored in the
128 rte_tel_json_add_obj_int(char *buf
, const int len
, const int used
,
129 const char *name
, int val
)
131 int ret
, end
= used
- 1;
132 if (used
<= 2) /* assume empty, since minimum is '{}' */
133 return __json_snprintf(buf
, len
, "{\"%s\":%d}", name
,
136 ret
= __json_snprintf(buf
+ end
, len
- end
, ",\"%s\":%d}",
138 return ret
== 0 ? used
: end
+ ret
;
142 * Add a new element with string value to the JSON object stored in the
146 rte_tel_json_add_obj_str(char *buf
, const int len
, const int used
,
147 const char *name
, const char *val
)
149 int ret
, end
= used
- 1;
150 if (used
<= 2) /* assume empty, since minimum is '{}' */
151 return __json_snprintf(buf
, len
, "{\"%s\":\"%s\"}", name
, val
);
153 ret
= __json_snprintf(buf
+ end
, len
- end
, ",\"%s\":\"%s\"}",
155 return ret
== 0 ? used
: end
+ ret
;
158 #endif /*_RTE_TELEMETRY_JSON_H_*/