1 /* GNU Mailutils -- a suite of utilities for electronic mail -*- c -*-
2    Copyright (C) 1999-2021 Free Software Foundation, Inc.
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 3 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General
15    Public License along with this library.  If not, see
16    <http://www.gnu.org/licenses/>. */
17 
18 #ifndef _MAILUTILS_DEBUG_H
19 #define _MAILUTILS_DEBUG_H
20 
21 #include <stdarg.h>
22 
23 #include <mailutils/types.h>
24 #include <mailutils/error.h>
25 #include <mailutils/sys/debcat.h>
26 
27 #ifdef __cplusplus
28 extern "C" {
29 #endif
30 
31 extern int mu_debug_line_info;
32 
33 #define MU_DEBUG_ERROR  0
34 #define MU_DEBUG_TRACE0 1
35 #define MU_DEBUG_TRACE MU_DEBUG_TRACE0
36 #define MU_DEBUG_TRACE1 2
37 #define MU_DEBUG_TRACE2 3
38 #define MU_DEBUG_TRACE3 4
39 #define MU_DEBUG_TRACE4 5
40 #define MU_DEBUG_TRACE5 6
41 #define MU_DEBUG_TRACE6 7
42 #define MU_DEBUG_TRACE7 8
43 #define MU_DEBUG_TRACE8 9
44 #define MU_DEBUG_TRACE9 10
45 
46 #define MU_DEBUG_PROT   11
47 
48 #define MU_DEBUG_LEVEL_MASK(lev) (1 << (lev))
49 #define MU_DEBUG_LEVEL_UPTO(lev) ((1 << ((lev)+1)) - 1)
50 #define MU_DEBUG_LEVEL_RANGE(a, b)					\
51   ((a) == 0 ? MU_DEBUG_LEVEL_UPTO (b) :					\
52               MU_DEBUG_LEVEL_UPTO (b) & ~MU_DEBUG_LEVEL_UPTO ((a) - 1))
53 
54 
55 size_t mu_debug_register_category (char *name);
56 size_t mu_debug_next_handle (void);
57 int mu_debug_level_p (mu_debug_handle_t catn, mu_debug_level_t level);
58 void mu_debug_enable_category (const char *catname, size_t catlen,
59 			       mu_debug_level_t level);
60 void mu_debug_disable_category (const char *catname, size_t catlen);
61 int mu_debug_category_level (const char *catname, size_t catlen,
62 			     mu_debug_level_t *plev);
63 void mu_debug_parse_spec (const char *spec);
64 int mu_debug_format_spec(mu_stream_t str, const char *names, int showunset);
65 
66 int mu_debug_get_category_level (mu_debug_handle_t catn,
67 				 mu_debug_level_t *plev);
68 int mu_debug_set_category_level (mu_debug_handle_t catn,
69 				 mu_debug_level_t level);
70 
71 void mu_debug_clear_all (void);
72 
73 void mu_debug_log (const char *fmt, ...) MU_PRINTFLIKE(1,2);
74 void mu_debug_log_begin (const char *fmt, ...) MU_PRINTFLIKE(1,2);
75 void mu_debug_log_cont (const char *fmt, ...) MU_PRINTFLIKE(1,2);
76 void mu_debug_log_end (const char *fmt, ...) MU_PRINTFLIKE(1,2);
77 void mu_debug_log_nl (void);
78 
79 int mu_debug_get_iterator (mu_iterator_t *piterator, int skipunset);
80 
81 
82 #define MU_ASSERT(expr)						\
83  do								\
84   {								\
85     int __rc = expr;						\
86     if (__rc)							\
87       {								\
88 	mu_error ("%s:%d: " #expr " failed: %s",                \
89                   __FILE__, __LINE__, mu_strerror (__rc));	\
90 	abort ();						\
91       }								\
92   }                                                             \
93  while (0)
94 
95 
96 
97 #define mu_debug(cat,lev,s)					         \
98   do                                                                     \
99     if (mu_debug_level_p (cat, lev))			                 \
100       {								         \
101 	if (mu_debug_line_info)					         \
102 	  {								 \
103 	    mu_debug_log_begin ("\033X<%d>%s:%d: ",	                 \
104 				MU_LOGMODE_LOCUS, __FILE__, __LINE__);   \
105 	    mu_debug_log_end s;						 \
106 	  }								 \
107 	else								 \
108 	  mu_debug_log s;						 \
109       }                                                                  \
110   while (0)
111 
112   /* Debugging hooks. */
113   /* Dump a stack trace and terminate the program. */
114 void mu_gdb_bt (void);
115   /* Sleep till attached to by gdb. */
116 void mu_wd (unsigned to);
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif /* _MAILUTILS_DEBUG_H */
123