-
Notifications
You must be signed in to change notification settings - Fork 2
/
debug.c
122 lines (90 loc) · 2.13 KB
/
debug.c
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#include <stddef.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <fcntl.h>
#include <conio.h>
#include <dos.h>
#include "config.h"
#include "debug.h"
static int logfile = -1;
int openlog(const char *name)
{
// Name not specified.
if (name == NULL)
return 1;
// Logfile already open.
if (logfile != -1)
return 1;
if (_dos_creat(name, _A_NORMAL, &logfile) != 0) {
logmsg("_dos_creat failed to open logfile %s", name);
return 1;
}
return 0;
}
int closelog(void)
{
// If we're not logging, just return.
if (logfile == -1) {
return 1;
}
_dos_close(logfile);
logfile = -1;
return 0;
}
int logstr(const char *fmt, ...)
{
va_list ap;
unsigned len;
char line[128];
// If we're not logging, just return.
if (logfile == -1)
return 1;
va_start(ap, fmt);
portable_vsnprintf(line, sizeof(line), fmt, ap);
va_end(ap);
_dos_write(logfile, line, strlen(line), &len);
return 0;
}
int logmsg(const char *fmt, ...)
{
va_list ap;
unsigned len;
char line[128];
// If we're not logging, just return.
if (logfile == -1)
return 1;
va_start(ap, fmt);
portable_vsnprintf(line, sizeof(line), fmt, ap);
va_end(ap);
_dos_write(logfile, line, strlen(line), &len);
_dos_write(logfile, "\r\n", 2, &len);
return 0;
}
void loghex(const void far *ptr, int buflen) {
const unsigned char *buf = ptr;
int i, j;
// If we're not logging, just return.
if (logfile == -1)
return;
for (i = 0; i < buflen; i += 16) {
logstr("%06x: ", i);
for (j = 0; j < 16; j++)
if (i + j < buflen)
logstr("%02x ", buf[i+j]);
else
logstr(" ");
logstr(" ");
for (j = 0; j < 16; j++) {
if (i + j < buflen) {
if (buf[i+j] >= ' ' && buf[i+j] <= '~') {
logstr("%c", buf[i+j]);
} else {
logstr(".");
}
}
}
logstr("\r\n");
}
}