| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | |
|---|
| 16 | |
|---|
| 17 | |
|---|
| 18 | |
|---|
| 19 | |
|---|
| 20 | |
|---|
| 21 | |
|---|
| 22 | #include "internal.h" |
|---|
| 23 | #include <stdarg.h> |
|---|
| 24 | #include <stdlib.h> |
|---|
| 25 | #include <assert.h> |
|---|
| 26 | #include <ctype.h> |
|---|
| 27 | #include <string.h> |
|---|
| 28 | #ifdef HAVE_UNISTD_H |
|---|
| 29 | #include <unistd.h> |
|---|
| 30 | #endif |
|---|
| 31 | #ifdef HAVE_SYS_TIME_H |
|---|
| 32 | #include <sys/time.h> |
|---|
| 33 | #endif |
|---|
| 34 | #ifdef HAVE_IO_H |
|---|
| 35 | #include <io.h> |
|---|
| 36 | #endif |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | void _sc_error(sc_context_t *ctx, const char *format, ...) |
|---|
| 40 | { |
|---|
| 41 | va_list ap; |
|---|
| 42 | |
|---|
| 43 | va_start(ap, format); |
|---|
| 44 | sc_do_log_va(ctx, SC_LOG_TYPE_ERROR, NULL, 0, NULL, format, ap); |
|---|
| 45 | va_end(ap); |
|---|
| 46 | } |
|---|
| 47 | |
|---|
| 48 | |
|---|
| 49 | void _sc_debug(sc_context_t *ctx, const char *format, ...) |
|---|
| 50 | { |
|---|
| 51 | va_list ap; |
|---|
| 52 | |
|---|
| 53 | va_start(ap, format); |
|---|
| 54 | sc_do_log_va(ctx, SC_LOG_TYPE_DEBUG, NULL, 0, NULL, format, ap); |
|---|
| 55 | va_end(ap); |
|---|
| 56 | } |
|---|
| 57 | |
|---|
| 58 | void sc_do_log(sc_context_t *ctx, int type, const char *file, int line, const char *func, const char *format, ...) |
|---|
| 59 | { |
|---|
| 60 | va_list ap; |
|---|
| 61 | |
|---|
| 62 | va_start(ap, format); |
|---|
| 63 | sc_do_log_va(ctx, type, file, line, func, format, ap); |
|---|
| 64 | va_end(ap); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | void sc_do_log_va(sc_context_t *ctx, int type, const char *file, int line, const char *func, const char *format, va_list args) |
|---|
| 68 | { |
|---|
| 69 | int (*display_fn)(sc_context_t *, const char *); |
|---|
| 70 | char buf[1836], *p; |
|---|
| 71 | const char *tag = ""; |
|---|
| 72 | int r; |
|---|
| 73 | size_t left; |
|---|
| 74 | |
|---|
| 75 | assert(ctx != NULL); |
|---|
| 76 | |
|---|
| 77 | switch (type) { |
|---|
| 78 | case SC_LOG_TYPE_ERROR: |
|---|
| 79 | if (!ctx->suppress_errors) { |
|---|
| 80 | display_fn = &sc_ui_display_error; |
|---|
| 81 | tag = "error:"; |
|---|
| 82 | break; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | |
|---|
| 86 | tag = "error (suppressed):"; |
|---|
| 87 | type = SC_LOG_TYPE_DEBUG; |
|---|
| 88 | |
|---|
| 89 | case SC_LOG_TYPE_DEBUG: |
|---|
| 90 | if (ctx->debug == 0) |
|---|
| 91 | return; |
|---|
| 92 | display_fn = &sc_ui_display_debug; |
|---|
| 93 | break; |
|---|
| 94 | |
|---|
| 95 | default: |
|---|
| 96 | return; |
|---|
| 97 | } |
|---|
| 98 | |
|---|
| 99 | if (file != NULL) { |
|---|
| 100 | r = snprintf(buf, sizeof(buf), "[%s] %s:%d:%s: ", |
|---|
| 101 | ctx->app_name, file, line, func ? func : ""); |
|---|
| 102 | if (r < 0 || (unsigned int)r > sizeof(buf)) |
|---|
| 103 | return; |
|---|
| 104 | } else { |
|---|
| 105 | r = 0; |
|---|
| 106 | } |
|---|
| 107 | p = buf + r; |
|---|
| 108 | left = sizeof(buf) - r; |
|---|
| 109 | |
|---|
| 110 | r = vsnprintf(p, left, format, args); |
|---|
| 111 | if (r < 0) |
|---|
| 112 | return; |
|---|
| 113 | p += r; |
|---|
| 114 | left -= r; |
|---|
| 115 | |
|---|
| 116 | display_fn(ctx, buf); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | void sc_hex_dump(sc_context_t *ctx, const u8 * in, size_t count, char *buf, size_t len) |
|---|
| 120 | { |
|---|
| 121 | char *p = buf; |
|---|
| 122 | int lines = 0; |
|---|
| 123 | |
|---|
| 124 | assert(buf != NULL && in != NULL); |
|---|
| 125 | buf[0] = 0; |
|---|
| 126 | if ((count * 5) > len) |
|---|
| 127 | return; |
|---|
| 128 | while (count) { |
|---|
| 129 | char ascbuf[17]; |
|---|
| 130 | size_t i; |
|---|
| 131 | |
|---|
| 132 | for (i = 0; i < count && i < 16; i++) { |
|---|
| 133 | sprintf(p, "%02X ", *in); |
|---|
| 134 | if (isprint(*in)) |
|---|
| 135 | ascbuf[i] = *in; |
|---|
| 136 | else |
|---|
| 137 | ascbuf[i] = '.'; |
|---|
| 138 | p += 3; |
|---|
| 139 | in++; |
|---|
| 140 | } |
|---|
| 141 | count -= i; |
|---|
| 142 | ascbuf[i] = 0; |
|---|
| 143 | for (; i < 16 && lines; i++) { |
|---|
| 144 | strcat(p, " "); |
|---|
| 145 | p += 3; |
|---|
| 146 | } |
|---|
| 147 | strcat(p, ascbuf); |
|---|
| 148 | p += strlen(p); |
|---|
| 149 | sprintf(p, "\n"); |
|---|
| 150 | p++; |
|---|
| 151 | lines++; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|