Annotation of embedaddon/mpd/src/contrib/libpdel/debug/debug.h, revision 1.1

1.1     ! misho       1: 
        !             2: /*
        !             3:  * Copyright (c) 2001-2002 Packet Design, LLC.
        !             4:  * All rights reserved.
        !             5:  * 
        !             6:  * Subject to the following obligations and disclaimer of warranty,
        !             7:  * use and redistribution of this software, in source or object code
        !             8:  * forms, with or without modifications are expressly permitted by
        !             9:  * Packet Design; provided, however, that:
        !            10:  * 
        !            11:  *    (i)  Any and all reproductions of the source or object code
        !            12:  *         must include the copyright notice above and the following
        !            13:  *         disclaimer of warranties; and
        !            14:  *    (ii) No rights are granted, in any manner or form, to use
        !            15:  *         Packet Design trademarks, including the mark "PACKET DESIGN"
        !            16:  *         on advertising, endorsements, or otherwise except as such
        !            17:  *         appears in the above copyright notice or in the software.
        !            18:  * 
        !            19:  * THIS SOFTWARE IS BEING PROVIDED BY PACKET DESIGN "AS IS", AND
        !            20:  * TO THE MAXIMUM EXTENT PERMITTED BY LAW, PACKET DESIGN MAKES NO
        !            21:  * REPRESENTATIONS OR WARRANTIES, EXPRESS OR IMPLIED, REGARDING
        !            22:  * THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY AND ALL IMPLIED
        !            23:  * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE,
        !            24:  * OR NON-INFRINGEMENT.  PACKET DESIGN DOES NOT WARRANT, GUARANTEE,
        !            25:  * OR MAKE ANY REPRESENTATIONS REGARDING THE USE OF, OR THE RESULTS
        !            26:  * OF THE USE OF THIS SOFTWARE IN TERMS OF ITS CORRECTNESS, ACCURACY,
        !            27:  * RELIABILITY OR OTHERWISE.  IN NO EVENT SHALL PACKET DESIGN BE
        !            28:  * LIABLE FOR ANY DAMAGES RESULTING FROM OR ARISING OUT OF ANY USE
        !            29:  * OF THIS SOFTWARE, INCLUDING WITHOUT LIMITATION, ANY DIRECT,
        !            30:  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, PUNITIVE, OR CONSEQUENTIAL
        !            31:  * DAMAGES, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, LOSS OF
        !            32:  * USE, DATA OR PROFITS, HOWEVER CAUSED AND UNDER ANY THEORY OF
        !            33:  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
        !            34:  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
        !            35:  * THE USE OF THIS SOFTWARE, EVEN IF PACKET DESIGN IS ADVISED OF
        !            36:  * THE POSSIBILITY OF SUCH DAMAGE.
        !            37:  *
        !            38:  * Author: Archie Cobbs <archie@freebsd.org>
        !            39:  */
        !            40: 
        !            41: #ifndef _PDEL_PDEL_DEBUG_H_
        !            42: #define _PDEL_PDEL_DEBUG_H_
        !            43: 
        !            44: /*
        !            45:  * This flag globally enables/disables debugging.
        !            46:  */
        !            47: #define PDEL_DEBUG                     0
        !            48: 
        !            49: #if PDEL_DEBUG
        !            50: /*
        !            51:  * Flags that enable various debugging options.
        !            52:  */
        !            53: enum {
        !            54:        PDEL_DEBUG_HTTP,
        !            55:        PDEL_DEBUG_HTTP_HDRS,
        !            56:        PDEL_DEBUG_HTTP_CONNECTION_CACHE,
        !            57:        PDEL_DEBUG_HTTP_SERVLET_COOKIEAUTH,
        !            58:        PDEL_DEBUG_PEVENT,
        !            59:        PDEL_DEBUG_MUTEX,
        !            60: };
        !            61: 
        !            62: /*
        !            63:  * Runtime variable which controls debugging.
        !            64:  */
        !            65: #define PDEL_DEBUG_FLAGS (0                                            \
        !            66:        | (1 << PDEL_DEBUG_HTTP)                                        \
        !            67:        | (1 << PDEL_DEBUG_PEVENT)                                      \
        !            68:        | (1 << PDEL_DEBUG_MUTEX)                                       \
        !            69:        | 0)
        !            70: 
        !            71: #endif /* PDEL_DEBUG */
        !            72: 
        !            73: /*
        !            74:  * Macro to test whether a specific debug flag is enabled.
        !            75:  */
        !            76: #if PDEL_DEBUG
        !            77: #define PDEL_DEBUG_ENABLED(f)                                          \
        !            78:        ((PDEL_DEBUG_FLAGS & (1 << PDEL_DEBUG_ ## f)) != 0)
        !            79: #else
        !            80: #define PDEL_DEBUG_ENABLED(f)          (0)
        !            81: #endif
        !            82: 
        !            83: /*
        !            84:  * Macro for printing some debugging output.
        !            85:  */
        !            86: #if PDEL_DEBUG
        !            87: #define DBG(c, fmt, args...)                                           \
        !            88:        do {                                                            \
        !            89:                char buf[240];                                          \
        !            90:                const char *s;                                          \
        !            91:                int r, n;                                               \
        !            92:                                                                        \
        !            93:                if (PDEL_DEBUG_ENABLED(c)) {                            \
        !            94:                        snprintf(buf, sizeof(buf),                      \
        !            95:                            "%p[%s]: " fmt "\n", pthread_self(),        \
        !            96:                            __FUNCTION__ , ## args);                    \
        !            97:                        for (r = strlen(buf), s = buf;                  \
        !            98:                            r > 0; r -= n, s += n) {                    \
        !            99:                                if ((n = write(1, s, r)) == -1)         \
        !           100:                                        break;                          \
        !           101:                        }                                               \
        !           102:                }                                                       \
        !           103:        } while (0)
        !           104: #else
        !           105: #define DBG(c, fmt, args...)   do { } while (0)
        !           106: #endif /* !PDEL_DEBUG */
        !           107: 
        !           108: /*
        !           109:  * Mutex macros with additional debugging.
        !           110:  */
        !           111: #if PDEL_DEBUG
        !           112: 
        !           113: #define MUTEX_LOCK(m, c)                                               \
        !           114:        do {                                                            \
        !           115:                int _r;                                                 \
        !           116:                                                                        \
        !           117:                DBG(MUTEX, "locking %s", #m);                           \
        !           118:                _r = pthread_mutex_lock(m);                             \
        !           119:                assert(_r == 0);                                        \
        !           120:                (c)++;                                                  \
        !           121:                DBG(MUTEX, "%s locked -> %d", #m, (c));                 \
        !           122:        } while (0)
        !           123: 
        !           124: #define MUTEX_UNLOCK(m, c)                                             \
        !           125:        do {                                                            \
        !           126:                int _r;                                                 \
        !           127:                                                                        \
        !           128:                (c)--;                                                  \
        !           129:                DBG(MUTEX, "unlocking %s -> %d", #m, (c));              \
        !           130:                _r = pthread_mutex_unlock(m);                           \
        !           131:                assert(_r == 0);                                        \
        !           132:                DBG(MUTEX, "%s unlocked", #m);                          \
        !           133:        } while (0)
        !           134: 
        !           135: #define MUTEX_TRYLOCK(m, c)                                            \
        !           136:        do {                                                            \
        !           137:                int _r;                                                 \
        !           138:                                                                        \
        !           139:                DBG(MUTEX, "try locking %s", #m);                       \
        !           140:                _r = pthread_mutex_trylock(m);                          \
        !           141:                if (_r == 0) {                                          \
        !           142:                        (c)++;                                          \
        !           143:                        DBG(MUTEX, "%s locked -> %d", #m, (c));         \
        !           144:                } else                                                  \
        !           145:                        DBG(MUTEX, "%s lock failed", #m);               \
        !           146:                errno = _r;                                             \
        !           147:        } while (0)
        !           148: 
        !           149: #else  /* !PDEL_DEBUG */
        !           150: 
        !           151: #define MUTEX_LOCK(m, c)       (void)pthread_mutex_lock(m)
        !           152: #define MUTEX_UNLOCK(m, c)     (void)pthread_mutex_unlock(m)
        !           153: #define MUTEX_TRYLOCK(m, c)    (errno = pthread_mutex_trylock(m))
        !           154: 
        !           155: #endif /* !PDEL_DEBUG */
        !           156: 
        !           157: #endif /* _PDEL_PDEL_DEBUG_H_ */

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>