summaryrefslogtreecommitdiff
path: root/CDAL/Debug.h
blob: cb3a503f4ed97bdd7aa591f8aa0f86b275bf4a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
/*
 * Debug.h
 *
 * Copyright (C) ST-Ericsson SA 2011
 * Authors: Srimanta Panda <srimanta.panda@stericsson.com>,
 *          Ola Borgelin <ola.borgelin@stericsson.com>,
 *          Karin Hedlund <karin.hedlund@stericsson.com>,
 *          Markus Andersson <markus.m.andersson@stericsson.com> for ST-Ericsson.
 * License terms: 3-clause BSD license
 *
 */

#pragma once

//#define _DEBUG
#define DEBUG_ERROR
#ifdef _DEBUG
#define DEBUG_INFO
#define DEBUG_HEXDUMP
#define DEBUG_HEXDUMP_SIZE 1024
#endif

#ifdef _WIN32
#define flockfile _lock_file
#define funlockfile _unlock_file
#else
#include <cstddef>
#endif

#if defined(DEBUG_ERROR) || defined(DEBUG_INFO) || defined(DEBUG_HEXDUMP)
#include <cstdio>
#include <cstdarg>
#endif

class Debug
{
public:
#ifdef _WIN32
    inline static void error(const char* format, ...) {
#else
    inline static void error(const char* format, ...) __attribute__((format(printf, 1, 0))) {
#endif
#ifdef DEBUG_ERROR
        flockfile(stdout);
        printf("ERROR - ");
        va_list args;
        va_start(args, format);
        vprintf(format, args);
        va_end(args);
        printf("\n");
        fflush(stdout);
        funlockfile(stdout);
#endif
    }

#ifdef _WIN32
    static inline void info(const char* format, ...) {
#else
    static inline void info(const char* format, ...) __attribute__((format(printf, 1, 0))) {
#endif
#ifdef DEBUG_INFO
        flockfile(stdout);
        printf("INFO - ");
        va_list args;
        va_start(args, format);
        vprintf(format, args);
        va_end(args);
        printf("\n");
        fflush(stdout);
        funlockfile(stdout);
#endif
    }

    inline static void hexdump(const char* message, unsigned char* buffer, size_t length) {
#ifdef DEBUG_HEXDUMP
        flockfile(stdout);
        printf("%s (%d):\n", message, length);
        size_t printLength = length < DEBUG_HEXDUMP_SIZE ? length : DEBUG_HEXDUMP_SIZE;

        for (size_t i = 0; i < printLength; i++) {
            printf("%02x ", buffer[i]);

            if ((i + 1) % 16 == 0 && (i + 1) < printLength)
                printf("\n");
        }

        printf("\n");

        if (printLength != length) {
            printf("%d bytes more...\n", length - printLength);
        }

        printf("\n");
        fflush(stdout);
        funlockfile(stdout);
#endif
    }
private:
    Debug() {}
    ~Debug() {}
    Debug(const Debug&) {}
};