| 1 |
//======================================================================== |
| 2 |
// |
| 3 |
// Trace.cc |
| 4 |
// |
| 5 |
// Nested tracing. |
| 6 |
// |
| 7 |
// Copyright 2020 Glyph & Cog, LLC |
| 8 |
// |
| 9 |
//======================================================================== |
| 10 |
|
| 11 |
#include <aconf.h> |
| 12 |
|
| 13 |
#if ENABLE_TRACING |
| 14 |
|
| 15 |
#include <stdio.h> |
| 16 |
#include <stdarg.h> |
| 17 |
#include <sys/types.h> |
| 18 |
#include <sys/time.h> |
| 19 |
#include <unistd.h> |
| 20 |
#include "GString.h" |
| 21 |
#include "Trace.h" |
| 22 |
|
| 23 |
// NB: This module is NOT thread-safe. |
| 24 |
|
| 25 |
static bool traceInitialized = false; |
| 26 |
static FILE *traceOut = NULL; |
| 27 |
|
| 28 |
static void traceInit() { |
| 29 |
if (traceInitialized) { |
| 30 |
return; |
| 31 |
} |
| 32 |
//~ this could read an env var to set up an output file |
| 33 |
GString *fileName = GString::format("/tmp/trace.{0:d}", (int)getpid()); |
| 34 |
traceOut = fopen(fileName->getCString(), "w"); |
| 35 |
delete fileName; |
| 36 |
traceInitialized = true; |
| 37 |
} |
| 38 |
|
| 39 |
static void traceHeader(char flag, void *handle) { |
| 40 |
timeval tv; |
| 41 |
gettimeofday(&tv, NULL); |
| 42 |
if (handle) { |
| 43 |
fprintf(traceOut, "%c %ld %06ld %p ", flag, tv.tv_sec, tv.tv_usec, handle); |
| 44 |
} else { |
| 45 |
fprintf(traceOut, "%c %ld %06ld 0x0 ", flag, tv.tv_sec, tv.tv_usec); |
| 46 |
} |
| 47 |
} |
| 48 |
|
| 49 |
void traceBegin(void *nestHandle, const char *fmt, ...) { |
| 50 |
traceInit(); |
| 51 |
if (!traceOut) { |
| 52 |
return; |
| 53 |
} |
| 54 |
traceHeader('B', nestHandle); |
| 55 |
va_list args; |
| 56 |
va_start(args, fmt); |
| 57 |
vfprintf(traceOut, fmt, args); |
| 58 |
va_end(args); |
| 59 |
fprintf(traceOut, "\n"); |
| 60 |
} |
| 61 |
|
| 62 |
void traceEnd(void *nestHandle, const char *fmt, ...) { |
| 63 |
traceInit(); |
| 64 |
if (!traceOut) { |
| 65 |
return; |
| 66 |
} |
| 67 |
traceHeader('E', nestHandle); |
| 68 |
va_list args; |
| 69 |
va_start(args, fmt); |
| 70 |
vfprintf(traceOut, fmt, args); |
| 71 |
va_end(args); |
| 72 |
fprintf(traceOut, "\n"); |
| 73 |
} |
| 74 |
|
| 75 |
void traceAlloc(void *resourceHandle, const char *fmt, ...) { |
| 76 |
traceInit(); |
| 77 |
if (!traceOut) { |
| 78 |
return; |
| 79 |
} |
| 80 |
traceHeader('A', resourceHandle); |
| 81 |
va_list args; |
| 82 |
va_start(args, fmt); |
| 83 |
vfprintf(traceOut, fmt, args); |
| 84 |
va_end(args); |
| 85 |
fprintf(traceOut, "\n"); |
| 86 |
} |
| 87 |
|
| 88 |
void traceFree(void *resourceHandle, const char *fmt, ...) { |
| 89 |
traceInit(); |
| 90 |
if (!traceOut) { |
| 91 |
return; |
| 92 |
} |
| 93 |
traceHeader('F', resourceHandle); |
| 94 |
va_list args; |
| 95 |
va_start(args, fmt); |
| 96 |
vfprintf(traceOut, fmt, args); |
| 97 |
va_end(args); |
| 98 |
fprintf(traceOut, "\n"); |
| 99 |
} |
| 100 |
|
| 101 |
void traceMessage(const char *fmt, ...) { |
| 102 |
traceInit(); |
| 103 |
if (!traceOut) { |
| 104 |
return; |
| 105 |
} |
| 106 |
traceHeader('M', NULL); |
| 107 |
va_list args; |
| 108 |
va_start(args, fmt); |
| 109 |
vfprintf(traceOut, fmt, args); |
| 110 |
va_end(args); |
| 111 |
fprintf(traceOut, "\n"); |
| 112 |
} |
| 113 |
|
| 114 |
#endif // ENABLE_TRACING |