forked from percepio/TraceRecorderSource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
trcPrint.c
314 lines (264 loc) · 8.79 KB
/
trcPrint.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
/*
* Percepio Trace Recorder for Tracealyzer v4.6.3
* Copyright 2021 Percepio AB
* www.percepio.com
*
* SPDX-License-Identifier: Apache-2.0
*
* The implementation for print.
*/
#include <trcRecorder.h>
#if (TRC_USE_TRACEALYZER_RECORDER == 1)
#if (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING)
#if (TRC_CFG_SCHEDULING_ONLY == 0) && (TRC_CFG_INCLUDE_USER_EVENTS == 1)
#include <stdarg.h>
static traceResult prvTraceVPrintF(TraceStringHandle_t xChannel, const char* szFormat, uint32_t uiLength, uint32_t uiArgs, va_list *pxVL);
typedef struct TracePrintInfo
{
TraceStringHandle_t defaultChannel;
TraceStringHandle_t consoleChannel;
} TracePrintInfo_t;
static TracePrintInfo_t *pxPrintInfo;
traceResult xTracePrintInitialize(TracePrintBuffer_t *pxBuffer)
{
TRC_ASSERT_EQUAL_SIZE(TracePrintBuffer_t, TracePrintInfo_t);
/* This should never fail */
TRC_ASSERT(pxBuffer != 0);
pxPrintInfo = (TracePrintInfo_t*)pxBuffer;
pxPrintInfo->defaultChannel = 0;
pxPrintInfo->consoleChannel = 0;
xTraceSetComponentInitialized(TRC_RECORDER_COMPONENT_PRINT);
return TRC_SUCCESS;
}
/******************************************************************************
* xTracePrint
*
* Generates "User Events", with unformatted text.
*
* User Events can be used for very efficient application logging, and are shown
* as yellow labels in the main trace view.
*
* You may group User Events into User Event Channels. The yellow User Event
* labels shows the logged string, preceded by the channel name within
* brackets. For example:
*
* "[MyChannel] Hello World!"
*
* The User Event Channels are shown in the View Filter, which makes it easy to
* select what User Events you wish to display. User Event Channels are created
* using xTraceStringRegister().
*
* Example:
*
* TraceStringHandle_t xChannel = xTraceStringRegister("MyChannel");
* ...
* xTracePrint(xChannel, "Hello World!");
*
******************************************************************************/
traceResult xTracePrint(TraceStringHandle_t xChannel, const char* szString)
{
uint32_t uiLength = 0;
uint32_t i = 0;
/* We need to check this */
if (!xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_PRINT))
{
return TRC_FAIL;
}
if (szString == 0)
{
szString = "";
}
while ((szString[i] != 0) && (i < 128))
{
i++;
}
uiLength = i + 1; /* Null termination */
return prvTraceVPrintF(xChannel, szString, uiLength, 0, (va_list*)0);
}
/*******************************************************************************
* xTraceConsoleChannelPrintF
*
* Wrapper for vTracePrint, using the default channel. Can be used as a drop-in
* replacement for printf and similar functions, e.g. in a debug logging macro.
*
* Example:
*
* // Old: #define LogString debug_console_printf
*
* // New, log to Tracealyzer instead:
* #define LogString xTraceConsoleChannelPrintF
* ...
* LogString("My value is: %d", myValue);
******************************************************************************/
traceResult xTraceConsoleChannelPrintF(const char* szFormat, ...)
{
traceResult xResult;
va_list xVL;
/* We need to check this */
if (!xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_PRINT))
{
return TRC_FAIL;
}
if (pxPrintInfo->consoleChannel == 0)
{
if (xTraceStringRegister("Debug Console", &pxPrintInfo->consoleChannel) == TRC_FAIL)
{
return TRC_FAIL;
}
}
va_start(xVL, szFormat);
xResult = xTraceVPrintF(pxPrintInfo->consoleChannel, szFormat, xVL);
va_end(xVL);
return xResult;
}
/******************************************************************************
* xTracePrintF
*
* Generates "User Events", with formatted text and data, similar to a "printf".
* It is very fast since the actual formatting is done on the host side when the
* trace is displayed.
*
* User Events can be used for very efficient application logging, and are shown
* as yellow labels in the main trace view.
* An advantage of User Events is that data can be plotted in the "User Event
* Signal Plot" view, visualizing any data you log as User Events, discrete
* states or control system signals (e.g. system inputs or outputs).
*
* You may group User Events into User Event Channels. The yellow User Event
* labels show the logged string, preceded by the channel name within brackets.
*
* Example:
*
* "[MyChannel] Hello World!"
*
* The User Event Channels are shown in the View Filter, which makes it easy to
* select what User Events you wish to display. User Event Channels are created
* using xTraceStringRegister().
*
* Example:
*
* TraceStringHandle_t adc_uechannel = xTraceStringRegister("ADC User Events");
* ...
* xTracePrintF(adc_uechannel,
* "ADC channel %d: %d volts",
* ch, adc_reading);
*
* All data arguments are assumed to be 32 bit wide. The following formats are
* supported:
* %d - signed integer. The following width and padding format is supported: "%05d" -> "-0042" and "%5d" -> " -42"
* %u - unsigned integer. The following width and padding format is supported: "%05u" -> "00042" and "%5u" -> " 42"
* %X - hexadecimal (uppercase). The following width and padding format is supported: "%04X" -> "002A" and "%4X" -> " 2A"
* %x - hexadecimal (lowercase). The following width and padding format is supported: "%04x" -> "002a" and "%4x" -> " 2a"
* %s - string (currently, this must be an earlier stored symbol name)
*
* Up to 15 data arguments are allowed, with a total size of maximum 60 byte
* including 8 byte for the base event fields and the format string. So with
* one data argument, the maximum string length is 48 chars. If this is exceeded
* the string is truncated (4 bytes at a time).
*
******************************************************************************/
traceResult xTracePrintF(TraceStringHandle_t xChannel, const char* szFormat, ...)
{
traceResult xResult;
va_list xVL;
/* We need to check this */
if (!xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_PRINT))
{
return TRC_FAIL;
}
va_start(xVL, szFormat);
xResult = xTraceVPrintF(xChannel, szFormat, xVL);
va_end(xVL);
return xResult;
}
/******************************************************************************
* xTraceVPrintF
*
* xTracePrintF variant that accepts a va_list.
* See xTraceVPrintF documentation for further details.
*
******************************************************************************/
traceResult xTraceVPrintF(TraceStringHandle_t xChannel, const char* szFormat, va_list xVL)
{
uint32_t i = 0;
uint32_t uiArgs = 0;
uint32_t uiLength;
/* We need to check this */
if (!xTraceIsComponentInitialized(TRC_RECORDER_COMPONENT_PRINT))
{
return TRC_FAIL;
}
if (szFormat == 0)
{
szFormat = "";
}
/* Count the number of arguments in the format string (e.g., %d) */
for (i = 0; (szFormat[i] != 0) && (i < 128); i++)
{
if (szFormat[i] == '%')
{
if (szFormat[i + 1] == 0)
{
/* Found end of string, let for loop detect it */
continue;
}
if (szFormat[i + 1] != '%')
{
uiArgs++; /* Found an argument */
}
i++; /* Move past format specifier or non-argument '%' */
}
}
uiLength = i + 1; /* Null termination */
return prvTraceVPrintF(xChannel, szFormat, uiLength, uiArgs, &xVL);
}
static traceResult prvTraceVPrintF(TraceStringHandle_t xChannel, const char* szFormat, uint32_t uiLength, uint32_t uiArgs, va_list *pxVL)
{
TraceEventHandle_t xEventHandle = 0;
uint32_t i, uiRemaining;
uint32_t uiValue;
uint32_t uiEventCode = PSF_EVENT_USER_EVENT + 1 + uiArgs; /* Add channel (1) */
uint32_t uiSize = sizeof(void*) + uiArgs * sizeof(TraceUnsignedBaseType_t) + uiLength; /* Add channel (sizeof(void*)) */
if (xChannel == 0)
{
if (pxPrintInfo->defaultChannel == 0)
{
/* Channel is not present */
if (xTraceStringRegister("Default", &pxPrintInfo->defaultChannel) == TRC_FAIL)
{
return TRC_FAIL;
}
}
xChannel = pxPrintInfo->defaultChannel;
}
/* Added channel to uiEventCode and uiSize */
if (xTraceEventBegin(uiEventCode, uiSize , &xEventHandle) == TRC_FAIL)
{
return TRC_FAIL;
}
/* Add xChannel */
xTraceEventAddPointer(xEventHandle, (void*)xChannel);
/* Add all arguments */
for (i = 0; i < uiArgs; i++)
{
xTraceEventAddUnsignedBaseType(xEventHandle, va_arg(*pxVL, TraceUnsignedBaseType_t));
}
xTraceEventPayloadRemaining(xEventHandle, &uiRemaining);
if (uiRemaining < uiLength)
{
uiLength = uiRemaining - 1; /* Make room for null termination */
}
/* Add format string */
xTraceEventAddData(xEventHandle, (void*)szFormat, uiLength);
/* Check if we can truncate */
xTraceEventPayloadRemaining(xEventHandle, &uiValue);
if (uiValue > 0)
{
xTraceEventAdd8(xEventHandle, 0);
}
xTraceEventEnd(xEventHandle);
return TRC_SUCCESS;
}
#endif /* (TRC_CFG_SCHEDULING_ONLY == 0) && (TRC_CFG_INCLUDE_USER_EVENTS == 1) */
#endif /* (TRC_CFG_RECORDER_MODE == TRC_RECORDER_MODE_STREAMING) */
#endif /* (TRC_USE_TRACEALYZER_RECORDER == 1) */