Changeset 4151 for zzuf/trunk/src
- Timestamp:
- Dec 20, 2009, 1:24:50 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
zzuf/trunk/src/libzzuf/debug.c
r4112 r4151 25 25 #endif 26 26 #include <stdio.h> 27 #include <string.h> 27 28 #if defined HAVE_UNISTD_H 28 29 # include <unistd.h> … … 43 44 * either in base 10 or in hexadecimal. 44 45 */ 45 #define WRITE_INT( fd,i, base) \46 #define WRITE_INT(i, base) \ 46 47 do \ 47 48 { \ 48 49 char buf[128], *b = buf + 127; \ 49 50 if(i <= 0) \ 50 write(fd,(i = -i) ? "-" : "0", 1); /* XXX: hack here */ \51 append((i = -i) ? "-" : "0", 1); /* XXX: hack here */ \ 51 52 while(i) \ 52 53 { \ … … 54 55 i /= base; \ 55 56 } \ 56 write(fd,b + 1, (int)(buf + 127 - b)); \57 append(b + 1, (int)(buf + 127 - b)); \ 57 58 } while(0) 59 60 /* Temporary buffer for deferred output */ 61 char debugbuffer[BUFSIZ]; 62 size_t debugcount = 1; 58 63 59 64 void _zz_debug(char const *format, ...) … … 86 91 * - fprintf(stderr, "\n"); 87 92 */ 93 static inline void append(void const *data, size_t count) 94 { 95 if (debugcount + count <= sizeof(debugbuffer)) 96 { 97 memcpy(debugbuffer + debugcount, data, count); 98 debugcount += count; 99 } 100 } 101 88 102 static void mydebug(char const *format, va_list args) 89 103 { … … 92 106 int saved_errno; 93 107 94 if(_zz_debugfd < 0)95 return;96 97 108 saved_errno = errno; 98 109 99 write(_zz_debugfd, "** zzuf debug ** ", 17); 110 /* If there is spare data and the debug fd is open, we send the data */ 111 if (debugcount && _zz_debugfd >= 0) 112 { 113 write(_zz_debugfd, debugbuffer, debugcount); 114 debugcount = 0; 115 } 116 117 append("** zzuf debug ** ", 17); 100 118 for(f = format; *f; f++) 101 119 { 102 120 if(*f != '%') 103 121 { 104 write(_zz_debugfd,f, 1);122 append(f, 1); 105 123 continue; 106 124 } … … 114 132 char i = (char)(unsigned char)va_arg(args, int); 115 133 if(i >= 0x20 && i < 0x7f) 116 write(_zz_debugfd,&i, 1);134 append(&i, 1); 117 135 else if(i == '\n') 118 write(_zz_debugfd,"\\n", 2);136 append("\\n", 2); 119 137 else if(i == '\t') 120 write(_zz_debugfd,"\\t", 2);138 append("\\t", 2); 121 139 else if(i == '\r') 122 write(_zz_debugfd,"\\r", 2);140 append("\\r", 2); 123 141 else 124 142 { 125 write(_zz_debugfd,"\\x", 2);126 write(_zz_debugfd,hex2char + ((i & 0xf0) >> 4), 1);127 write(_zz_debugfd,hex2char + (i & 0x0f), 1);143 append("\\x", 2); 144 append(hex2char + ((i & 0xf0) >> 4), 1); 145 append(hex2char + (i & 0x0f), 1); 128 146 } 129 147 } … … 131 149 { 132 150 int i = va_arg(args, int); 133 WRITE_INT( _zz_debugfd,i, 10);151 WRITE_INT(i, 10); 134 152 } 135 153 else if(*f == 'x') 136 154 { 137 155 int i = va_arg(args, int); 138 WRITE_INT( _zz_debugfd,i, 16);156 WRITE_INT(i, 16); 139 157 } 140 158 else if(f[0] == 'l' && (f[1] == 'i' || f[1] == 'd')) 141 159 { 142 160 long int i = va_arg(args, long int); 143 WRITE_INT( _zz_debugfd,i, 10);161 WRITE_INT(i, 10); 144 162 f++; 145 163 } … … 147 165 { 148 166 long long int i = va_arg(args, long long int); 149 WRITE_INT( _zz_debugfd,i, 10);167 WRITE_INT(i, 10); 150 168 f += 2; 151 169 } … … 154 172 double g = va_arg(args, double), h = 0.0000001; 155 173 int i = (int)g; 156 WRITE_INT( _zz_debugfd,i, 10);174 WRITE_INT(i, 10); 157 175 for(i = 0; i < 7; i++) 158 176 { … … 162 180 break; 163 181 if(i == 0) 164 write(_zz_debugfd,".", 1);165 write(_zz_debugfd,hex2char + (int)g, 1);182 append(".", 1); 183 append(hex2char + (int)g, 1); 166 184 } 167 185 } … … 170 188 uintptr_t i = va_arg(args, uintptr_t); 171 189 if(!i) 172 write(_zz_debugfd,"NULL", 5);190 append("NULL", 5); 173 191 else 174 192 { 175 write(_zz_debugfd,"0x", 2);176 WRITE_INT( _zz_debugfd,i, 16);193 append("0x", 2); 194 WRITE_INT(i, 16); 177 195 } 178 196 } … … 181 199 char *s = va_arg(args, char *); 182 200 if(!s) 183 write(_zz_debugfd,"(nil)", 5);201 append("(nil)", 5); 184 202 else 185 203 { … … 187 205 while(s[l]) 188 206 l++; 189 write(_zz_debugfd,s, l);207 append(s, l); 190 208 } 191 209 } … … 193 211 { 194 212 int i = va_arg(args, int); 195 write(_zz_debugfd,hex2char + ((i & 0xf0) >> 4), 1);196 write(_zz_debugfd,hex2char + (i & 0x0f), 1);213 append(hex2char + ((i & 0xf0) >> 4), 1); 214 append(hex2char + (i & 0x0f), 1); 197 215 f += 2; 198 216 } 199 217 else 200 218 { 201 write(_zz_debugfd, f - 1, 2); 202 } 203 } 204 write(_zz_debugfd, "\n", 1); 219 append(f - 1, 2); 220 } 221 } 222 append("\n", 1); 223 224 /* If the debug fd is open, we send the data */ 225 if (_zz_debugfd >= 0) 226 { 227 write(_zz_debugfd, debugbuffer, debugcount); 228 debugcount = 0; 229 } 230 205 231 errno = saved_errno; 206 232 } 233
Note: See TracChangeset
for help on using the changeset viewer.