-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathedtrace.c
384 lines (324 loc) · 8.55 KB
/
edtrace.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#include <edidentifier.h>
__CIDENT_RCSID(gr_edtrace_c,"$Id: edtrace.c,v 1.50 2024/11/19 16:18:25 cvsuser Exp $")
/* -*- mode: c; indent-width: 4; -*- */
/* $Id: edtrace.c,v 1.50 2024/11/19 16:18:25 cvsuser Exp $
* Simple diagnostic trace.
*
*
*
* Copyright (c) 1998 - 2024, Adam Young.
* All rights reserved.
*
* This file is part of the GRIEF Editor.
*
* The GRIEF Editor is free software: you can redistribute it
* and/or modify it under the terms of the GRIEF Editor License.
*
* Redistributions of source code must retain the above copyright
* notice, and must be distributed with the license document above.
*
* Redistributions in binary form must reproduce the above copyright
* notice, and must include the license document above in
* the documentation and/or other materials provided with the
* distribution.
*
* The GRIEF Editor is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* License for more details.
* ==end==
*/
#include <editor.h>
#if defined(HAVE_SYS_TIME_H)
#include <sys/time.h>
#endif
#include <time.h>
#include <stdarg.h>
#include <assert.h>
#include <edconfig.h>
#include <edtrace.h>
#include <edenv.h> /* gputenvv(), ggetenv() */
#include <libstr.h>
static int debug_open(void);
static void debug_pad(const char *prefix, int depth);
static unsigned x_debug_flags = 0; /* EDDEBUG_Fxxx */
static const char * x_debug_log = NULL;
static FILE * x_debug_file = NULL;
int
trace_filename(const char *name)
{
#if (defined(_WIN32) || defined(WIN32)) && defined(_MSC_VER)
if (x_debug_log) {
free((void *)x_debug_log);
}
x_debug_log = (name ? _strdup(name) : NULL);
#else
if (x_debug_log) {
free((void *)x_debug_log);
}
x_debug_log = (name ? strdup(name) : NULL);
#endif
if (x_debug_file) {
fclose(x_debug_file);
x_debug_file = NULL;
}
return debug_open();
}
void
trace_active(unsigned flags)
{
x_debug_flags = flags;
}
int
trace_isactive(void)
{
return (x_debug_flags && x_debug_file);
}
FILE *
trace_stream(void)
{
return x_debug_file;
}
void
trace_flush(void)
{
if (x_debug_file) {
fflush(x_debug_file);
}
}
static void
hex(const unsigned char *data, int length, int offsets, const char *term)
{
#define HEXWIDTH 16
char buf1[HEXWIDTH * 4], buf2[HEXWIDTH * 2];
const unsigned char *end = data + length;
unsigned offset = 0, len1, len2;
int done;
if (0 == x_debug_flags || 0 == length) {
return;
}
buf1[len1 = 0] = 0;
len2 = 0;
if (NULL == term) term = "\n";
do {
const unsigned ch = *data++;
done = (data == end);
len1 += sprintf(buf1 + len1, "%02x ", ch);
len2 += sprintf(buf2 + len2, "%c", (ch >= ' ' && ch < 0x7f ? ch : '.'));
if ((offset++ && 0 == (offset % HEXWIDTH)) || done) {
if (offsets < 0) { /* unpadded */
trace_log("%02x: %s | %s |%s", length, buf1, buf2, term);
} else {
if (offsets) {
trace_log("%04x: %-*s | %-*s |%s", offset - HEXWIDTH, HEXWIDTH*3, buf1, HEXWIDTH, buf2, term);
} else {
trace_log("| %-*s | %-*s |%s", HEXWIDTH*3, buf1, HEXWIDTH, buf2, term);
}
}
buf1[len1 = 0] = 0;
len2 = 0;
}
} while (! done);
}
static int
debug_open(void)
{
if (NULL == x_debug_file) {
const char *name;
if (NULL == (name = ggetenv("GRIEF_LOG"))) {
name = x_debug_log;
}
x_debug_file = fopen((name ? name : GRLOG_FILE), "w");
#if !defined(DOSISH)
if (NULL == x_debug_file) {
#if defined(__OS2__)
x_debug_file = fopen(GRLOG_FILE, "w");
#else
x_debug_file = fopen("/tmp/" GRLOG_FILE, "w");
#endif
}
#endif /*DOSISH*/
}
return (x_debug_file != NULL);
}
static void
debug_pad(const char *prefix, int depth)
{
char buf[1024], *p; /* MAGIC */
int len, i;
strxcpy(buf, (prefix ? prefix : ""), sizeof(buf));
len = (int)strlen(buf);
if ((i = depth) > 0) {
if (depth > len) {
depth = len;
}
p = buf + len;
while (i-- > 0) {
*p++ = '.';
}
*p = '\0';
}
trace_str(buf);
}
int
trace_log(const char *str, ...)
{
va_list ap;
int ret;
va_start(ap, str);
ret = trace_logv(str, ap);
va_end(ap);
return ret;
}
int
trace_logv(const char *fmt, va_list ap)
{
int ret = 0;
if (x_debug_flags) {
if (debug_open()) {
ret = vfprintf(x_debug_file, fmt, ap);
if (EDTRACE_FFLUSH & x_debug_flags) {
fflush(x_debug_file);
}
}
}
return ret;
}
void
trace_logx(int level, const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
trace_logxv(level, fmt, ap);
va_end(ap);
}
static const struct tm *
my_localtime(void)
{
const time_t ctm = time(NULL);
static struct tm x_ltm;
static time_t x_ctm;
if (ctm != x_ctm) {
if (ctm == (x_ctm + 1)) {
if (++x_ltm.tm_sec >= 60) {
x_ltm.tm_sec = 0;
if (++x_ltm.tm_min >= 60) {
x_ltm.tm_min = 0;
if (++x_ltm.tm_hour >= 24) {
#if defined(HAVE_LOCALTIME_R)
(void) localtime_r(&ctm, &x_ltm);
#else
x_ltm = *localtime(&ctm);
#endif
}
}
}
} else {
#if defined(HAVE_LOCALTIME_R)
(void) localtime_r(&ctm, &x_ltm);
#else
x_ltm = *localtime(&ctm);
#endif
}
x_ctm = ctm;
}
return &x_ltm;
}
void
trace_logxv(int level, const char *fmt, va_list ap)
{
static int old_level = -1;
char buf[32];
if (x_debug_flags) {
#if (TODO)
if (EDDEBUG_TIME & x_debug_flags) {
const struct tm *tp = tp = my_localtime();
sprintf(buf, "%02d:%02d:%02d.%d:",
tp->tm_year + 1900, tp->tm_min, tp->tm_sec, subsec);
}
#endif
if (old_level == level) {
strcpy(buf, "\t");
} else {
sprintf(buf, "%02d:..", level);
}
debug_pad(buf, level);
old_level = level;
if (debug_open()) {
vfprintf(x_debug_file, fmt, ap);
if (EDTRACE_FFLUSH & x_debug_flags) {
fflush(x_debug_file);
}
}
}
}
void
trace_hex(const void *data, int length)
{
hex(data, length, (length <= HEXWIDTH ? -1 : 1), "\n");
}
void
trace_data(const void *data, int length, const char *term)
{
hex(data, length, FALSE, (term ? term : "\n"));
}
int
trace_str(const char *str)
{
int ret = 0;
if (x_debug_flags) {
if (str && debug_open()) {
ret = fputs(str, x_debug_file);
if (EDTRACE_FFLUSH & x_debug_flags) {
fflush(x_debug_file);
}
}
}
return ret;
}
/*
* MT-LEVEL:
* Not MT safe since the returned address shall be a reference to a local
* static buffer.
*/
const char *
c_string(const char *str)
{
static char buf[4 * 1024]; /* MAGIC */
const char *endbp = (buf + (sizeof(buf) - 10));
register char *bp = buf;
char c;
if (0 == x_debug_flags) {
return str;
}
while (0 != (c = *str++)) {
switch (c) { /* escapes */
case '\a': *bp++ = '\\', *bp++ = 'a'; break;
case '\b': *bp++ = '\\', *bp++ = 'b'; break;
case '\f': *bp++ = '\\', *bp++ = 'f'; break;
case '\n': *bp++ = '\\', *bp++ = 'n'; break;
case '\r': *bp++ = '\\', *bp++ = 'r'; break;
case '\t': *bp++ = '\\', *bp++ = 't'; break;
case '\v': *bp++ = '\\', *bp++ = 'v'; break;
case '\\': *bp++ = '\\', *bp++ = '\\'; break;
case 0x1b: *bp++ = '^', *bp++ = '['; break;
default:
if (c < ' ') { /* oct */
sprintf(bp, "\\%03o", (unsigned char)c);
bp += 4;
} else { /* otherwise direct */
*bp++ = c;
}
break;
}
if (bp > endbp) { /* truncate */
*bp++ = '.';
*bp++ = '.';
*bp++ = '.';
break;
}
}
*bp = '\0'; /* terminate */
return buf;
}
/*end*/