Rename: field type -> field class
[babeltrace.git] / include / babeltrace / common-internal.h
CommitLineData
1670bffd
PP
1#ifndef BABELTRACE_COMMON_INTERNAL_H
2#define BABELTRACE_COMMON_INTERNAL_H
3
c55a9f58 4#include <stdbool.h>
3dca2276 5#include <babeltrace/assert-internal.h>
1670bffd 6#include <babeltrace/babeltrace-internal.h>
5cd6d0e5 7#include <babeltrace/trace-ir/field-classes.h>
56e18c4c
PP
8#include <babeltrace/trace-ir/field-path.h>
9#include <babeltrace/trace-ir/event-class.h>
85cd02cf 10#include <stdarg.h>
44c440bc
PP
11#include <inttypes.h>
12#include <stdint.h>
3dca2276 13#include <glib.h>
1670bffd 14
ad96d936
PP
15#define BT_COMMON_COLOR_RESET "\033[0m"
16#define BT_COMMON_COLOR_BOLD "\033[1m"
17#define BT_COMMON_COLOR_FG_DEFAULT "\033[39m"
18#define BT_COMMON_COLOR_FG_RED "\033[31m"
19#define BT_COMMON_COLOR_FG_GREEN "\033[32m"
20#define BT_COMMON_COLOR_FG_YELLOW "\033[33m"
21#define BT_COMMON_COLOR_FG_BLUE "\033[34m"
22#define BT_COMMON_COLOR_FG_MAGENTA "\033[35m"
23#define BT_COMMON_COLOR_FG_CYAN "\033[36m"
24#define BT_COMMON_COLOR_FG_LIGHT_GRAY "\033[37m"
25#define BT_COMMON_COLOR_BG_DEFAULT "\033[49m"
26#define BT_COMMON_COLOR_BG_RED "\033[41m"
27#define BT_COMMON_COLOR_BG_GREEN "\033[42m"
28#define BT_COMMON_COLOR_BG_YELLOW "\033[43m"
29#define BT_COMMON_COLOR_BG_BLUE "\033[44m"
30#define BT_COMMON_COLOR_BG_MAGENTA "\033[45m"
31#define BT_COMMON_COLOR_BG_CYAN "\033[46m"
32#define BT_COMMON_COLOR_BG_LIGHT_GRAY "\033[47m"
33
db0f160a
PP
34struct bt_common_lttng_live_url_parts {
35 GString *proto;
36 GString *hostname;
37 GString *target_hostname;
38 GString *session_name;
39
40 /* -1 means default port */
41 int port;
42};
43
6eecdc5f
PP
44/*
45 * Checks if the current process has setuid or setgid access rights.
46 * Returns `true` if so.
47 */
1670bffd
PP
48BT_HIDDEN
49bool bt_common_is_setuid_setgid(void);
50
6eecdc5f
PP
51/*
52 * Returns the system-wide plugin path, e.g.
53 * `/usr/lib/babeltrace/plugins`. Do not free the return value.
54 */
1670bffd
PP
55BT_HIDDEN
56const char *bt_common_get_system_plugin_path(void);
57
6eecdc5f
PP
58/*
59 * Returns the user plugin path, e.g.
60 * `/home/user/.local/lib/babeltrace/plugins`. You need to free the
61 * return value.
62 */
1670bffd
PP
63BT_HIDDEN
64char *bt_common_get_home_plugin_path(void);
65
6eecdc5f
PP
66/*
67 * Appends the list of directories in `paths` to the array `dirs`.
68 * `paths` is a list of directories separated by `:`. Returns 0 on
69 * success.
70 */
1670bffd
PP
71BT_HIDDEN
72int bt_common_append_plugin_path_dirs(const char *paths, GPtrArray *dirs);
73
6eecdc5f
PP
74/*
75 * Returns `true` if terminal color codes are supported for this
76 * process.
77 */
ad96d936
PP
78BT_HIDDEN
79bool bt_common_colors_supported(void);
80
290725f7
PP
81BT_HIDDEN
82const char *bt_common_color_reset(void);
83
84BT_HIDDEN
85const char *bt_common_color_bold(void);
86
87BT_HIDDEN
88const char *bt_common_color_fg_default(void);
89
90BT_HIDDEN
91const char *bt_common_color_fg_red(void);
92
93BT_HIDDEN
94const char *bt_common_color_fg_green(void);
95
96BT_HIDDEN
97const char *bt_common_color_fg_yellow(void);
98
99BT_HIDDEN
100const char *bt_common_color_fg_blue(void);
101
102BT_HIDDEN
103const char *bt_common_color_fg_magenta(void);
104
105BT_HIDDEN
106const char *bt_common_color_fg_cyan(void);
107
108BT_HIDDEN
109const char *bt_common_color_fg_light_gray(void);
110
111BT_HIDDEN
112const char *bt_common_color_bg_default(void);
113
114BT_HIDDEN
115const char *bt_common_color_bg_red(void);
116
117BT_HIDDEN
118const char *bt_common_color_bg_green(void);
119
120BT_HIDDEN
121const char *bt_common_color_bg_yellow(void);
122
123BT_HIDDEN
124const char *bt_common_color_bg_blue(void);
125
126BT_HIDDEN
127const char *bt_common_color_bg_magenta(void);
128
129BT_HIDDEN
130const char *bt_common_color_bg_cyan(void);
131
132BT_HIDDEN
133const char *bt_common_color_bg_light_gray(void);
134
6eecdc5f
PP
135/*
136 * Returns the substring from `input` to the first character found
137 * in the list of characters `end_chars`, unescaping any character
138 * found in `escapable_chars`, and sets `*end_pos` to the position of
139 * the end (from `input`). The caller owns the returned GString.
140 */
db0f160a
PP
141BT_HIDDEN
142GString *bt_common_string_until(const char *input, const char *escapable_chars,
143 const char *end_chars, size_t *end_pos);
144
6eecdc5f
PP
145/*
146 * Returns the quoted version of `input` for a shell. If
147 * `with_single_quotes` is `true`, prepends and appends the `'` prefix
148 * and suffix to the returned string; otherwise the caller should
149 * prepend and append them manually, although they are not always
150 * required. The caller owns the returned GString.
151 */
db0f160a 152BT_HIDDEN
36b405c6 153GString *bt_common_shell_quote(const char *input, bool with_single_quotes);
db0f160a 154
6eecdc5f
PP
155/*
156 * Returns `true` if `input` is a string made only of printable
157 * characters.
158 */
db0f160a
PP
159BT_HIDDEN
160bool bt_common_string_is_printable(const char *input);
161
6eecdc5f
PP
162/*
163 * Destroys the parts of an LTTng live URL as returned by
164 * bt_common_parse_lttng_live_url().
165 */
db0f160a
PP
166BT_HIDDEN
167void bt_common_destroy_lttng_live_url_parts(
168 struct bt_common_lttng_live_url_parts *parts);
169
6eecdc5f
PP
170/*
171 * Parses the LTTng live URL `url` and returns its different parts.
172 * If there's an error, writes the error message into `*error_buf`
173 * up to `error_buf_size` bytes. You must destroy the returned value
174 * with bt_common_destroy_lttng_live_url_parts().
175 */
db0f160a
PP
176BT_HIDDEN
177struct bt_common_lttng_live_url_parts bt_common_parse_lttng_live_url(
178 const char *url, char *error_buf, size_t error_buf_size);
179
6eecdc5f
PP
180/*
181 * Normalizes (in place) a star globbing pattern to be used with
182 * bt_common_star_glob_match(). This function always succeeds.
183 */
9009cc24
PP
184BT_HIDDEN
185void bt_common_normalize_star_glob_pattern(char *pattern);
186
6eecdc5f
PP
187/*
188 * Returns `true` if `candidate` (of size `candidate_len`) matches
189 * the star globbing pattern `pattern` (of size `pattern_len`).
190 */
9009cc24
PP
191BT_HIDDEN
192bool bt_common_star_glob_match(const char *pattern, size_t pattern_len,
193 const char *candidate, size_t candidate_len);
194
6eecdc5f
PP
195/*
196 * Normalizes the path `path`:
197 *
198 * * If it's a relative path, converts it to an absolute path using
199 * `wd` as the working directory (or the current working directory
200 * if `wd` is NULL).
201 * * Removes consecutive and trailing slashes.
202 * * Resolves `..` and `.` in the path (both in `path` and in `wd`).
203 * * Does NOT resolve symbolic links.
204 *
205 * The caller owns the returned GString.
206 */
e49a18d1
PP
207BT_HIDDEN
208GString *bt_common_normalize_path(const char *path, const char *wd);
209
85cd02cf
PP
210typedef void (* bt_common_handle_custom_specifier_func)(void *priv_data,
211 char **buf, size_t avail_size, const char **fmt, va_list *args);
212
213/*
214 * This is a custom vsnprintf() which handles the standard conversion
215 * specifier as well as custom ones.
216 *
217 * `fmt` is a typical printf()-style format string, with the following
218 * limitations:
219 *
220 * * The `*` width specifier is not accepted.
221 * * The `*` precision specifier is not accepted.
222 * * The `j` and `t` length modifiers are not accepted.
223 * * The `n` format specifier is not accepted.
224 * * The format specifiers defined in <inttypes.h> are not accepted
225 * except for `PRId64`, `PRIu64`, `PRIx64`, `PRIX64`, `PRIo64`, and
226 * `PRIi64`.
227 *
228 * `intro` specifies which special character immediately following an
229 * introductory `%` character in `fmt` is used to indicate a custom
230 * conversion specifier. For example, if `intro` is '@', then any `%@`
231 * sequence in `fmt` is the beginning of a custom conversion specifier.
232 *
233 * When a custom conversion specifier is encountered in `fmt`,
234 * the function calls `handle_specifier`. This callback receives:
235 *
236 * `priv_data`:
237 * Custom, private data.
238 *
239 * `buf`:
240 * Address of the current buffer pointer. `*buf` is the position to
241 * append new data. The callback must update `*buf` when appending
242 * new data. The callback must ensure not to write passed the whole
243 * buffer passed to bt_common_custom_vsnprintf().
244 *
245 * `avail_size`:
246 * Number of bytes left in whole buffer from the `*buf` point.
247 *
248 * `fmt`:
249 * Address of the current format string pointer. `*fmt` points to
250 * the introductory `%` character, which is followed by the
251 * character `intro`. The callback must update `*fmt` so that it
252 * points after the whole custom conversion specifier.
253 *
254 * `args`:
255 * Variable argument list. Use va_arg() to get new arguments from
256 * this list and update it at the same time.
257 *
258 * Because this is an internal utility, this function and its callback
259 * do not return error codes: they abort when there's any error (bad
260 * format string, for example).
261 */
262BT_HIDDEN
263void bt_common_custom_vsnprintf(char *buf, size_t buf_size,
264 char intro,
265 bt_common_handle_custom_specifier_func handle_specifier,
266 void *priv_data, const char *fmt, va_list *args);
267
268/*
269 * Variadic form of bt_common_custom_vsnprintf().
270 */
271BT_HIDDEN
272void bt_common_custom_snprintf(char *buf, size_t buf_size,
273 char intro,
274 bt_common_handle_custom_specifier_func handle_specifier,
275 void *priv_data, const char *fmt, ...);
276
108e5a1e 277/*
85cd02cf 278 * Returns the system page size.
108e5a1e
MJ
279 */
280BT_HIDDEN
281size_t bt_common_get_page_size(void);
282
3dca2276 283static inline
5cd6d0e5 284const char *bt_common_field_class_id_string(enum bt_field_class_id class_id)
3dca2276 285{
5cd6d0e5
PP
286 switch (class_id) {
287 case BT_FIELD_CLASS_ID_UNSIGNED_INTEGER:
288 return "BT_FIELD_CLASS_ID_UNSIGNED_INTEGER";
289 case BT_FIELD_CLASS_ID_SIGNED_INTEGER:
290 return "BT_FIELD_CLASS_ID_SIGNED_INTEGER";
291 case BT_FIELD_CLASS_ID_REAL:
292 return "BT_FIELD_CLASS_ID_REAL";
293 case BT_FIELD_CLASS_ID_UNSIGNED_ENUMERATION:
294 return "BT_FIELD_CLASS_ID_UNSIGNED_ENUMERATION";
295 case BT_FIELD_CLASS_ID_SIGNED_ENUMERATION:
296 return "BT_FIELD_CLASS_ID_SIGNED_ENUMERATION";
297 case BT_FIELD_CLASS_ID_STRING:
298 return "BT_FIELD_CLASS_ID_STRING";
299 case BT_FIELD_CLASS_ID_STRUCTURE:
300 return "BT_FIELD_CLASS_ID_STRUCTURE";
301 case BT_FIELD_CLASS_ID_STATIC_ARRAY:
302 return "BT_FIELD_CLASS_ID_STATIC_ARRAY";
303 case BT_FIELD_CLASS_ID_DYNAMIC_ARRAY:
304 return "BT_FIELD_CLASS_ID_DYNAMIC_ARRAY";
305 case BT_FIELD_CLASS_ID_VARIANT:
306 return "BT_FIELD_CLASS_ID_VARIANT";
3dca2276
PP
307 default:
308 return "(unknown)";
309 }
310};
311
312static inline
5cd6d0e5 313const char *bt_common_field_class_integer_preferred_display_base_string(enum bt_field_class_integer_preferred_display_base base)
3dca2276
PP
314{
315 switch (base) {
5cd6d0e5
PP
316 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY:
317 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_BINARY";
318 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL:
319 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_OCTAL";
320 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL:
321 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_DECIMAL";
322 case BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL:
323 return "BT_FIELD_CLASS_INTEGER_PREFERRED_DISPLAY_BASE_HEXADECIMAL";
3dca2276
PP
324 default:
325 return "(unknown)";
326 }
327}
328
329static inline
330const char *bt_common_scope_string(enum bt_scope scope)
331{
332 switch (scope) {
44c440bc
PP
333 case BT_SCOPE_PACKET_HEADER:
334 return "BT_SCOPE_PACKET_HEADER";
335 case BT_SCOPE_PACKET_CONTEXT:
336 return "BT_SCOPE_PACKET_CONTEXT";
337 case BT_SCOPE_EVENT_HEADER:
338 return "BT_SCOPE_EVENT_HEADER";
339 case BT_SCOPE_EVENT_COMMON_CONTEXT:
340 return "BT_SCOPE_EVENT_COMMON_CONTEXT";
341 case BT_SCOPE_EVENT_SPECIFIC_CONTEXT:
342 return "BT_SCOPE_EVENT_SPECIFIC_CONTEXT";
3dca2276
PP
343 case BT_SCOPE_EVENT_PAYLOAD:
344 return "BT_SCOPE_EVENT_PAYLOAD";
3dca2276
PP
345 default:
346 return "(unknown)";
347 }
348}
349
350static inline
351const char *bt_common_event_class_log_level_string(
352 enum bt_event_class_log_level level)
353{
354 switch (level) {
3dca2276
PP
355 case BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY:
356 return "BT_EVENT_CLASS_LOG_LEVEL_EMERGENCY";
357 case BT_EVENT_CLASS_LOG_LEVEL_ALERT:
358 return "BT_EVENT_CLASS_LOG_LEVEL_ALERT";
359 case BT_EVENT_CLASS_LOG_LEVEL_CRITICAL:
360 return "BT_EVENT_CLASS_LOG_LEVEL_CRITICAL";
361 case BT_EVENT_CLASS_LOG_LEVEL_ERROR:
362 return "BT_EVENT_CLASS_LOG_LEVEL_ERROR";
363 case BT_EVENT_CLASS_LOG_LEVEL_WARNING:
364 return "BT_EVENT_CLASS_LOG_LEVEL_WARNING";
365 case BT_EVENT_CLASS_LOG_LEVEL_NOTICE:
366 return "BT_EVENT_CLASS_LOG_LEVEL_NOTICE";
367 case BT_EVENT_CLASS_LOG_LEVEL_INFO:
368 return "BT_EVENT_CLASS_LOG_LEVEL_INFO";
369 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM:
370 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_SYSTEM";
371 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM:
372 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROGRAM";
373 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS:
374 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_PROCESS";
375 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE:
376 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_MODULE";
377 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT:
378 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_UNIT";
379 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION:
380 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_FUNCTION";
381 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE:
382 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG_LINE";
383 case BT_EVENT_CLASS_LOG_LEVEL_DEBUG:
384 return "BT_EVENT_CLASS_LOG_LEVEL_DEBUG";
385 default:
386 return "(unknown)";
387 }
388};
389
390static inline
391GString *bt_field_path_string(struct bt_field_path *path)
392{
393 GString *str = g_string_new(NULL);
44c440bc 394 uint64_t i;
3dca2276
PP
395
396 BT_ASSERT(path);
397
398 if (!str) {
399 goto end;
400 }
401
402 g_string_append_printf(str, "[%s", bt_common_scope_string(
403 bt_field_path_get_root_scope(path)));
404
405 for (i = 0; i < bt_field_path_get_index_count(path); i++) {
44c440bc
PP
406 g_string_append_printf(str, ", %" PRIu64,
407 bt_field_path_get_index_by_index(path, i));
3dca2276
PP
408 }
409
410 g_string_append(str, "]");
411
412end:
413 return str;
414}
415
1670bffd 416#endif /* BABELTRACE_COMMON_INTERNAL_H */
This page took 0.049794 seconds and 4 git commands to generate.