blob: 08ad44cf497d2956160c82b7297ca1477cc82839 [file] [log] [blame]
Simon Glass9054a022017-12-04 13:48:20 -07001/*
2 * Logging support
3 *
4 * Copyright (c) 2017 Google, Inc
5 * Written by Simon Glass <sjg@chromium.org>
6 *
7 * SPDX-License-Identifier: GPL-2.0+
8 */
9
10#ifndef __LOG_H
11#define __LOG_H
12
13#ifdef DEBUG
14#define _DEBUG 1
15#else
16#define _DEBUG 0
17#endif
18
19#ifdef CONFIG_SPL_BUILD
20#define _SPL_BUILD 1
21#else
22#define _SPL_BUILD 0
23#endif
24
25/*
26 * Output a debug text when condition "cond" is met. The "cond" should be
27 * computed by a preprocessor in the best case, allowing for the best
28 * optimization.
29 */
30#define debug_cond(cond, fmt, args...) \
31 do { \
32 if (cond) \
33 printf(pr_fmt(fmt), ##args); \
34 } while (0)
35
36/* Show a message if DEBUG is defined in a file */
37#define debug(fmt, args...) \
38 debug_cond(_DEBUG, fmt, ##args)
39
40/* Show a message if not in SPL */
41#define warn_non_spl(fmt, args...) \
42 debug_cond(!_SPL_BUILD, fmt, ##args)
43
44/*
45 * An assertion is run-time check done in debug mode only. If DEBUG is not
46 * defined then it is skipped. If DEBUG is defined and the assertion fails,
47 * then it calls panic*( which may or may not reset/halt U-Boot (see
48 * CONFIG_PANIC_HANG), It is hoped that all failing assertions are found
49 * before release, and after release it is hoped that they don't matter. But
50 * in any case these failing assertions cannot be fixed with a reset (which
51 * may just do the same assertion again).
52 */
53void __assert_fail(const char *assertion, const char *file, unsigned int line,
54 const char *function);
55#define assert(x) \
56 ({ if (!(x) && _DEBUG) \
57 __assert_fail(#x, __FILE__, __LINE__, __func__); })
58
59#endif