00001 /* 00002 * L.h - support for logging (printf...) style debugging. 00003 * 00004 * Copyright (c) 1997 Phil Maker 00005 * All rights reserved. 00006 * 00007 * Redistribution and use in source and binary forms, with or without 00008 * modification, are permitted provided that the following conditions 00009 * are met: 00010 * 1. Redistributions of source code must retain the above copyright 00011 * notice, this list of conditions and the following disclaimer. 00012 * 2. Redistributions in binary form must reproduce the above copyright 00013 * notice, this list of conditions and the following disclaimer in the 00014 * documentation and/or other materials provided with the distribution. 00015 * 00016 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 00017 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00018 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00019 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 00020 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00021 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 00022 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 00023 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00024 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 00025 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 00026 * SUCH DAMAGE. 00027 * 00028 * Id: L.h,v 1.2 1998/01/17 10:57:03 pjm Exp 00029 */ 00030 00031 00032 #ifndef _L_h_ 00033 #define _L_h_ 1 00034 00035 #ifdef __cplusplus 00036 extern "C" { 00037 #endif 00038 00039 #ifndef WITHOUT_NANA 00040 00041 /* 00042 * nana-config.h - the system wide configuration file; we put the ifndef 00043 * around it to avoid the file 5 million times during a compile. 00044 */ 00045 00046 #ifndef _nana_config_h_ 00047 #include <nana-config.h> 00048 #endif 00049 00050 /* 00051 * L_LEVEL sets the level of logging analogously to NDEBUG in assert.h 00052 * 00053 * L_LEVEL == 2: always print. 00054 * L_LEVEL == 1: print iff the guard is true. 00055 * L_LEVEL == 0: never print. 00056 */ 00057 00058 #ifndef L_LEVEL /* define DEFAULT for L_LEVEL */ 00059 #define L_LEVEL 1 00060 #endif 00061 00062 /* 00063 * L_DEFAULT_HANDLER - the default print handler; by default we just 00064 * use fprintf. 00065 */ 00066 00067 #ifndef L_DEFAULT_HANDLER /* define default handler */ 00068 #define L_DEFAULT_HANDLER fprintf 00069 #endif /* L_DEFAULT_HANDLER */ 00070 00071 /* 00072 * L_DEFAULT_GUARD - the default guard expression; a message is printed 00073 * iff the guard is true. By default its always true. 00074 */ 00075 00076 #ifndef L_DEFAULT_GUARD 00077 #define L_DEFAULT_GUARD (1) 00078 #endif 00079 00080 /* 00081 * L_DEFAULT_PARAMS - the default value to be passed as the second argument 00082 * to the handler macro when an invariant fails. 00083 */ 00084 00085 00086 #ifndef L_DEFAULT_PARAMS 00087 #define L_DEFAULT_PARAMS stderr 00088 #endif 00089 00090 /* 00091 * L_SHOW_TIME_FORMAT - the format string for printing the time. 00092 */ 00093 00094 #ifndef L_SHOW_TIME_FORMAT 00095 #define L_SHOW_TIME_FORMAT "%.6f:\t" 00096 #endif 00097 00098 /* 00099 * L_SHOW_TIME_NOW - the function to measure the time. 00100 */ 00101 00102 #ifndef L_SHOW_TIME_NOW 00103 #include <now.h> 00104 #define L_SHOW_TIME_NOW now() 00105 #endif 00106 00107 /* 00108 * L_SHOW_TIME - if it is defined then we will put time stamps at the 00109 * beginning of each message using L_SHOW_TIME_FORMAT and L_SHOW_TIME_NOW. 00110 */ 00111 00112 #ifdef L_SHOW_TIME 00113 #define _L_SHOWTIME(h,p) h (p, L_SHOW_TIME_FORMAT, L_SHOW_TIME_NOW) 00114 #else 00115 #define _L_SHOWTIME(h,p) /* nothing */ 00116 #endif /* L_SHOWTIME */ 00117 00118 /* 00119 * LGHP(g,h,p, f...) - print a log message. 00120 * 00121 * g - the guard; print the message iff this is true 00122 * h - the handler function that does the actual printing 00123 * p - a parameter for the handler function; e.g. a file descriptor. 00124 * f - printf style format string; we put this at the end since we 00125 * we need to be able have 1 or more arguments (e.g. L("x") and 00126 * L("%d",10); we use GNU cccp preprocessor varargs extension 00127 * to handle multiple arguments. 00128 */ 00129 00130 #ifndef __GNUC__ 00131 error you need gcc for this stuff to work properly 00132 #endif 00133 00134 #if L_LEVEL == 2 /* always log the message */ 00135 #define LGHP(g,h,p,f...) \ 00136 do { \ 00137 _L_SHOWTIME(h,p); \ 00138 h (p, ##f); \ 00139 } while(0) 00140 00141 #elif L_LEVEL == 1 /* log it iff the guard is true */ 00142 #define LGHP(g,h,p,f...) \ 00143 do { \ 00144 if(g) { \ 00145 _L_SHOWTIME(h,p); \ 00146 h (p, ##f); \ 00147 } \ 00148 } while(0) 00149 #elif L_LEVEL == 0 /* no logging so ignore them */ 00150 #define LGHP(g,h,p,f...) /* nothing */ 00151 #endif /* L_LEVEL */ 00152 00153 /* 00154 * User routines. 00155 */ 00156 00157 #define L(f...) \ 00158 LGHP(L_DEFAULT_GUARD,L_DEFAULT_HANDLER,L_DEFAULT_PARAMS,##f) 00159 #define LG(g,f...) \ 00160 LGHP(g,L_DEFAULT_HANDLER,L_DEFAULT_PARAMS,##f) 00161 #define LH(h,f...) \ 00162 LGHP(L_DEFAULT_GUARD,h,L_DEFAULT_PARAMS,##f) 00163 #define LP(p,f...) \ 00164 LGHP(L_DEFAULT_GUARD,L_DEFAULT_HANDLER,p,##f) 00165 #define LGP(g,p,f...) \ 00166 LGHP(g,L_DEFAULT_HANDLER,p,##f) 00167 #define LHP(h,p,f...) \ 00168 LGHP(L_DEFAULT_GUARD,h,p,##f) 00169 00170 /* 00171 * V* - since the L* macros take a variable numbers of arguments we 00172 * have problems compiling calls to L with C preprocessors other 00173 * than GNU cccp. The V* macros are called using a bracketed 00174 * argument list, e.g. VL((f,x,s)); 00175 * 00176 * if we are compiling with GNU C then they simpily call the normal 00177 * varargs macro. if we are not using GNU CC then they map to empty. 00178 */ 00179 00180 #define VL(a) L a 00181 #define VLG(a) LG a 00182 #define VLH(a) LH a 00183 #define VLP(a) LP a 00184 #define VLGP(a) LGP a 00185 #define VLHP(a) LHP a 00186 #define VLGHP(a) LGHP a 00187 00188 #else /* defined(WITHOUT_NANA) */ 00189 00190 #define VL(a) /* empty */ 00191 #define VLG(a) /* empty */ 00192 #define VLH(a) /* empty */ 00193 #define VLP(a) /* empty */ 00194 #define VLGP(a) /* empty */ 00195 #define VLHP(a) /* empty */ 00196 #define VLGHP(a) /* empty */ 00197 00198 #endif /* !defined(WITHOUT_NANA) */ 00199 00200 #ifdef __cplusplus 00201 } 00202 #endif 00203 00204 #endif /* _L_h_ */ 00205 00206 00207
1.3.4