-
Notifications
You must be signed in to change notification settings - Fork 1
/
debug.cpp
101 lines (79 loc) · 1.54 KB
/
debug.cpp
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
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <QMessageBox>
#include <Windows.h>
#include "debug.h"
#define APP_NAME "qrdbtool"
#define DEBUG_BUFFER_SIZE 1324
static int debug_level = 0;
static RedirectFunc fud, fuu;
int set_debug_level(int level)
{
int ret = debug_level;
debug_level = level;
return ret;
}
int mod_debug_level(int mod_by)
{
return set_debug_level(debug_level+mod_by);
}
int FORMAT_PRINTF UserPrintf(const char* fmt, ...)
{
char *dbg;
va_list ap;
dbg = (char *)malloc(DEBUG_BUFFER_SIZE);
if (!dbg)
return 0;
va_start(ap, fmt);
size_t len = vsnprintf(dbg, DEBUG_BUFFER_SIZE, fmt, ap);
va_end(ap);
if (fuu)
{
fuu(dbg);
}
else
{
QMessageBox::information(nullptr, APP_NAME, dbg);
}
free(dbg);
return (int)len;
}
int FORMAT_PRINTF DebugPrintf(const char* fmt, ...)
{
if (debug_level <= 0)
return 0;
char *dbg;
va_list ap;
dbg = (char *)malloc(DEBUG_BUFFER_SIZE);
if (!dbg)
return 0;
va_start(ap, fmt);
size_t len = vsnprintf(dbg, DEBUG_BUFFER_SIZE, fmt, ap);
va_end(ap);
if (fud)
{
fud(dbg);
}
else
{
if (debug_level == 1)
{
printf("%s", dbg);
}
else
{
MessageBoxA(nullptr, dbg, APP_NAME, MB_ICONWARNING);
}
}
free(dbg);
return (int)len;
}
void redirect_dprintf(RedirectFunc func)
{
fud = func;
}
void redirect_uprintf(RedirectFunc func)
{
fuu = func;
}