35d30034a55c45e6cfaf28697467863278b7fad4
[lwext4.git] / lwext4 / ext4_debug.h
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 <stdio.h>
53 #include <inttypes.h>
54
55 #ifndef PRIu64
56 #define PRIu64 "llu"
57 #endif
58
59 #ifndef PRId64
60 #define PRId64 "lld"
61 #endif
62
63
64 #define DEBUG_BALLOC (1ul << 0)
65 #define DEBUG_BCACHE (1ul << 1)
66 #define DEBUG_BITMAP (1ul << 2)
67 #define DEBUG_BLOCK_GROUP (1ul << 3)
68 #define DEBUG_BLOCKDEV (1ul << 4)
69 #define DEBUG_DIR_IDX (1ul << 5)
70 #define DEBUG_DIR (1ul << 6)
71 #define DEBUG_EXTENT (1ul << 7)
72 #define DEBUG_FS (1ul << 8)
73 #define DEBUG_HASH (1ul << 9)
74 #define DEBUG_IALLOC (1ul << 10)
75 #define DEBUG_INODE (1ul << 11)
76 #define DEBUG_SUPER (1ul << 12)
77 #define DEBUG_XATTR (1ul << 13)
78 #define DEBUG_MKFS (1ul << 14)
79 #define DEBUG_EXT4 (1ul << 15)
80 #define DEBUG_JBD (1ul << 16)
81 #define DEBUG_MBR (1ul << 17)
82
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  * @brief   m new debug mask.*/
134 void ext4_dmask_set(uint32_t m);
135
136 /**@brief   Global mask debug clear.
137  * @brief   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 /**@brief   Debug printf.*/
146 #define ext4_dbg(m, ...)                                                       \
147         do {                                                                   \
148                 if (m & ext4_dmask_get()) {                                    \
149                         if (CONFIG_DEBUG_PREFIX) {                             \
150                                 printf("%s", ext4_dmask_id2str(m));            \
151                                 printf("l: %d   ", __LINE__);                  \
152                         }                                                      \
153                         printf(__VA_ARGS__);                                   \
154                         fflush(stdout);                                        \
155                 }                                                              \
156         } while (0)
157 #else
158 #define ext4_dbg(m, ...) do { } while (0)
159 #endif
160
161 #if CONFIG_DEBUG_ASSERT
162 /**@brief   Debug assertion.*/
163 #if CONFIG_HAVE_OWN_ASSERT
164 #define ext4_assert(_v)                                                        \
165         do {                                                                   \
166                 if (!(_v)) {                                                   \
167                         printf("assertion failed:\nfile: %s\nline: %d\n",      \
168                                __FILE__, __LINE__);                            \
169                                while (1)                                       \
170                                        ;                                       \
171                 }                                                              \
172         } while (0)
173 #else
174 #define ext4_assert(_v) assert(_v)
175 #endif
176 #else
177 #define ext4_assert(_v)
178 #endif
179
180 #ifdef __cplusplus
181 }
182 #endif
183
184 #endif /* EXT4_DEBUG_H_ */
185
186 /**
187  * @}
188  */