Skip to content

Commit edea669

Browse files
committed
Asked Copilot for help
1 parent 2676d67 commit edea669

1 file changed

Lines changed: 29 additions & 51 deletions

File tree

src/main.cpp

Lines changed: 29 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#include <esp_log.h>
88
#include <temperature_sensor.h>
99

10+
#include <cstring>
11+
1012
// Library Includes
1113
#include <Arduino.h>
1214

@@ -491,70 +493,47 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
491493
// by 3?
492494
constexpr uint_fast16_t STATS_BUFFER_SIZE =
493495
REC_BYTES_PER_TASK * (NUM_MAIN_TASKS + NUM_ESP_TASKS);
494-
static char statsBuffer[STATS_BUFFER_SIZE] = {'\0'};
496+
char statsBuffer[STATS_BUFFER_SIZE] = {'\0'};
495497
if (uxTaskGetNumberOfTasks() > NUM_MAIN_TASKS + NUM_ESP_TASKS) {
496498
LOGE_LOCKED(
497499
TAG,
498500
"Number of tasks (%d) exceeds expected max (%d), skipping to "
499501
"prevent memory corruption",
500502
uxTaskGetNumberOfTasks(), NUM_MAIN_TASKS + NUM_ESP_TASKS);
501-
// Serial.flush();
502503
delay(LOG_ITEM_INTERVAL_MS);
503-
// } else {
504-
} else if (statsBuffer[0] == '\0') {
504+
} else {
505+
if (Logging::takeLogMutex()) {
505506
// Refresh stats buffer
506-
507-
// TODO: Not recommended in production
508507
vTaskGetRunTimeStats(statsBuffer);
509-
statsBuffer[STATS_BUFFER_SIZE - 1] =
510-
'\0'; // hard cap, avoid over-read
511-
// uxTaskGetSystemState();
508+
statsBuffer[STATS_BUFFER_SIZE - 1] = '\0';
509+
512510
size_t usedBytes = strnlen(statsBuffer, STATS_BUFFER_SIZE);
513-
LOGD_LOCKED(TAG, "Task Buffer Used (%): %d",
514-
usedBytes * 100 / sizeof(statsBuffer));
515-
LOGD_LOCKED(TAG, "Stats Buffer Used: %d bytes", usedBytes);
516-
LOGD_LOCKED(TAG, "Stats Buffer Free: %d bytes",
517-
sizeof(statsBuffer) - usedBytes);
518-
LOGD_LOCKED(TAG, "Stats Buffer Size: %d", sizeof(statsBuffer));
519-
// LOGI_LOCKED(TAG, "Task Run Time Stats:\n%s", statsBuffer);
520-
// Serial.flush();
521-
delay(LOG_ITEM_INTERVAL_MS);
511+
ESP_LOGD(TAG, "Task Buffer Used (%): %u",
512+
static_cast<unsigned>((usedBytes * 100U) /
513+
STATS_BUFFER_SIZE));
514+
ESP_LOGD(TAG, "Stats Buffer Used: %u bytes",
515+
static_cast<unsigned>(usedBytes));
516+
ESP_LOGD(TAG, "Stats Buffer Free: %u bytes",
517+
static_cast<unsigned>(STATS_BUFFER_SIZE - usedBytes));
518+
ESP_LOGD(TAG, "Stats Buffer Size: %u",
519+
static_cast<unsigned>(STATS_BUFFER_SIZE));
522520

523-
// for (TaskInfo *taskDesc : load.mainTaskDescriptions) {
524-
// if (taskDesc == nullptr) {
525-
// LOGE_LOCKED(TAG, "Caught null task description
526-
// pointer!"); continue;
527-
// }
528-
529-
// if (taskDesc->pxHandle == nullptr) {
530-
// LOGE_LOCKED(TAG, "Caught null task handle!");
531-
// continue;
532-
// }
533-
534-
// taskDesc->minFreeStack_Bytes =
535-
// uxTaskGetStackHighWaterMark(taskDesc->pxHandle);
536-
// LOGI_LOCKED(TAG, "T: %s, U: %u, F: %u", taskDesc->name,
537-
// taskDesc->stackSize_bytes -
538-
// taskDesc->minFreeStack_Bytes,
539-
// taskDesc->minFreeStack_Bytes);
540-
// // Serial.flush();
541-
// delay(LOG_ITEM_INTERVAL_MS);
542-
// }
543-
// Serial.flush();
544-
delay(LOG_ITEM_INTERVAL_MS);
545-
} else {
546-
// Print buffer
547521
char *saveptr = nullptr;
548-
char *line = strtok_r(statsBuffer, "\n", &saveptr);
549-
delay(LOG_ITEM_INTERVAL_MS);
550-
LOGI_LOCKED(TAG, "Task Info: %s:", line);
551-
while (line != nullptr) {
552-
line = strtok_r(nullptr, "\n", &saveptr);
553-
LOGI_LOCKED(TAG, "Task Info: %s:", line);
522+
for (char *line = strtok_r(statsBuffer, "\n", &saveptr);
523+
line != nullptr; line = strtok_r(nullptr, "\n", &saveptr)) {
524+
size_t crPos = strcspn(line, "\r");
525+
line[crPos] = '\0';
526+
if (line[0] != '\0') {
527+
ESP_LOGI(TAG, "%s", line);
528+
}
554529
}
555-
delay(LOG_ITEM_INTERVAL_MS);
556530

557-
statsBuffer[0] = '\0';
531+
Logging::giveLogMutex();
532+
} else {
533+
ESP_LOGW(TAG, "Skipping runtime stats log; mutex unavailable");
534+
}
535+
536+
delay(LOG_ITEM_INTERVAL_MS);
558537

559538
for (TaskInfo *taskDesc : load.mainTaskDescriptions) {
560539
if (taskDesc == nullptr) {
@@ -573,7 +552,6 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
573552
taskDesc->stackSize_bytes -
574553
taskDesc->minFreeStack_Bytes,
575554
taskDesc->minFreeStack_Bytes);
576-
// Serial.flush();
577555
delay(LOG_ITEM_INTERVAL_MS);
578556
}
579557
delay(LOG_ITEM_INTERVAL_MS);

0 commit comments

Comments
 (0)