| 52 | | if ((!(debug_level & level)) && (level != 0)) |
|---|
| 53 | | return; |
|---|
| 54 | | |
|---|
| 55 | | if (hextodump == NULL) |
|---|
| 56 | | return; |
|---|
| 57 | | |
|---|
| 58 | | for (i = 0; i < size; i++) |
|---|
| 59 | | { |
|---|
| 60 | | logstr[len++] = to_hex_char(hextodump[i] >> 4); |
|---|
| 61 | | logstr[len++] = to_hex_char(hextodump[i]); |
|---|
| 62 | | logstr[len++] = ' '; |
|---|
| 63 | | } |
|---|
| 64 | | |
|---|
| 65 | | logstr[len++] = '\n'; |
|---|
| 66 | | logstr[len] = 0; |
|---|
| 67 | | ufprintf(logfile, logstr, level); |
|---|
| 68 | | } |
|---|
| 69 | | |
|---|
| 70 | | /** |
|---|
| 71 | | * |
|---|
| 72 | | * Depending on the value of fh, we will either print to the screen, or |
|---|
| 73 | | * a log file. |
|---|
| 74 | | * |
|---|
| 75 | | */ |
|---|
| 76 | | void ufprintf(FILE *fh, char *instr, int level) |
|---|
| 77 | | { |
|---|
| 78 | | |
|---|
| 79 | | // If xsup_debug.c is used outside the main Xsupplicant source, it will |
|---|
| 80 | | // get compiler errors because of the lack of xsup_ipc.*. So, we should |
|---|
| 81 | | // define EXTERNAL_USE so that we don't attempt to build that part. |
|---|
| 82 | | /*#ifndef EXTERNAL_USE |
|---|
| 83 | | // No matter what, we want to send it to connected gui interfaces. |
|---|
| 84 | | if (level == DEBUG_NORMAL) |
|---|
| 85 | | { |
|---|
| 86 | | xsup_ipc_send_log(level, instr); |
|---|
| 87 | | } |
|---|
| 88 | | #endif*/ |
|---|
| 89 | | |
|---|
| 90 | | // No decide where else to log to. |
|---|
| 91 | | if (((isdaemon == 2) || (fh == NULL)) && (syslogging != 1)) |
|---|
| 92 | | { |
|---|
| 93 | | printf("%s", instr); |
|---|
| 94 | | fflush(stdout); |
|---|
| 95 | | } else if (syslogging ==1) { |
|---|
| 96 | | // XXX Consider ways of using other log levels. |
|---|
| 97 | | syslog(LOG_ALERT, "%s", instr); |
|---|
| 98 | | } else { |
|---|
| 99 | | fprintf(fh, "%s", instr); |
|---|
| 100 | | fflush(fh); |
|---|
| 101 | | } |
|---|
| 102 | | } |
|---|
| 103 | | |
|---|
| 104 | | /** |
|---|
| 105 | | * |
|---|
| 106 | | * dump some hex values -- also |
|---|
| 107 | | * show the ascii version of the dump. |
|---|
| 108 | | * |
|---|
| 109 | | */ |
|---|
| 110 | | void debug_hex_dump(unsigned char level, uint8_t *hextodump, int size) |
|---|
| 111 | | { |
|---|
| 112 | | int i; |
|---|
| 113 | | char buf[80]; |
|---|
| 114 | | int str_idx = 0; |
|---|
| 115 | | int chr_idx = 0; |
|---|
| 116 | | int count; |
|---|
| 117 | | int total; |
|---|
| 118 | | int tmp; |
|---|
| 119 | | |
|---|
| 120 | | if ((!(debug_level & level)) && (level != 0)) |
|---|
| 121 | | return; |
|---|
| 122 | | |
|---|
| 123 | | if (hextodump == NULL) |
|---|
| 124 | | return; |
|---|
| 125 | | |
|---|
| 126 | | /* Initialize constant fields */ |
|---|
| 127 | | memset(buf, ' ', sizeof(buf)); |
|---|
| 128 | | buf[4] = '|'; |
|---|
| 129 | | buf[54] = '|'; |
|---|
| 130 | | buf[72] = '\n'; |
|---|
| 131 | | buf[73] = 0; |
|---|
| 132 | | |
|---|
| 133 | | count = 0; |
|---|
| 134 | | total = 0; |
|---|
| 135 | | for (i = 0; i < size; i++) |
|---|
| 136 | | { |
|---|
| 137 | | if (count == 0) |
|---|
| 138 | | { |
|---|
| 139 | | str_idx = 6; |
|---|
| 140 | | chr_idx = 56; |
|---|
| 141 | | |
|---|
| 142 | | buf[0] = to_hex_char(total >> 8); |
|---|
| 143 | | buf[1] = to_hex_char(total >> 4); |
|---|
| 144 | | buf[2] = to_hex_char(total); |
|---|
| 145 | | } |
|---|
| 146 | | |
|---|
| 147 | | /* store the number */ |
|---|
| 148 | | tmp = hextodump[i]; |
|---|
| 149 | | buf[str_idx++] = to_hex_char(tmp >> 4); |
|---|
| 150 | | buf[str_idx++] = to_hex_char(tmp); |
|---|
| 151 | | str_idx++; |
|---|
| 152 | | |
|---|
| 153 | | /* store the character */ |
|---|
| 154 | | buf[chr_idx++] = isprint(tmp) ? tmp : '.'; |
|---|
| 155 | | |
|---|
| 156 | | total++; |
|---|
| 157 | | count++; |
|---|
| 158 | | if (count >= 16) |
|---|
| 159 | | { |
|---|
| 160 | | count = 0; |
|---|
| 161 | | ufprintf(logfile, buf, level); |
|---|
| 162 | | } |
|---|
| 163 | | } |
|---|
| 164 | | |
|---|
| 165 | | /* Print partial line if any */ |
|---|
| 166 | | if (count != 0) |
|---|
| 167 | | { |
|---|
| 168 | | /* Clear out any junk */ |
|---|
| 169 | | while (count < 16) |
|---|
| 170 | | { |
|---|
| 171 | | buf[str_idx] = ' '; /* MSB hex */ |
|---|
| 172 | | buf[str_idx+1] = ' '; /* LSB hex */ |
|---|
| 173 | | str_idx += 3; |
|---|
| 174 | | |
|---|
| 175 | | buf[chr_idx++] = ' '; |
|---|
| 176 | | |
|---|
| 177 | | count++; |
|---|
| 178 | | } |
|---|
| 179 | | ufprintf(logfile, buf, level); |
|---|
| 180 | | } |
|---|
| 181 | | } |
|---|
| 189 | | void debug_printf(unsigned char level, char *fmt, ...) |
|---|
| 190 | | { |
|---|
| 191 | | char dumpstr[2048], temp[2048]; |
|---|
| 192 | | |
|---|
| 193 | | if (((level & debug_level) || (level == 0)) && (fmt != NULL)) |
|---|
| 194 | | { |
|---|
| 195 | | va_list ap; |
|---|
| 196 | | va_start(ap, fmt); |
|---|
| 197 | | |
|---|
| 198 | | memset((char *)&dumpstr, 0x00, 2048); |
|---|
| 199 | | memset((char *)&temp, 0x00, 2048); |
|---|
| 200 | | |
|---|
| 201 | | // Print out a tag that identifies the type of debug message being used. |
|---|
| 202 | | switch (level) |
|---|
| 203 | | { |
|---|
| 204 | | case DEBUG_NORMAL: |
|---|
| 205 | | break; |
|---|
| 206 | | |
|---|
| 207 | | case DEBUG_CONFIG: |
|---|
| 208 | | strcpy((char *)&dumpstr, "[CONFIG] "); |
|---|
| 209 | | break; |
|---|
| 210 | | |
|---|
| 211 | | case DEBUG_STATE: |
|---|
| 212 | | strcpy((char *)&dumpstr, "[STATE] "); |
|---|
| 213 | | break; |
|---|
| 214 | | |
|---|
| 215 | | case DEBUG_TLV: |
|---|
| 216 | | strcpy((char *)&dumpstr, "[TLV] "); |
|---|
| 217 | | break; |
|---|
| 218 | | |
|---|
| 219 | | case DEBUG_INT: |
|---|
| 220 | | strcpy((char *)&dumpstr, "[INT] "); |
|---|
| 221 | | break; |
|---|
| 222 | | |
|---|
| 223 | | case DEBUG_EVERYTHING: |
|---|
| 224 | | strcpy((char *)&dumpstr, "[ALL] "); |
|---|
| 225 | | break; |
|---|
| 226 | | |
|---|
| 227 | | case DEBUG_EXCESSIVE: |
|---|
| 228 | | strcpy((char *)&dumpstr, "[EXCESSIVE] "); |
|---|
| 229 | | break; |
|---|
| 230 | | } |
|---|
| 231 | | |
|---|
| 232 | | vsnprintf((char *)&temp, 2048, fmt, ap); |
|---|
| 233 | | |
|---|
| 234 | | strcat((char *)&dumpstr, (char *)&temp); |
|---|
| 235 | | |
|---|
| 236 | | ufprintf(logfile, dumpstr, level); |
|---|
| 237 | | |
|---|
| 238 | | va_end(ap); |
|---|
| 239 | | } |
|---|
| 240 | | } |
|---|