Root/jzboot/src/debug.c

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
22#include "debug.h"
23
24static int debug_level = LEVEL_ERROR;
25
26void set_debug_level(int level) {
27    debug_level = level;
28}
29
30int get_debug_level() {
31    return debug_level;
32}
33
34void debug(int level, const char *fmt, ...) {
35    va_list list;
36
37    va_start(list, fmt);
38
39    if(level <= debug_level) {
40        if(level <= LEVEL_ERROR)
41            vfprintf(stderr, fmt, list);
42        else
43            vprintf(fmt, list);
44    }
45
46    va_end(list);
47}
48
49void hexdump(const void *data, size_t size) {
50        const unsigned char *bytes = data;
51
52        for(int i = 0; i < size; i+= 16) {
53                debug(LEVEL_DEBUG, "%04X ", i);
54
55                int chunk_size = size - i;
56        if(chunk_size > 16)
57            chunk_size = 16;
58
59                for(int j = 0; j < chunk_size; j++) {
60                        debug(LEVEL_DEBUG, "%02X ", bytes[i + j]);
61
62                        if(j == 7)
63                                debug(LEVEL_DEBUG, " ");
64                }
65
66                for(int j = 0; j < 16 - chunk_size; j++) {
67                        debug(LEVEL_DEBUG, " ");
68
69                        if(j == 8)
70                                debug(LEVEL_DEBUG, " ");
71                }
72
73                debug(LEVEL_DEBUG, "|");
74
75                for(int j = 0; j < chunk_size; j++) {
76                        debug(LEVEL_DEBUG, "%c", isprint(bytes[i + j]) ? bytes[i + j] : '.');
77                }
78
79                debug(LEVEL_DEBUG, "|\n");
80        }
81}
82
83

Archive Download this file



interactive