1 | /* |
2 | * Copyright (c) 2013 Grzegorz Kostka (kostka.grzegorz@gmail.com) |
3 | * All rights reserved. |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
28 | |
29 | /** @addtogroup lwext4 |
30 | * @{ |
31 | */ |
32 | /** |
33 | * @file ext4_debug.c |
34 | * @brief Debug printf and assert macros. |
35 | */ |
36 | |
37 | #ifndef EXT4_DEBUG_H_ |
38 | #define EXT4_DEBUG_H_ |
39 | |
40 | #ifdef __cplusplus |
41 | extern "C" { |
42 | #endif |
43 | |
44 | #include <ext4_config.h> |
45 | #include <ext4_errno.h> |
46 | |
47 | #if !CONFIG_HAVE_OWN_ASSERT |
48 | #include <assert.h> |
49 | #endif |
50 | |
51 | #include <stdint.h> |
52 | #include <inttypes.h> |
53 | |
54 | #ifndef PRIu64 |
55 | #define PRIu64 "llu" |
56 | #endif |
57 | |
58 | #ifndef PRId64 |
59 | #define PRId64 "lld" |
60 | #endif |
61 | |
62 | |
63 | #define DEBUG_BALLOC (1ul << 0) |
64 | #define DEBUG_BCACHE (1ul << 1) |
65 | #define DEBUG_BITMAP (1ul << 2) |
66 | #define DEBUG_BLOCK_GROUP (1ul << 3) |
67 | #define DEBUG_BLOCKDEV (1ul << 4) |
68 | #define DEBUG_DIR_IDX (1ul << 5) |
69 | #define DEBUG_DIR (1ul << 6) |
70 | #define DEBUG_EXTENT (1ul << 7) |
71 | #define DEBUG_FS (1ul << 8) |
72 | #define DEBUG_HASH (1ul << 9) |
73 | #define DEBUG_IALLOC (1ul << 10) |
74 | #define DEBUG_INODE (1ul << 11) |
75 | #define DEBUG_SUPER (1ul << 12) |
76 | #define DEBUG_XATTR (1ul << 13) |
77 | #define DEBUG_MKFS (1ul << 14) |
78 | #define DEBUG_EXT4 (1ul << 15) |
79 | #define DEBUG_JBD (1ul << 16) |
80 | #define DEBUG_MBR (1ul << 17) |
81 | |
82 | #define DEBUG_NOPREFIX (1ul << 31) |
83 | #define DEBUG_ALL (0xFFFFFFFF) |
84 | |
85 | static inline const char *ext4_dmask_id2str(uint32_t m) |
86 | { |
87 | switch(m) { |
88 | case DEBUG_BALLOC: |
89 | return "ext4_balloc: " ; |
90 | case DEBUG_BCACHE: |
91 | return "ext4_bcache: " ; |
92 | case DEBUG_BITMAP: |
93 | return "ext4_bitmap: " ; |
94 | case DEBUG_BLOCK_GROUP: |
95 | return "ext4_block_group: " ; |
96 | case DEBUG_BLOCKDEV: |
97 | return "ext4_blockdev: " ; |
98 | case DEBUG_DIR_IDX: |
99 | return "ext4_dir_idx: " ; |
100 | case DEBUG_DIR: |
101 | return "ext4_dir: " ; |
102 | case DEBUG_EXTENT: |
103 | return "ext4_extent: " ; |
104 | case DEBUG_FS: |
105 | return "ext4_fs: " ; |
106 | case DEBUG_HASH: |
107 | return "ext4_hash: " ; |
108 | case DEBUG_IALLOC: |
109 | return "ext4_ialloc: " ; |
110 | case DEBUG_INODE: |
111 | return "ext4_inode: " ; |
112 | case DEBUG_SUPER: |
113 | return "ext4_super: " ; |
114 | case DEBUG_XATTR: |
115 | return "ext4_xattr: " ; |
116 | case DEBUG_MKFS: |
117 | return "ext4_mkfs: " ; |
118 | case DEBUG_JBD: |
119 | return "ext4_jbd: " ; |
120 | case DEBUG_MBR: |
121 | return "ext4_mbr: " ; |
122 | case DEBUG_EXT4: |
123 | return "ext4: " ; |
124 | } |
125 | return "" ; |
126 | } |
127 | #define DBG_NONE "" |
128 | #define DBG_INFO "[info] " |
129 | #define DBG_WARN "[warn] " |
130 | #define DBG_ERROR "[error] " |
131 | |
132 | /**@brief Global mask debug set. |
133 | * @param m new debug mask.*/ |
134 | void ext4_dmask_set(uint32_t m); |
135 | |
136 | /**@brief Global mask debug clear. |
137 | * @param m new debug mask.*/ |
138 | void ext4_dmask_clr(uint32_t m); |
139 | |
140 | /**@brief Global debug mask get. |
141 | * @return debug mask*/ |
142 | uint32_t ext4_dmask_get(void); |
143 | |
144 | #if CONFIG_DEBUG_PRINTF |
145 | #include <stdio.h> |
146 | |
147 | /**@brief Debug printf.*/ |
148 | #define ext4_dbg(m, ...) \ |
149 | do { \ |
150 | if ((m) & ext4_dmask_get()) { \ |
151 | if (!((m) & DEBUG_NOPREFIX)) { \ |
152 | printf("%s", ext4_dmask_id2str(m)); \ |
153 | printf("l: %d ", __LINE__); \ |
154 | } \ |
155 | printf(__VA_ARGS__); \ |
156 | fflush(stdout); \ |
157 | } \ |
158 | } while (0) |
159 | #else |
160 | #define ext4_dbg(m, ...) do { } while (0) |
161 | #endif |
162 | |
163 | #if CONFIG_DEBUG_ASSERT |
164 | /**@brief Debug assertion.*/ |
165 | #if CONFIG_HAVE_OWN_ASSERT |
166 | #include <stdio.h> |
167 | |
168 | #define ext4_assert(_v) \ |
169 | do { \ |
170 | if (!(_v)) { \ |
171 | printf("assertion failed:\nfile: %s\nline: %d\n", \ |
172 | __FILE__, __LINE__); \ |
173 | while (1) \ |
174 | ; \ |
175 | } \ |
176 | } while (0) |
177 | #else |
178 | #define ext4_assert(_v) assert(_v) |
179 | #endif |
180 | #else |
181 | #define ext4_assert(_v) ((void)(_v)) |
182 | #endif |
183 | |
184 | #ifdef __cplusplus |
185 | } |
186 | #endif |
187 | |
188 | #endif /* EXT4_DEBUG_H_ */ |
189 | |
190 | /** |
191 | * @} |
192 | */ |
193 | |