1 /* SPDX-License-Identifier: BSD-3-Clause */
2 /*
3  * Copyright (c) 2021, Arm Limited and Contributors. All rights reserved.
4  */
5 
6 #ifndef TRACE_H_
7 #define TRACE_H_
8 
9 #include "compiler.h"
10 
11 #ifdef EXPORT_PUBLIC_INTERFACE_TRACE
12 #define TRACE_EXPORTED __attribute__((__visibility__("default")))
13 #else
14 #define TRACE_EXPORTED
15 #endif
16 
17 #define TRACE_LEVEL_NONE	(0)
18 #define TRACE_LEVEL_ERROR	(1)
19 #define TRACE_LEVEL_INFO	(2)
20 #define TRACE_LEVEL_DEBUG	(3)
21 
22 #ifndef TRACE_LEVEL
23 #error "Trace level is not defined!"
24 #endif /* TRACE_LEVEL */
25 
26 /**
27  * no_ts_trace_printf will be optimized out becase of the 'if (0)' but all the
28  * checks will still run against the format string and the parameters.
29  */
30 #define no_ts_trace_printf(func, line, level, fmt, ...)				\
31 	do {									\
32 		if (0) {							\
33 			ts_trace_printf(func, line, level, fmt, ##__VA_ARGS__);	\
34 		}								\
35 	} while (0)
36 
37 extern void (*trace_puts_interface)(const char *str);
38 void trace_puts(const char *str);
39 TRACE_EXPORTED
40 void ts_trace_printf(const char *func, int line, int level, const char *fmt, ...) __printf(4, 5);
41 
42 #if TRACE_LEVEL >= TRACE_LEVEL_ERROR
43 #define EMSG(...)	ts_trace_printf(__func__, __LINE__, TRACE_LEVEL_ERROR, __VA_ARGS__)
44 #else
45 #define EMSG(...)	no_ts_trace_printf(__func__, __LINE__, TRACE_LEVEL_ERROR, __VA_ARGS__)
46 #endif /* TRACE_LEVEL >= TRACE_LEVEL_ERROR */
47 
48 #if TRACE_LEVEL >= TRACE_LEVEL_INFO
49 #define IMSG(...)	ts_trace_printf(__func__, __LINE__, TRACE_LEVEL_INFO, __VA_ARGS__)
50 #else
51 #define IMSG(...)	no_ts_trace_printf(__func__, __LINE__, TRACE_LEVEL_INFO, __VA_ARGS__)
52 #endif /* TRACE_LEVEL >= TRACE_LEVEL_INFO */
53 
54 #if TRACE_LEVEL >= TRACE_LEVEL_DEBUG
55 #define DMSG(...)	ts_trace_printf(__func__, __LINE__, TRACE_LEVEL_DEBUG, __VA_ARGS__)
56 #else
57 #define DMSG(...)	no_ts_trace_printf(__func__, __LINE__, TRACE_LEVEL_DEBUG, __VA_ARGS__)
58 #endif /* TRACE_LEVEL >= TRACE_LEVEL_DEBUG */
59 
60 #endif /* TRACE_H_ */
61