lib/misc/debug.h
changeset 0 6f7a81934006
equal deleted inserted replaced
-1:000000000000 0:6f7a81934006
       
     1 // Copyright (C) 1999,2000 Bruce Guenter <bruceg@em.ca>
       
     2 //
       
     3 // This program is free software; you can redistribute it and/or modify
       
     4 // it under the terms of the GNU General Public License as published by
       
     5 // the Free Software Foundation; either version 2 of the License, or
       
     6 // (at your option) any later version.
       
     7 //
       
     8 // This program is distributed in the hope that it will be useful,
       
     9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
       
    11 // GNU General Public License for more details.
       
    12 //
       
    13 // You should have received a copy of the GNU General Public License
       
    14 // along with this program; if not, write to the Free Software
       
    15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
       
    16 
       
    17 #ifndef VMAILMGR__DEBUG__H__
       
    18 #define VMAILMGR__DEBUG__H__
       
    19 
       
    20 #define trace(X) trace2(this,X)
       
    21 #define traceptr(X,P) trace3(this,X " " #P "=",P)
       
    22 #define tracestr(X,P) trace4(this,X " " #P "=(",P)
       
    23 
       
    24 #ifdef TRACE
       
    25 
       
    26 #include <unistd.h>
       
    27 
       
    28 static inline const char* itoh(unsigned i)
       
    29 {
       
    30   static char convert[17] = "0123456789abcdef";
       
    31   static char buf[9];
       
    32   for(unsigned j = 8; j > 0; ) {
       
    33     --j;
       
    34     buf[j] = convert[i & 0xf];
       
    35     i >>= 4;
       
    36   }
       
    37   buf[8] = 0;
       
    38   return buf;
       
    39 }
       
    40 
       
    41 static inline void trace2(const void* s, const char* x)
       
    42 {
       
    43   write(2, itoh((unsigned)s), 8);
       
    44   write(2, ": ", 2);
       
    45   write(2, x, strlen(x));
       
    46   write(2, "\n", 1);
       
    47 }
       
    48 
       
    49 static inline void trace3(const void* s, const char* x, const void* p)
       
    50 {
       
    51   write(2, itoh((unsigned)s), 8);
       
    52   write(2, ": ", 2);
       
    53   write(2, x, strlen(x));
       
    54   write(2, itoh((unsigned)p), 8);
       
    55   write(2, "\n", 1);
       
    56 }
       
    57 
       
    58 static inline void trace4(const void* s, const char* x, const char* p)
       
    59 {
       
    60   write(2, itoh((unsigned)s), 8);
       
    61   write(2, ": ", 2);
       
    62   write(2, x, strlen(x));
       
    63   write(2, itoh((unsigned)p), 8);
       
    64   write(2, ")'", 2);
       
    65   write(2, p, strlen(p));
       
    66   write(2, "'\n", 2);
       
    67 }
       
    68 
       
    69 #else
       
    70 #define trace2(X,P)
       
    71 #define trace3(T,X,P)
       
    72 #define trace4(T,X,P)
       
    73 #endif
       
    74 
       
    75 #ifdef DEBUG
       
    76 
       
    77 static inline void debug(const char* x)
       
    78 {
       
    79   write(2, x, strlen(x));
       
    80   write(2, "\n", 1);
       
    81 }
       
    82 
       
    83 static inline void fail(int code, const char* message)
       
    84 {
       
    85   debug(message);
       
    86   exit(code);
       
    87 }
       
    88 
       
    89 #else
       
    90 #define fail(X,Y) exit(X)
       
    91 #define debug(X)
       
    92 #endif
       
    93 
       
    94 #endif