OpenDNSSEC-enforcer  1.3.4
/build/buildd/opendnssec-1.3.4/enforcer/ksm/debug.c
Go to the documentation of this file.
00001 /*
00002  * $Id: debug.c 731 2009-05-18 08:24:19Z sion $
00003  *
00004  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00019  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00020  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00021  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00023  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00025  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  */
00028 
00029 /*+
00030  * debug.c - Debug Routines
00031  *
00032  * Description:
00033  *      Contains functions used to produce debug output.
00034  *
00035  *      Debug information is controlled by the debug bitmask.  Different items
00036  *      of debug information are controlled by the different bits, so setting or
00037  *      clearing those bits controls what information is output.
00038 -*/
00039 
00040 #include <stdarg.h>
00041 #include <stdio.h>
00042 
00043 #include "ksm/debug.h"
00044 #include "ksm/message.h"
00045 
00046 /* Bitmask of debug flags */
00047 
00048 static unsigned int m_debug = 0;
00049 
00050 /*+
00051  * DbgGet - Get Debug Bitmask
00052  *
00053  * Description:
00054  *      Returns the current value of the debug bitmask.
00055  *
00056  * Returns:
00057  *      unsigned int
00058  *          Current value of the debug bitmask.
00059 -*/
00060 
00061 unsigned int DbgGet(void)
00062 {
00063     return m_debug;
00064 }
00065 
00066 
00067 
00068 /*+
00069  * DbgSet - Set Debug Bitmask
00070  *
00071  * Description:
00072  *      Sets the debug bitmask to the given value.
00073  *
00074  * Input:
00075  *      unsigned int mask
00076  *          New bitmask value.
00077  *
00078  * Returns:
00079  *      unsigned int
00080  *          Previous setting of the debug bitmask.
00081 -*/
00082 
00083 unsigned int DbgSet(unsigned int mask)
00084 {
00085     unsigned int oldmask;
00086     
00087     oldmask = m_debug;
00088     m_debug = mask;
00089     return oldmask;
00090 }
00091 
00092 
00093 /*+
00094  * DbgIsSet - Is Debug Bit Set?
00095  *
00096  * Description:
00097  *      Checks if any of the bits in the passed bitmask are also set in the
00098  *      current debug bitmask.
00099  *
00100  * Arguments:
00101  *      unsigned int mask
00102  *          Bitmask to test.
00103  *
00104  * Returns:
00105  *      int
00106  *          1 if any of the bits in the mask are set.
00107  *          0 if none of them are set.
00108 -*/
00109 
00110 int DbgIsSet(unsigned int flags)
00111 {
00112     return (flags & m_debug);
00113 }
00114 
00115 
00116 
00117 /*+
00118  * DbgOutput - Output Debug Message
00119  *
00120  * Description:
00121  *      Outputs a debug message to stdout if one or more of the bits in the
00122  *      given bitmask is also set in the debug bit mask.  If no bits are set,
00123  *      the function is a no-op.
00124  *
00125  * Arguments:
00126  *      unsigned int mask
00127  *          Only output the text if one or more of the bits in this bitmask is
00128  *          also set in the debug bitmask.
00129  *
00130  *      const char* format
00131  *          printf()-style format string for the message.
00132  *
00133  *      ...
00134  *          Arguments for the format string
00135 -*/
00136 
00137 void DbgOutput(unsigned int mask, const char* format, ...)
00138 {
00139     va_list ap;
00140 
00141     if (DbgIsSet(mask)) {
00142         va_start(ap, format);
00143         printf("DEBUG: ");
00144         vprintf(format, ap);
00145         va_end(ap);
00146     }
00147 
00148     return;
00149 }
00150 
00151 
00152 /*+
00153  * DbgLog - Output Debug Message
00154  *
00155  * Description:
00156  *      Outputs a debug message via MsgLog if one or more of the bits in the
00157  *      given bitmask is also set in the debug bit mask.  If no bits are set,
00158  *      the function is a no-op.
00159  *
00160  * Arguments:
00161  *      unsigned int mask
00162  *          Only output the text if one or more of the bits in this bitmask is
00163  *          also set in the debug bitmask.
00164  *
00165  *      int status
00166  *              Status code identifying the message to output.
00167  *
00168  *      ...
00169  *          Arguments for the format string
00170 -*/
00171 
00172 void DbgLog(unsigned int mask, int status, ...)
00173 {
00174         va_list ap;             /* variable arguments */
00175 
00176         if (DbgIsSet(mask)) {
00177 
00178                 /* Must output the message, so get the arguments as a va_list */
00179 
00180                 va_start(ap, status);
00181                 MsgLogAp(status, ap);
00182                 va_end(ap);
00183         }
00184 
00185         return;
00186 }
00187 
00188 
00189 
00190 /*+
00191  * DbgPrint - Unconditionally Print Debug Message
00192  *
00193  * Description:
00194  *      Outputs a debug message on stdout.
00195  *
00196  * Arguments:
00197  *      const char* format
00198  *          printf()-style format string for the message.
00199  *
00200  *      ...
00201  *          Arguments for the format string
00202 -*/
00203 
00204 void DbgPrint(const char* format, ...)
00205 {
00206     va_list ap;
00207 
00208     va_start(ap, format);
00209     printf("DEBUG: ");
00210     vprintf(format, ap);
00211     va_end(ap);
00212 
00213     return;
00214 }