OpenDNSSEC-enforcer  1.4.1
debug.c
Go to the documentation of this file.
1 /*
2  * $Id: debug.c 731 2009-05-18 08:24:19Z sion $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. 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  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*+
30  * debug.c - Debug Routines
31  *
32  * Description:
33  * Contains functions used to produce debug output.
34  *
35  * Debug information is controlled by the debug bitmask. Different items
36  * of debug information are controlled by the different bits, so setting or
37  * clearing those bits controls what information is output.
38 -*/
39 
40 #include <stdarg.h>
41 #include <stdio.h>
42 
43 #include "ksm/debug.h"
44 #include "ksm/message.h"
45 
46 /* Bitmask of debug flags */
47 
48 static unsigned int m_debug = 0;
49 
50 /*+
51  * DbgGet - Get Debug Bitmask
52  *
53  * Description:
54  * Returns the current value of the debug bitmask.
55  *
56  * Returns:
57  * unsigned int
58  * Current value of the debug bitmask.
59 -*/
60 
61 unsigned int DbgGet(void)
62 {
63  return m_debug;
64 }
65 
66 
67 
68 /*+
69  * DbgSet - Set Debug Bitmask
70  *
71  * Description:
72  * Sets the debug bitmask to the given value.
73  *
74  * Input:
75  * unsigned int mask
76  * New bitmask value.
77  *
78  * Returns:
79  * unsigned int
80  * Previous setting of the debug bitmask.
81 -*/
82 
83 unsigned int DbgSet(unsigned int mask)
84 {
85  unsigned int oldmask;
86 
87  oldmask = m_debug;
88  m_debug = mask;
89  return oldmask;
90 }
91 
92 
93 /*+
94  * DbgIsSet - Is Debug Bit Set?
95  *
96  * Description:
97  * Checks if any of the bits in the passed bitmask are also set in the
98  * current debug bitmask.
99  *
100  * Arguments:
101  * unsigned int mask
102  * Bitmask to test.
103  *
104  * Returns:
105  * int
106  * 1 if any of the bits in the mask are set.
107  * 0 if none of them are set.
108 -*/
109 
110 int DbgIsSet(unsigned int flags)
111 {
112  return (flags & m_debug);
113 }
114 
115 
116 
117 /*+
118  * DbgOutput - Output Debug Message
119  *
120  * Description:
121  * Outputs a debug message to stdout if one or more of the bits in the
122  * given bitmask is also set in the debug bit mask. If no bits are set,
123  * the function is a no-op.
124  *
125  * Arguments:
126  * unsigned int mask
127  * Only output the text if one or more of the bits in this bitmask is
128  * also set in the debug bitmask.
129  *
130  * const char* format
131  * printf()-style format string for the message.
132  *
133  * ...
134  * Arguments for the format string
135 -*/
136 
137 void DbgOutput(unsigned int mask, const char* format, ...)
138 {
139  va_list ap;
140 
141  if (DbgIsSet(mask)) {
142  va_start(ap, format);
143  printf("DEBUG: ");
144  vprintf(format, ap);
145  va_end(ap);
146  }
147 
148  return;
149 }
150 
151 
152 /*+
153  * DbgLog - Output Debug Message
154  *
155  * Description:
156  * Outputs a debug message via MsgLog if one or more of the bits in the
157  * given bitmask is also set in the debug bit mask. If no bits are set,
158  * the function is a no-op.
159  *
160  * Arguments:
161  * unsigned int mask
162  * Only output the text if one or more of the bits in this bitmask is
163  * also set in the debug bitmask.
164  *
165  * int status
166  * Status code identifying the message to output.
167  *
168  * ...
169  * Arguments for the format string
170 -*/
171 
172 void DbgLog(unsigned int mask, int status, ...)
173 {
174  va_list ap; /* variable arguments */
175 
176  if (DbgIsSet(mask)) {
177 
178  /* Must output the message, so get the arguments as a va_list */
179 
180  va_start(ap, status);
181  MsgLogAp(status, ap);
182  va_end(ap);
183  }
184 
185  return;
186 }
187 
188 
189 
190 /*+
191  * DbgPrint - Unconditionally Print Debug Message
192  *
193  * Description:
194  * Outputs a debug message on stdout.
195  *
196  * Arguments:
197  * const char* format
198  * printf()-style format string for the message.
199  *
200  * ...
201  * Arguments for the format string
202 -*/
203 
204 void DbgPrint(const char* format, ...)
205 {
206  va_list ap;
207 
208  va_start(ap, format);
209  printf("DEBUG: ");
210  vprintf(format, ap);
211  va_end(ap);
212 
213  return;
214 }