blob: 1495374e4d3b7284df893f34e4c275ee3a555024 [file] [log] [blame]
Imre Kis2ccd8e82021-10-08 11:21:14 +02001/* 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
Gabor Tothed8305b2024-09-24 12:47:26 +020011#ifdef EXPORT_PUBLIC_INTERFACE_TRACE
12#define TRACE_EXPORTED __attribute__((__visibility__("default")))
13#else
14#define TRACE_EXPORTED
15#endif
16
Imre Kis2ccd8e82021-10-08 11:21:14 +020017#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
Gabor Toth193f6dd2024-09-27 11:49:49 +020023#error "Trace level is not defined!"
Imre Kis2ccd8e82021-10-08 11:21:14 +020024#endif /* TRACE_LEVEL */
25
26/**
27 * no_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_trace_printf(func, line, level, fmt, ...) \
31 do { \
32 if (0) { \
33 trace_printf(func, line, level, fmt, ##__VA_ARGS__); \
34 } \
35 } while (0)
36
Gabor Ambrus7ccab792023-08-14 22:56:56 +020037extern void (*trace_puts_interface)(const char *str);
Imre Kis2ccd8e82021-10-08 11:21:14 +020038void trace_puts(const char *str);
Gabor Tothed8305b2024-09-24 12:47:26 +020039TRACE_EXPORTED
Imre Kis2ccd8e82021-10-08 11:21:14 +020040void 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(...) trace_printf(__func__, __LINE__, TRACE_LEVEL_ERROR, __VA_ARGS__)
44#else
45#define EMSG(...) no_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(...) trace_printf(__func__, __LINE__, TRACE_LEVEL_INFO, __VA_ARGS__)
50#else
51#define IMSG(...) no_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(...) trace_printf(__func__, __LINE__, TRACE_LEVEL_DEBUG, __VA_ARGS__)
56#else
57#define DMSG(...) no_trace_printf(__func__, __LINE__, TRACE_LEVEL_DEBUG, __VA_ARGS__)
58#endif /* TRACE_LEVEL >= TRACE_LEVEL_DEBUG */
59
60#endif /* TRACE_H_ */