comparison fluid-mnf/trace.c @ 311:9cecc930d78f

fluid-mnf: original source from TI, defenestrated line endings and rearranged directory structure, but no *.[ch] source file content changes yet
author Mychaela Falconia <falcon@freecalypso.org>
date Sat, 29 Feb 2020 05:36:07 +0000
parents
children
comparison
equal deleted inserted replaced
310:ae39d76d5b7a 311:9cecc930d78f
1 /******************************************************************************
2 * Generic tracing library
3 *
4 * Idea, design and coding by Mads Meisner-Jensen, mmj@ti.com
5 *
6 * $Id: trace.c 1.5 Fri, 11 Oct 2002 08:40:21 +0200 mmj $
7 *
8 ******************************************************************************/
9
10 #include "trace.h"
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdarg.h>
15
16
17 #define TRACE_MODS 2
18
19
20 /******************************************************************************
21 *
22 *****************************************************************************/
23
24 static struct trace_s {
25 int spaces; // number of spaces to indent per level
26 int level; // current indentation level (private)
27 int enable; // global trace enable/disable flag
28 FILE *fp; // file descriptor of file to write traces to
29 int mask[TRACE_MODS]; // bitmask for each module
30 } trace;
31
32 int tr_init(int spaces, char *filename)
33 {
34 trace.spaces = spaces;
35 trace.level = 0;
36 trace.enable = 1;
37 trace.fp = stdout;
38 memset(trace.mask, 0, sizeof(trace.mask));
39
40 if (filename != NULL && (trace.fp = fopen(filename, "w")) == NULL) {
41 return -1;
42 }
43 return 0;
44 }
45
46 int tr_mask(int mask)
47 {
48 int module, old;
49
50 module = ((mask & TrModMask) >> 24) - 1;
51 if (module < 0 || module >= TRACE_MODS)
52 return -1;
53
54 old = trace.mask[module];
55 trace.mask[module] = mask & TrBitMask;
56
57 return old;
58 }
59
60 void tr_enable(int onoff)
61 {
62 trace.enable = onoff;
63 }
64
65 // return current indentation if this trace type is enabled, otherwise
66 // return zero.
67 int tr_query(int mask)
68 {
69 int module;
70
71 module = (mask & TrModMask) >> 24;
72 if (module < 0 || module >= TRACE_MODS)
73 return 0;
74
75 return (trace.mask[module] & (mask & TrBitMask) ?
76 trace.level * trace.spaces : 0);
77 }
78
79 extern void hexdump(const char *p, int size, unsigned int address, int unitsize);
80
81 void tr_hexdump(int mask, const void *p, int size)
82 {
83 unsigned int module;
84
85 module = mask & TrModMask;
86 mask = mask & TrBitMask;
87
88 if ((mask & trace.mask[module >> 24]) == 0)
89 return;
90
91 hexdump(p, size, 0, 1);
92 }
93
94 void tr(int mask, char *format, ...)
95 {
96 va_list args;
97 unsigned int type, module;
98 int indent;
99 static char buf[256];
100 const char spaces[160 + 1] =
101 " "
102 " "
103 " "
104 " ";
105
106 if (!trace.enable)
107 return;
108
109 type = mask & TrTypeMask;
110 module = mask & TrModMask;
111 mask = mask & TrBitMask;
112
113 if ((mask & trace.mask[(module >> 24) - 1]) == 0)
114 return;
115
116 if (type == TrEnd)
117 trace.level--;
118
119 indent = (type == TrCont ? 0 : trace.level);
120
121 if (indent < 0 || indent > 40) {
122 indent = trace.level = 0;
123 fprintf(stderr, "WARNING: trace indent out of range!\n");
124 }
125 if (strlen(format) > 0) {
126 va_start(args, format);
127 vsprintf(buf, format, args);
128 indent *= trace.spaces;
129 fprintf(trace.fp, "%s%s", &spaces[sizeof(spaces) - 1 - indent], buf);
130 fflush(trace.fp);
131 }
132 if (type == TrBegin)
133 trace.level++;
134 }