VLC 4.0.0-dev
Loading...
Searching...
No Matches
vlc_tracer.h
Go to the documentation of this file.
1/*****************************************************************************
2 * vlc_tracer.h: tracing interface
3 * This library provides basic functions for threads to interact with user
4 * interface, such as trace output.
5 *****************************************************************************
6 * Copyright (C) 2021 VLC authors and VideoLAN
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU Lesser General Public License as published by
10 * the Free Software Foundation; either version 2.1 of the License, or
11 * (at your option) any later version.
12 *
13 * This program 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
16 * GNU Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 *****************************************************************************/
22
23#ifndef VLC_TRACES_H
24#define VLC_TRACES_H
25
26#include <stdarg.h>
27
28#include <vlc_common.h>
29#include <vlc_threads.h>
30
31/**
32 * \defgroup tracer Tracer module and API
33 * \ingroup os
34 *
35 * Functions for modules to emit traces.
36 *
37 * @{
38 * \file
39 * Tracing functions
40 *
41 * \defgroup tracer_module Tracer Module implementation
42 * \ingroup tracer
43 *
44 * @{
45 */
46
47/**
48 * Trace message values
49 */
58typedef union
60 int64_t integer;
61 double double_;
62 const char *string;
63 uint64_t uinteger;
65
66/**
67 * Trace message
68 */
71 const char *key; /**< Key to identify the value */
72 vlc_tracer_value_t value; /**< Trace value */
73 enum vlc_tracer_value type; /**< Type of the value */
74};
75
76struct vlc_tracer;
77
78/**
79 * Trace record containing the key-values from the trace.
80 */
83 /**
84 * Defines the list of key-value in the trace. The entries must link
85 * towards an array terminated by a VLC_TRACE_END entry, which remains
86 * valid as long as the trace itself should remain valid.
87 **/
88 const struct vlc_tracer_entry *entries;
89};
90
91
92/**
93 * Tracer operations returned by the module probe function
94 */
97 /**
98 * Called when tracing data
99 *
100 * \param sys data pointer set by vlc_tracer_open_cb()
101 * \param ts timestamp of the trace (based on vlc_tick_now())
102 * \param entries can only be \ref vlc_tracer_entry and the va-args list
103 * should be ended by a \ref vlc_tracer_entry with a NULL key.
104 */
105 void (*trace)(void *sys, vlc_tick_t ts, const struct vlc_tracer_trace *trace);
107 /**
108 * Called to clean module specific resources
109 *
110 * \param sys data pointer set by vlc_tracer_open_cb()
111 */
112 void (*destroy)(void *sys);
114
115/**
116 * Module probe/open function signature
117 *
118 * \param obj a valid object
119 * \param[out] sysp to module specific data
120 * \return the operations implemented by the module or NULL in case of error
121 * */
122typedef struct vlc_tracer_operations *(*vlc_tracer_open_cb)(vlc_object_t *obj,
123 void **restrict sysp);
124
125/**
126 * @}
127 *
128 * \defgroup tracer_api Tracer API
129 * \ingroup tracer
130 *
131 * @{
132 */
133
134/**
135 * Create a tracer object
136 *
137 * \note This function is for advanced debugging/testing.
138 * Use vlc_object_get_tracer() to get the existing tracer.
139 *
140 * \param parent parent object used to create the tracer
141 * \param name module to load or NULL for the default one
142 * \return a valid tracer or NULL in case of error
143 */
145 const char *name);
146
147/**
148 * Destroy a tracer object
149 */
150VLC_API void vlc_tracer_Destroy(struct vlc_tracer *tracer);
151
152/**
153 * Emit traces
154 *
155 * \param tracer tracer emitting the traces
156 * \param ts timestamp of the current trace
157 * \param trace the trace to register with its list of key / value parameters.
158 * Key must be a not NULL string. Value has to be defined with
159 * one of the type defined in the \ref vlc_tracer_entry union.
160 */
161
163 const struct vlc_tracer_trace *trace);
164#define vlc_tracer_TraceWithTs(tracer, ts, ...) \
165 (vlc_tracer_TraceWithTs)(tracer, ts, &(const struct vlc_tracer_trace) { \
166 .entries = (const struct vlc_tracer_entry[]) { \
167 __VA_ARGS__ \
168 } \
169 })
170
171#define vlc_tracer_Trace(tracer, ...) \
172 vlc_tracer_TraceWithTs(tracer, vlc_tick_now(), __VA_ARGS__)
173
174static inline struct vlc_tracer_entry vlc_tracer_entry_FromInt(const char *key, int64_t value)
176 vlc_tracer_value_t tracer_value;
177 tracer_value.integer = value;
178 struct vlc_tracer_entry trace = { key, tracer_value, VLC_TRACER_INT };
179 return trace;
180}
181
182static inline struct vlc_tracer_entry vlc_tracer_entry_FromUnsigned(const char *key, uint64_t value)
184 vlc_tracer_value_t tracer_value;
185 tracer_value.uinteger = value;
186 struct vlc_tracer_entry trace = { key, tracer_value, VLC_TRACER_UINT };
187 return trace;
188}
189
190static inline struct vlc_tracer_entry vlc_tracer_entry_FromDouble(const char *key, double value)
192 vlc_tracer_value_t tracer_value = {
193 .double_ = value,
194 };
195 struct vlc_tracer_entry trace = { key, tracer_value, VLC_TRACER_DOUBLE };
196 return trace;
197}
198
199static inline struct vlc_tracer_entry vlc_tracer_entry_FromString(const char *key, const char *value)
201 vlc_tracer_value_t tracer_value;
202 tracer_value.string = value;
203 struct vlc_tracer_entry trace = { key, tracer_value, VLC_TRACER_STRING };
204 return trace;
205}
206
207#ifndef __cplusplus
208#define VLC_TRACE_END \
209 vlc_tracer_entry_FromString(NULL, NULL)
210
211#define VLC_TRACE(key, value) \
212 _Generic((value), \
213 uint64_t: vlc_tracer_entry_FromUnsigned, \
214 int64_t: vlc_tracer_entry_FromInt, \
215 double: vlc_tracer_entry_FromDouble, \
216 char *: vlc_tracer_entry_FromString, \
217 const char *: vlc_tracer_entry_FromString) (key, value)
218#else
219#define VLC_TRACE_END \
220 vlc_tracer_entry_FromString(nullptr, nullptr)
221
222static inline struct vlc_tracer_entry VLC_TRACE(const char *key, int64_t value)
223{
225}
226
227static inline struct vlc_tracer_entry VLC_TRACE(const char *key, uint64_t value)
228{
230}
231
232static inline struct vlc_tracer_entry VLC_TRACE(const char *key, char *value)
233{
235}
236
237static inline struct vlc_tracer_entry VLC_TRACE(const char *key, const char *value)
238{
240}
241#endif
242
243#define VLC_TRACE_TICK_NS(key, tick) VLC_TRACE((key), NS_FROM_VLC_TICK((tick)))
245#ifdef __cplusplus
246#undef vlc_tracer_TraceWithTs
247#undef vlc_tracer_Trace
248
249template<typename... Entries>
250static inline void vlc_tracer_TraceWithTs(struct vlc_tracer *tracer,
251 vlc_tick_t ts,
252 struct vlc_tracer_entry first,
253 Entries... rest)
254{
255 const struct vlc_tracer_entry arr[] = { first, rest... };
256 const struct vlc_tracer_trace trace = { arr };
257 vlc_tracer_TraceWithTs(tracer, ts, &trace);
258}
259
260template<typename... Entries>
261static inline void vlc_tracer_Trace(struct vlc_tracer *tracer,
262 struct vlc_tracer_entry first,
263 Entries... rest)
264{
265 vlc_tracer_TraceWithTs(tracer, vlc_tick_now(), first, rest...);
266}
267#endif
268
269/**
270 * @}
271 *
272 * \defgroup tracer_helper Tracer helper functions
273 * \ingroup tracer
274 *
275 * @{
276 */
277
278static inline void vlc_tracer_TraceStreamPTS(struct vlc_tracer *tracer, const char *type,
279 const char *id, const char* stream,
280 vlc_tick_t pts)
281{
282 vlc_tracer_Trace(tracer, VLC_TRACE("type", type),
283 VLC_TRACE("id", id),
284 VLC_TRACE("stream", stream),
285 VLC_TRACE_TICK_NS("pts", pts),
287}
288
289static inline void vlc_tracer_TraceStreamDTS(struct vlc_tracer *tracer, const char *type,
290 const char *id, const char* stream,
291 vlc_tick_t pts, vlc_tick_t dts)
292{
293 vlc_tracer_Trace(tracer, VLC_TRACE("type", type),
294 VLC_TRACE("id", id),
295 VLC_TRACE("stream", stream),
296 VLC_TRACE_TICK_NS("pts", pts),
297 VLC_TRACE_TICK_NS("dts", dts),
299}
300
301static inline void vlc_tracer_TraceEvent(struct vlc_tracer *tracer, const char *type,
302 const char *id, const char *event)
303{
304 vlc_tracer_Trace(tracer, VLC_TRACE("type", type),
305 VLC_TRACE("id", id),
306 VLC_TRACE("event", event),
308}
309
310static inline void vlc_tracer_TracePCR( struct vlc_tracer *tracer, const char *type,
311 const char *id, vlc_tick_t pcr)
312{
313 vlc_tracer_Trace(tracer, VLC_TRACE("type", type),
314 VLC_TRACE("id", id),
315 VLC_TRACE_TICK_NS("pcr", pcr),
317}
318
319/**
320 * @}
321 */
322/**
323 * @}
324 */
325#endif
#define VLC_API
Definition fourcc_gen.c:31
vlc_tick_t vlc_tick_now(void)
Precision monotonic clock.
Definition thread.c:253
#define VLC_TRACE(key, value)
Definition vlc_tracer.h:212
#define VLC_TRACE_TICK_NS(key, tick)
Definition vlc_tracer.h:244
struct vlc_tracer * vlc_tracer_Create(vlc_object_t *parent, const char *name)
Create a tracer object.
Definition tracer.c:71
void vlc_tracer_Destroy(struct vlc_tracer *tracer)
Destroy a tracer object.
Definition tracer.c:88
static struct vlc_tracer_entry vlc_tracer_entry_FromDouble(const char *key, double value)
Definition vlc_tracer.h:191
static struct vlc_tracer_entry vlc_tracer_entry_FromUnsigned(const char *key, uint64_t value)
Definition vlc_tracer.h:183
#define VLC_TRACE_END
Definition vlc_tracer.h:209
#define vlc_tracer_TraceWithTs(tracer, ts,...)
Definition vlc_tracer.h:165
static struct vlc_tracer_entry vlc_tracer_entry_FromInt(const char *key, int64_t value)
Definition vlc_tracer.h:175
static struct vlc_tracer_entry vlc_tracer_entry_FromString(const char *key, const char *value)
Definition vlc_tracer.h:200
#define vlc_tracer_Trace(tracer,...)
Definition vlc_tracer.h:172
static void vlc_tracer_TraceStreamPTS(struct vlc_tracer *tracer, const char *type, const char *id, const char *stream, vlc_tick_t pts)
Definition vlc_tracer.h:279
static void vlc_tracer_TracePCR(struct vlc_tracer *tracer, const char *type, const char *id, vlc_tick_t pcr)
Definition vlc_tracer.h:311
static void vlc_tracer_TraceStreamDTS(struct vlc_tracer *tracer, const char *type, const char *id, const char *stream, vlc_tick_t pts, vlc_tick_t dts)
Definition vlc_tracer.h:290
static void vlc_tracer_TraceEvent(struct vlc_tracer *tracer, const char *type, const char *id, const char *event)
Definition vlc_tracer.h:302
vlc_tracer_value
Trace message values.
Definition vlc_tracer.h:52
@ VLC_TRACER_STRING
Definition vlc_tracer.h:55
@ VLC_TRACER_UINT
Definition vlc_tracer.h:56
@ VLC_TRACER_INT
Definition vlc_tracer.h:53
@ VLC_TRACER_DOUBLE
Definition vlc_tracer.h:54
const char name[16]
Definition httpd.c:1299
VLC object common members.
Definition vlc_objects.h:53
Trace message.
Definition vlc_tracer.h:71
vlc_tracer_value_t value
Trace value.
Definition vlc_tracer.h:73
const char * key
Key to identify the value.
Definition vlc_tracer.h:72
enum vlc_tracer_value type
Type of the value.
Definition vlc_tracer.h:74
Tracer operations returned by the module probe function.
Definition vlc_tracer.h:97
void(* destroy)(void *sys)
Called to clean module specific resources.
Definition vlc_tracer.h:113
void(* trace)(void *sys, vlc_tick_t ts, const struct vlc_tracer_trace *trace)
Called when tracing data.
Definition vlc_tracer.h:106
Trace record containing the key-values from the trace.
Definition vlc_tracer.h:83
const struct vlc_tracer_entry * entries
Defines the list of key-value in the trace.
Definition vlc_tracer.h:89
Definition tracer.c:36
Definition vlc_tracer.h:60
const char * string
Definition vlc_tracer.h:63
uint64_t uinteger
Definition vlc_tracer.h:64
double double_
Definition vlc_tracer.h:62
int64_t integer
Definition vlc_tracer.h:61
This file is a collection of common definitions and types.
Thread primitive declarations.
int64_t vlc_tick_t
High precision date or time interval.
Definition vlc_tick.h:48