ext4_journal: journal write skeleton code part 2.
[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_NOPREFIX (1ul << 31)
84 #define DEBUG_ALL (0xFFFFFFFF)
85
86 static inline const char *ext4_dmask_id2str(uint32_t m)
87 {
88         switch(m) {
89         case DEBUG_BALLOC:
90                 return "ext4_balloc: ";
91         case DEBUG_BCACHE:
92                 return "ext4_bcache: ";
93         case DEBUG_BITMAP:
94                 return "ext4_bitmap: ";
95         case DEBUG_BLOCK_GROUP:
96                 return "ext4_block_group: ";
97         case DEBUG_BLOCKDEV:
98                 return "ext4_blockdev: ";
99         case DEBUG_DIR_IDX:
100                 return "ext4_dir_idx: ";
101         case DEBUG_DIR:
102                 return "ext4_dir: ";
103         case DEBUG_EXTENT:
104                 return "ext4_extent: ";
105         case DEBUG_FS:
106                 return "ext4_fs: ";
107         case DEBUG_HASH:
108                 return "ext4_hash: ";
109         case DEBUG_IALLOC:
110                 return "ext4_ialloc: ";
111         case DEBUG_INODE:
112                 return "ext4_inode: ";
113         case DEBUG_SUPER:
114                 return "ext4_super: ";
115         case DEBUG_XATTR:
116                 return "ext4_xattr: ";
117         case DEBUG_MKFS:
118                 return "ext4_mkfs: ";
119         case DEBUG_JBD:
120                 return "ext4_jbd: ";
121         case DEBUG_MBR:
122                 return "ext4_mbr: ";
123         case DEBUG_EXT4:
124                 return "ext4: ";
125         }
126         return "";
127 }
128 #define DBG_NONE  ""
129 #define DBG_INFO  "[info]  "
130 #define DBG_WARN  "[warn]  "
131 #define DBG_ERROR "[error] "
132
133 /**@brief   Global mask debug set.
134  * @brief   m new debug mask.*/
135 void ext4_dmask_set(uint32_t m);
136
137 /**@brief   Global mask debug clear.
138  * @brief   m new debug mask.*/
139 void ext4_dmask_clr(uint32_t m);
140
141 /**@brief   Global debug mask get.
142  * @return  debug mask*/
143 uint32_t ext4_dmask_get(void);
144
145 #if CONFIG_DEBUG_PRINTF
146 /**@brief   Debug printf.*/
147 #define ext4_dbg(m, ...)                                                       \
148         do {                                                                   \
149                 if ((m) & ext4_dmask_get()) {                                  \
150                         if (!((m) & DEBUG_NOPREFIX)) {                         \
151                                 printf("%s", ext4_dmask_id2str(m));            \
152                                 printf("l: %d   ", __LINE__);                  \
153                         }                                                      \
154                         printf(__VA_ARGS__);                                   \
155                         fflush(stdout);                                        \
156                 }                                                              \
157         } while (0)
158 #else
159 #define ext4_dbg(m, ...) do { } while (0)
160 #endif
161
162 #if CONFIG_DEBUG_ASSERT
163 /**@brief   Debug assertion.*/
164 #if CONFIG_HAVE_OWN_ASSERT
165 #define ext4_assert(_v)                                                        \
166         do {                                                                   \
167                 if (!(_v)) {                                                   \
168                         printf("assertion failed:\nfile: %s\nline: %d\n",      \
169                                __FILE__, __LINE__);                            \
170                                while (1)                                       \
171                                        ;                                       \
172                 }                                                              \
173         } while (0)
174 #else
175 #define ext4_assert(_v) assert(_v)
176 #endif
177 #else
178 #define ext4_assert(_v)
179 #endif
180
181 #ifdef __cplusplus
182 }
183 #endif
184
185 #endif /* EXT4_DEBUG_H_ */
186
187 /**
188  * @}
189  */