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
|
//========================================================================
//
// Trace.cc
//
// Nested tracing.
//
// Copyright 2020 Glyph & Cog, LLC
//
//========================================================================
#include <aconf.h>
#if ENABLE_TRACING
#include <stdio.h>
#include <stdarg.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include "GString.h"
#include "Trace.h"
// NB: This module is NOT thread-safe.
static bool traceInitialized = false;
static FILE *traceOut = NULL;
static void traceInit() {
if (traceInitialized) {
return;
}
//~ this could read an env var to set up an output file
GString *fileName = GString::format("/tmp/trace.{0:d}", (int)getpid());
traceOut = fopen(fileName->getCString(), "w");
delete fileName;
traceInitialized = true;
}
static void traceHeader(char flag, void *handle) {
timeval tv;
gettimeofday(&tv, NULL);
if (handle) {
fprintf(traceOut, "%c %ld %06ld %p ", flag, tv.tv_sec, tv.tv_usec, handle);
} else {
fprintf(traceOut, "%c %ld %06ld 0x0 ", flag, tv.tv_sec, tv.tv_usec);
}
}
void traceBegin(void *nestHandle, const char *fmt, ...) {
traceInit();
if (!traceOut) {
return;
}
traceHeader('B', nestHandle);
va_list args;
va_start(args, fmt);
vfprintf(traceOut, fmt, args);
va_end(args);
fprintf(traceOut, "\n");
}
void traceEnd(void *nestHandle, const char *fmt, ...) {
traceInit();
if (!traceOut) {
return;
}
traceHeader('E', nestHandle);
va_list args;
va_start(args, fmt);
vfprintf(traceOut, fmt, args);
va_end(args);
fprintf(traceOut, "\n");
}
void traceAlloc(void *resourceHandle, const char *fmt, ...) {
traceInit();
if (!traceOut) {
return;
}
traceHeader('A', resourceHandle);
va_list args;
va_start(args, fmt);
vfprintf(traceOut, fmt, args);
va_end(args);
fprintf(traceOut, "\n");
}
void traceFree(void *resourceHandle, const char *fmt, ...) {
traceInit();
if (!traceOut) {
return;
}
traceHeader('F', resourceHandle);
va_list args;
va_start(args, fmt);
vfprintf(traceOut, fmt, args);
va_end(args);
fprintf(traceOut, "\n");
}
void traceMessage(const char *fmt, ...) {
traceInit();
if (!traceOut) {
return;
}
traceHeader('M', NULL);
va_list args;
va_start(args, fmt);
vfprintf(traceOut, fmt, args);
va_end(args);
fprintf(traceOut, "\n");
}
#endif // ENABLE_TRACING
|