1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 
25 #ifndef TRUSTY_UTIL_H_
26 #define TRUSTY_UTIL_H_
27 
28 #include <trusty/sysdeps.h>
29 
30 /* Returns the basename of |str|. This is defined as the last path
31  * component, assuming the normal POSIX separator '/'. If there are no
32  * separators, returns |str|.
33  */
34 const char* trusty_basename(const char* str);
35 
36 #define TRUSTY_STRINGIFY(x) #x
37 #define TRUSTY_TO_STRING(x) TRUSTY_STRINGIFY(x)
38 
39 /*
40  * Aborts the program if @expr is false.
41  *
42  * This has no effect unless TIPC_ENABLE_DEBUG is defined.
43  */
44 #ifdef TIPC_ENABLE_DEBUG
45 #define trusty_assert(expr)                           \
46     do {                                              \
47         if (!(expr)) {                                \
48             trusty_fatal("assert fail: " #expr "\n"); \
49         }                                             \
50     } while (0)
51 #else
52 #define trusty_assert(expr)
53 #endif
54 
55 /*
56  * Prints debug message.
57  *
58  * This has no effect unless TIPC_ENABLE_DEBUG and LOCAL_LOG is defined.
59  */
60 #ifdef TIPC_ENABLE_DEBUG
61 #define trusty_debug(message, ...)                                    \
62     do {                                                              \
63         if (LOCAL_LOG) {                                              \
64             trusty_printf(trusty_basename(__FILE__));                 \
65             trusty_printf(":" TRUSTY_TO_STRING(__LINE__) ": DEBUG "); \
66             trusty_printf(message, ##__VA_ARGS__);                    \
67         }                                                             \
68     } while (0)
69 #else
70 #define trusty_debug(message, ...)
71 #endif
72 
73 /*
74  * Prints info message.
75  */
76 #define trusty_info(message, ...)                 \
77     do {                                          \
78         trusty_printf(trusty_basename(__FILE__)); \
79         trusty_printf(": INFO ");                 \
80         trusty_printf(message, ##__VA_ARGS__);    \
81     } while (0)
82 
83 /*
84  * Prints error message.
85  */
86 #define trusty_error(message, ...)                                \
87     do {                                                          \
88         trusty_printf(trusty_basename(__FILE__));                 \
89         trusty_printf(":" TRUSTY_TO_STRING(__LINE__) ": ERROR "); \
90         trusty_printf(message, ##__VA_ARGS__);                    \
91     } while (0)
92 
93 /*
94  * Prints message and calls trusty_abort.
95  */
96 #define trusty_fatal(message, ...)                                \
97     do {                                                          \
98         trusty_printf(trusty_basename(__FILE__));                 \
99         trusty_printf(":" TRUSTY_TO_STRING(__LINE__) ": FATAL "); \
100         trusty_printf(message, ##__VA_ARGS__);                    \
101         trusty_abort();                                           \
102     } while (0)
103 
104 #endif /* TRUSTY_UTIL_H_ */
105