• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Institute of Parallel And Distributed Systems (IPADS), Shanghai Jiao Tong University (SJTU)
3  * Licensed under the Mulan PSL v2.
4  * You can use this software according to the terms and conditions of the Mulan PSL v2.
5  * You may obtain a copy of Mulan PSL v2 at:
6  *     http://license.coscl.org.cn/MulanPSL2
7  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8  * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9  * PURPOSE.
10  * See the Mulan PSL v2 for more details.
11  */
12 #ifndef FS_DEBUG_H
13 #define FS_DEBUG_H
14 
15 #include <stdio.h>
16 
17 /*
18  * 0: print nothing
19  * 1: print ext4_srv_dbg_base
20  * 2: print ext4_srv_dbg_base and ext4_srv_dbg
21  */
22 #define FS_DEBUG 0
23 
24 /**
25  * 0: do not print | 1: print
26  */
27 #define FS_DEBUG_TRACING 0
28 
29 #define COLOR_RED       "\033[31m"
30 #define COLOR_GREEN     "\033[32m"
31 #define COLOR_YELLOW    "\033[33m"
32 #define COLOR_YELLOW_BG "\033[33;3m"
33 #define COLOR_DEFAULT   "\033[0m"
34 
35 #define PRINT_HEX_PERLINE 32
36 
37 #define UNUSED(x) ((void)(x))
38 
print_hex(char * buf,int size,int per_line)39 static inline void print_hex(char *buf, int size, int per_line)
40 {
41     int i;
42     for (i = 0; i < size; i++) {
43         if (i % per_line == 0) {
44             printf("\n");
45         }
46         printf("%02x ", buf[i]);
47     }
48     printf("\n");
49 }
50 
51 #define colored_printf(COLOR, fmt, ...) \
52     printf(COLOR "" fmt "" COLOR_DEFAULT, ##__VA_ARGS__)
53 #define colored_printf_with_lineinfo(COLOR, fmt, ...)                        \
54     do {                                                                     \
55         colored_printf(COLOR, "<%s:%s:%d>: ", __FILE__, __func__, __LINE__); \
56         printf(fmt, ##__VA_ARGS__);                                          \
57     } while (0)
58 
59 #if FS_DEBUG == 1
60 #define fs_debug(fmt, ...) \
61     colored_printf_with_lineinfo(COLOR_YELLOW, fmt, ##__VA_ARGS__)
62 #define fs_debug_newline()            printf("\n")
63 #define fs_debug_print_hex(buf, size) print_hex(buf, size, PRINT_HEX_PERLINE)
64 #else
65 #define fs_debug(fmt, ...) \
66     do {                   \
67     } while (0)
68 #define fs_debug_newline() \
69     do {                   \
70     } while (0)
71 #define fs_debug_print_hex(...) \
72     do {                        \
73     } while (0)
74 #endif
75 
76 /* When i-th call fs_debug_pause_times, pause */
77 #define fs_debug_pause_times(i) \
78     do {                        \
79         static int tmp = 0;     \
80         if (++tmp == (i)) {     \
81             while (1)           \
82                 ;               \
83         }                       \
84     } while (0)
85 
86 #define fs_debug_print_once(fmt, ...)       \
87     do {                                    \
88         static bool print_once_flag = true; \
89         if (print_once_flag)                \
90             fs_debug(fmt, ##__VA_ARGS__);   \
91         print_once_flag = false;            \
92     } while (0)
93 
94 #define fs_assert_eq(exp1, exp2)                                            \
95     do {                                                                    \
96         if ((exp1) != (exp2)) {                                             \
97             printf(COLOR_YELLOW "<%s:%s:%d>: %d != %d\n" COLOR_DEFAULT " ", \
98                    __FILE__,                                                \
99                    __func__,                                                \
100                    __LINE__,                                                \
101                    exp1,                                                    \
102                    exp2);                                                   \
103             while (1)                                                       \
104                 ;                                                           \
105         }                                                                   \
106     } while (0)
107 
108 /* Tracing prints */
109 #if FS_DEBUG
110 #define fs_debug_error(fmt, ...) \
111     colored_printf_with_lineinfo(COLOR_RED, fmt, ##__VA_ARGS__)
112 #define fs_debug_warn(fmt, ...) \
113     colored_printf_with_lineinfo(COLOR_YELLOW_BG, fmt, ##__VA_ARGS__)
114 #else
115 #define fs_debug_error(fmt, ...) \
116     do {                         \
117     } while (0)
118 #define fs_debug_warn(fmt, ...) \
119     do {                        \
120     } while (0)
121 #endif
122 
123 #if FS_DEBUG_TRACING
124 #define fs_debug_trace_fswrapper(fmt, ...) \
125     colored_printf_with_lineinfo(COLOR_GREEN, fmt, ##__VA_ARGS__)
126 #else
127 #define fs_debug_trace_fswrapper(fmt, ...) \
128     do {                                   \
129     } while (0)
130 #endif
131 
132 #endif /* FS_DEBUG_H */