Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alexbevi
GitHub Repository: alexbevi/BizHawk
Path: blob/master/yabause/src/debug.c
2 views
1
/* Copyright 2005 Guillaume Duhamel
2
Copyright 2006 Theo Berkau
3
4
This file is part of Yabause.
5
6
Yabause is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
11
Yabause is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License
17
along with Yabause; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
*/
20
21
#include "debug.h"
22
23
#include <stdarg.h>
24
#include <stdlib.h>
25
#include <string.h>
26
27
//////////////////////////////////////////////////////////////////////////////
28
29
Debug * DebugInit(const char * n, DebugOutType t, char * s) {
30
Debug * d;
31
32
if ((d = (Debug *) malloc(sizeof(Debug))) == NULL)
33
return NULL;
34
35
d->output_type = t;
36
37
if ((d->name = strdup(n)) == NULL)
38
{
39
free(d);
40
return NULL;
41
}
42
43
switch(t) {
44
case DEBUG_STREAM:
45
d->output.stream = fopen(s, "w");
46
break;
47
case DEBUG_STRING:
48
d->output.string = s;
49
break;
50
case DEBUG_STDOUT:
51
d->output.stream = stdout;
52
break;
53
case DEBUG_STDERR:
54
d->output.stream = stderr;
55
break;
56
case DEBUG_CALLBACK:
57
d->output.callback = (void (*) (char*))s;
58
break;
59
}
60
61
return d;
62
}
63
64
//////////////////////////////////////////////////////////////////////////////
65
66
void DebugDeInit(Debug * d) {
67
if (d == NULL)
68
return;
69
70
switch(d->output_type) {
71
case DEBUG_STREAM:
72
if (d->output.stream)
73
fclose(d->output.stream);
74
break;
75
case DEBUG_STRING:
76
case DEBUG_STDOUT:
77
case DEBUG_STDERR:
78
case DEBUG_CALLBACK:
79
break;
80
}
81
if (d->name)
82
free(d->name);
83
free(d);
84
}
85
86
//////////////////////////////////////////////////////////////////////////////
87
88
void DebugChangeOutput(Debug * d, DebugOutType t, char * s) {
89
if (t != d->output_type) {
90
if (d->output_type == DEBUG_STREAM)
91
{
92
if (d->output.stream)
93
fclose(d->output.stream);
94
}
95
d->output_type = t;
96
}
97
switch(t) {
98
case DEBUG_STREAM:
99
d->output.stream = fopen(s, "w");
100
break;
101
case DEBUG_STRING:
102
d->output.string = s;
103
break;
104
case DEBUG_CALLBACK:
105
d->output.callback = (void (*) (char*))s;
106
break;
107
case DEBUG_STDOUT:
108
d->output.stream = stdout;
109
break;
110
case DEBUG_STDERR:
111
d->output.stream = stderr;
112
break;
113
}
114
}
115
116
//////////////////////////////////////////////////////////////////////////////
117
118
void DebugPrintf(Debug * d, const char * file, u32 line, const char * format, ...) {
119
va_list l;
120
static char strtmp[512];
121
static int strhash;
122
123
if (d == NULL)
124
return;
125
126
va_start(l, format);
127
128
switch(d->output_type) {
129
case DEBUG_STDOUT:
130
case DEBUG_STDERR:
131
case DEBUG_STREAM:
132
if (d->output.stream == NULL)
133
break;
134
fprintf(d->output.stream, "%s (%s:%ld): ", d->name, file, (long)line);
135
vfprintf(d->output.stream, format, l);
136
break;
137
case DEBUG_STRING:
138
{
139
int i;
140
if (d->output.string == NULL)
141
break;
142
143
i = sprintf(d->output.string, "%s (%s:%ld): ", d->name, file, (long)line);
144
vsprintf(d->output.string + i, format, l);
145
}
146
break;
147
case DEBUG_CALLBACK:
148
{
149
int i;
150
int strnewhash = 0;
151
i = sprintf(strtmp, "%s (%s:%ld): ", d->name, file, (long)line);
152
i += vsprintf(strtmp + i, format, l);
153
for ( ; i>0 ; i-- ) strnewhash += (int)(strtmp[i]);
154
if ( strnewhash != strhash ) d->output.callback( strtmp );
155
strhash = strnewhash;
156
}
157
break;
158
}
159
160
va_end(l);
161
}
162
163
//////////////////////////////////////////////////////////////////////////////
164
165
Debug * MainLog;
166
167
//////////////////////////////////////////////////////////////////////////////
168
169
void LogStart(void) {
170
MainLog = DebugInit("main", DEBUG_STDOUT, NULL);
171
// MainLog = DebugInit("main", DEBUG_STREAM, "stdout.txt");
172
}
173
174
//////////////////////////////////////////////////////////////////////////////
175
176
void LogStop(void) {
177
DebugDeInit(MainLog);
178
MainLog = NULL;
179
}
180
181
//////////////////////////////////////////////////////////////////////////////
182
183
void LogChangeOutput(DebugOutType t, char * s) {
184
185
DebugChangeOutput( MainLog, t, s );
186
}
187
188