Thank you, Richard! Timing seems to be indeed a problem. I managed to implement it in a slightly different way, but the timing is still an issue I think. What I did is I created a function that saves log strings in a char buffer and after a pre-defined time, prints it all to the serial terminal.
Here is the function:
`void logTrace(const char *format, …)
{
//xLogMutex = xSemaphoreCreateMutex();
if (xLogMutex == NULL) {
printf("Mutex not initialized!\n");
return;
}
if (logIndex < MAX_LOGS) {
char formattedMessage[MAX_LOG_LENGTH];
va_list args;
va_start(args, format);
vsnprintf(formattedMessage, MAX_LOG_LENGTH, format, args);
va_end(args);
// Take the mutex before writing to the log array
if (strstr(formattedMessage, "Blink") != NULL)
{
if (xSemaphoreTake(xLogMutex, portMAX_DELAY) == pdTRUE)
{
strncpy(logArray[logIndex], formattedMessage, MAX_LOG_LENGTH);
logIndex++; // Increment the log index
xSemaphoreGive(xLogMutex); // Release the mutex
}
}
} else {
printf("Log array is full!\n");
}
}
void initLogging() {
// Create the mutex only once during initialization
xLogMutex = xSemaphoreCreateMutex();
if (xLogMutex == NULL) {
printf("Failed to create log mutex!\n");
}
}`
Here is the way I use it in trace macros:
`#define traceTASK_SWITCHED_IN() logTrace(“{ "name": "%s", "cat": "%s", "ph": "%s","pid": %d, "tid": "%s", "ts": %lu },”,
pcTaskGetName(pxCurrentTCB), “task”, “B”, 1, pcTaskGetName(pxCurrentTCB), (unsigned long)(xTaskGetTickCount() * portTICK_PERIOD_MS * 1000))
#define traceTASK_SWITCHED_OUT() logTrace(“{ "name": "%s", "cat": "%s", "ph": "%s","pid": %d, "tid": "%s", "ts": %lu },”,
pcTaskGetName(pxCurrentTCB), “task”, “E”, 1, pcTaskGetName(pxCurrentTCB), (unsigned long)(xTaskGetTickCount() * portTICK_PERIOD_MS * 1000))
#define traceTASK_CREATE( pxNewTCB ) logTrace(“{ "name": "%s", "cat": "%s", "ph": "%s","pid": %d, "tid": "%s", "ts": %lu },”,
pcTaskGetName((TaskHandle_t)pxNewTCB), “task”, “M”, 1, pcTaskGetName((TaskHandle_t)pxNewTCB), (unsigned long)(xTaskGetTickCount() * portTICK_PERIOD_MS * 1000))
#define traceTASK_DELETE( pxTCB ) logTrace(“{ "name": "%s", "cat": "%s", "ph": "%s","pid": %d, "tid": "%s", "ts": %lu },”,
pcTaskGetName((TaskHandle_t)pxTCB), “task”, “E”, 1, pcTaskGetName((TaskHandle_t)pxTCB), (unsigned long)(xTaskGetTickCount() * portTICK_PERIOD_MS * 1000))
#define traceTASK_SUSPEND( pxTCB ) logTrace(“{ "name": "%s", "cat": "%s", "ph": "%s","pid": %d, "tid": "%s", "ts": %lu },”,
pcTaskGetName((TaskHandle_t)pxTCB), “task”, “E”, 1, pcTaskGetName((TaskHandle_t)pxTCB), (unsigned long)(xTaskGetTickCount() * portTICK_PERIOD_MS * 1000))
#define traceTASK_RESUME(xTask) logTrace(“{ "name": "%s", "cat": "%s", "ph": "%s","pid": %d, "tid": "%s", "ts": %lu },”,
pcTaskGetName((TaskHandle_t)xTask), “task”, “B”, 1, pcTaskGetName((TaskHandle_t)xTask), (unsigned long)(xTaskGetTickCount() * portTICK_PERIOD_MS * 1000))
`
This is the way, log looks like:
[
{ “name”: “Blink Task 1”, “cat”: “task”, “ph”: “M”,“pid”: 1, “tid”: “Blink Task 1”, “ts”: 0 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “M”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 0 },
{ “name”: “Blink Task 1”, “cat”: “task”, “ph”: “B”,“pid”: 1, “tid”: “Blink Task 1”, “ts”: 0 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “B”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 0 },
{ “name”: “Blink Task 1”, “cat”: “task”, “ph”: “E”,“pid”: 1, “tid”: “Blink Task 1”, “ts”: 0 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “E”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 0 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “B”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 100000 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “E”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 100000 },
{ “name”: “Blink Task 1”, “cat”: “task”, “ph”: “B”,“pid”: 1, “tid”: “Blink Task 1”, “ts”: 200000 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “B”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 200000 },
{ “name”: “Blink Task 1”, “cat”: “task”, “ph”: “E”,“pid”: 1, “tid”: “Blink Task 1”, “ts”: 200000 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “E”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 200000 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “B”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 300000 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “E”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 300000 },
{ “name”: “Blink Task 1”, “cat”: “task”, “ph”: “B”,“pid”: 1, “tid”: “Blink Task 1”, “ts”: 400000 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “B”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 400000 },
{ “name”: “Blink Task 1”, “cat”: “task”, “ph”: “E”,“pid”: 1, “tid”: “Blink Task 1”, “ts”: 400000 },
{ “name”: “Blink Task 2”, “cat”: “task”, “ph”: “E”,“pid”: 1, “tid”: “Blink Task 2”, “ts”: 400000 },
But the result is the following: