Skip to content

Commit 46ab693

Browse files
committed
Revert "Asked Copilot for help"
This reverts commit 36cfe155526100451b0b25dd27c85ed22c606ce5.
1 parent edea669 commit 46ab693

1 file changed

Lines changed: 51 additions & 29 deletions

File tree

src/main.cpp

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

10-
#include <cstring>
11-
1210
// Library Includes
1311
#include <Arduino.h>
1412

@@ -493,48 +491,71 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
493491
// by 3?
494492
constexpr uint_fast16_t STATS_BUFFER_SIZE =
495493
REC_BYTES_PER_TASK * (NUM_MAIN_TASKS + NUM_ESP_TASKS);
496-
char statsBuffer[STATS_BUFFER_SIZE] = {'\0'};
494+
static char statsBuffer[STATS_BUFFER_SIZE] = {'\0'};
497495
if (uxTaskGetNumberOfTasks() > NUM_MAIN_TASKS + NUM_ESP_TASKS) {
498496
LOGE_LOCKED(
499497
TAG,
500498
"Number of tasks (%d) exceeds expected max (%d), skipping to "
501499
"prevent memory corruption",
502500
uxTaskGetNumberOfTasks(), NUM_MAIN_TASKS + NUM_ESP_TASKS);
501+
// Serial.flush();
503502
delay(LOG_ITEM_INTERVAL_MS);
504-
} else {
505-
if (Logging::takeLogMutex()) {
503+
// } else {
504+
} else if (statsBuffer[0] == '\0') {
506505
// Refresh stats buffer
507-
vTaskGetRunTimeStats(statsBuffer);
508-
statsBuffer[STATS_BUFFER_SIZE - 1] = '\0';
509506

507+
// TODO: Not recommended in production
508+
vTaskGetRunTimeStats(statsBuffer);
509+
statsBuffer[STATS_BUFFER_SIZE - 1] =
510+
'\0'; // hard cap, avoid over-read
511+
// uxTaskGetSystemState();
510512
size_t usedBytes = strnlen(statsBuffer, STATS_BUFFER_SIZE);
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));
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);
520522

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
521547
char *saveptr = nullptr;
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-
}
529-
}
530-
531-
Logging::giveLogMutex();
532-
} else {
533-
ESP_LOGW(TAG, "Skipping runtime stats log; mutex unavailable");
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);
534554
}
535-
536555
delay(LOG_ITEM_INTERVAL_MS);
537556

557+
statsBuffer[0] = '\0';
558+
538559
for (TaskInfo *taskDesc : load.mainTaskDescriptions) {
539560
if (taskDesc == nullptr) {
540561
LOGE_LOCKED(TAG, "Caught null task description pointer!");
@@ -552,6 +573,7 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
552573
taskDesc->stackSize_bytes -
553574
taskDesc->minFreeStack_Bytes,
554575
taskDesc->minFreeStack_Bytes);
576+
// Serial.flush();
555577
delay(LOG_ITEM_INTERVAL_MS);
556578
}
557579
delay(LOG_ITEM_INTERVAL_MS);

0 commit comments

Comments
 (0)