| 1 | /* |
| 2 | * JzBoot: an USB bootloader for JZ series of Ingenic(R) microprocessors. |
| 3 | * Copyright (C) 2010 Sergey Gridassov <grindars@gmail.com> |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | */ |
| 18 | |
| 19 | #include <stdarg.h> |
| 20 | #include <stdio.h> |
| 21 | #include <ctype.h> |
| 22 | |
| 23 | #include "debug.h" |
| 24 | |
| 25 | static int debug_level = LEVEL_ERROR; |
| 26 | |
| 27 | void set_debug_level(int level) { |
| 28 | debug_level = level; |
| 29 | } |
| 30 | |
| 31 | int get_debug_level() { |
| 32 | return debug_level; |
| 33 | } |
| 34 | |
| 35 | void debug(int level, const char *fmt, ...) { |
| 36 | va_list list; |
| 37 | |
| 38 | va_start(list, fmt); |
| 39 | |
| 40 | if(level <= debug_level) { |
| 41 | if(level <= LEVEL_ERROR) |
| 42 | vfprintf(stderr, fmt, list); |
| 43 | else |
| 44 | vprintf(fmt, list); |
| 45 | } |
| 46 | |
| 47 | va_end(list); |
| 48 | } |
| 49 | |
| 50 | void hexdump(const void *data, size_t size) { |
| 51 | const unsigned char *bytes = data; |
| 52 | |
| 53 | for(int i = 0; i < size; i+= 16) { |
| 54 | debug(LEVEL_DEBUG, "%04X ", i); |
| 55 | |
| 56 | int chunk_size = size - i; |
| 57 | if(chunk_size > 16) |
| 58 | chunk_size = 16; |
| 59 | |
| 60 | for(int j = 0; j < chunk_size; j++) { |
| 61 | debug(LEVEL_DEBUG, "%02X ", bytes[i + j]); |
| 62 | |
| 63 | if(j == 7) |
| 64 | debug(LEVEL_DEBUG, " "); |
| 65 | } |
| 66 | |
| 67 | for(int j = 0; j < 16 - chunk_size; j++) { |
| 68 | debug(LEVEL_DEBUG, " "); |
| 69 | |
| 70 | if(j == 8) |
| 71 | debug(LEVEL_DEBUG, " "); |
| 72 | } |
| 73 | |
| 74 | debug(LEVEL_DEBUG, "|"); |
| 75 | |
| 76 | for(int j = 0; j < chunk_size; j++) { |
| 77 | debug(LEVEL_DEBUG, "%c", isprint(bytes[i + j]) ? bytes[i + j] : '.'); |
| 78 | } |
| 79 | |
| 80 | debug(LEVEL_DEBUG, "|\n"); |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | |