Skip to content

Commit fb6586f

Browse files
committed
Logging Test
1 parent 17c936f commit fb6586f

1 file changed

Lines changed: 81 additions & 60 deletions

File tree

src/main.cpp

Lines changed: 81 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "2026Core/CommonConfig.hpp" // Include after NacelleConfig due to macro precednece
1919
#include "2026Core/Net/Net-Application/NTP.hpp"
2020
// #include "2026Core/Net/Net-Application/OTA.hpp"
21+
#include "2026Core/Logging.hpp"
2122
#include "2026Core/Net/Net-Link/AdapterESPNow.hpp"
2223
#include "2026Core/Net/Net-Phy/AdapterWLAN.hpp"
2324
#include "LoadContainer.hpp"
@@ -54,15 +55,15 @@ LoadFSM loadFSM(load);
5455
// todo: move
5556
bool configureLoad() {
5657
if (!loadDevice.begin()) {
57-
ESP_LOGE(TAG, "Failed to initialize MCP23008T device at 0x%02X",
58-
loadDevice.getAddress());
58+
LOGE_LOCKED(TAG, "Failed to initialize MCP23008T device at 0x%02X",
59+
loadDevice.getAddress());
5960
return false;
6061
}
6162

6263
// First six pins outputs, made last two inputs as that's the default
6364
if (!loadDevice.setIODir(0b1100'0000)) {
64-
ESP_LOGE(TAG, "Failed to set IODIR on MCP23008T at 0x%02X",
65-
loadDevice.getAddress());
65+
LOGE_LOCKED(TAG, "Failed to set IODIR on MCP23008T at 0x%02X",
66+
loadDevice.getAddress());
6667
return false;
6768
}
6869

@@ -74,8 +75,8 @@ bool configureLoad() {
7475

7576
// Set all outputs low, note that pin 0 is inverted
7677
if (!loadDevice.setGPIO(0b0000'0000)) {
77-
ESP_LOGE(TAG, "Failed to set GPIO on MCP23008T at 0x%02X",
78-
loadDevice.getAddress());
78+
LOGE_LOCKED(TAG, "Failed to set GPIO on MCP23008T at 0x%02X",
79+
loadDevice.getAddress());
7980
return false;
8081
}
8182

@@ -105,10 +106,11 @@ inline bool showLEDsIfReady() {
105106
void setup() {
106107
static bool serialInitialized = false;
107108
if (!serialInitialized) {
108-
Serial.begin(115200);
109109
size_t txBuffer = Serial.setTxBufferSize(1024);
110+
Serial.begin(460800);
110111
ESP_LOGI(TAG, "Serial initialized @ %d baud w/ buffer size %d",
111112
Serial.baudRate(), txBuffer);
113+
Logging::initLoggingMutex();
112114
serialInitialized = true;
113115
}
114116

@@ -238,7 +240,7 @@ void setup() {
238240
if (result != pdPASS) {
239241
ESP_LOGE(TAG, "Failed to create task %s", taskDesc->name);
240242
} else {
241-
ESP_LOGD(
243+
ESP_LOGE(
242244
TAG,
243245
"Created task %s with priority %u and stack size %u bytes",
244246
taskDesc->name, taskDesc->priority,
@@ -282,10 +284,10 @@ vTaskUpdateFSM([[maybe_unused]] void *pvParameters) { // NOSONAR
282284
while (true) {
283285
// LoadFSM::UPDATE_RESULT result = loadFSM.updateState();
284286
// if (result == LoadFSM::UPDATE_RESULT::STATE_CHANGED) {
285-
// ESP_LOGI(TAG, "FSM State Changed: %d",
287+
// LOGI_LOCKED(TAG, "FSM State Changed: %d",
286288
// loadFSM.getCurrentState());
287289
// } else if (result == LoadFSM::UPDATE_RESULT::ERROR) {
288-
// ESP_LOGE(TAG, "Error updating FSM state");
290+
// LOGE_LOCKED(TAG, "Error updating FSM state");
289291
// }
290292
delay(RUN::TASK_INTERVALS::TI_FSM_mS);
291293
}
@@ -308,7 +310,7 @@ vTaskPollSensors([[maybe_unused]] void *pvParameters) { // NOSONAR
308310
vTaskAdjustLoad([[maybe_unused]] void *pvParameters) { // NOSONAR
309311
while (true) {
310312
static int i = 0;
311-
// ESP_LOGI(TAG, "Pitch PID Output: %f",
313+
// LOGI_LOCKED(TAG, "Pitch PID Output: %f",
312314
// pitchPIDController.compute(
313315
// i)); // todo - just a quick performances test
314316
i += 20;
@@ -382,10 +384,10 @@ vTaskConfigure([[maybe_unused]] void *pvParameters) { // NOSONAR
382384
while (true) {
383385
// if (!loadConfigured) {
384386
// if (loadDevice.begin()) { // FIXME!
385-
// ESP_LOGI(TAG, "MCP23008T initialized successfully.");
387+
// LOGI_LOCKED(TAG, "MCP23008T initialized successfully.");
386388
// loadConfigured = true;
387389
// } else {
388-
// ESP_LOGE(
390+
// LOGE_LOCKED(
389391
// TAG,
390392
// "Failed to initialize MCP23008T. Retrying in 5
391393
// seconds...");
@@ -423,7 +425,7 @@ vTaskConfigure([[maybe_unused]] void *pvParameters) { // NOSONAR
423425
[[noreturn]] void
424426
vTaskStatusLED([[maybe_unused]] void *pvParameters) { // NOSONAR
425427
while (true) {
426-
ESP_LOGV(TAG, "vTSL");
428+
LOGV_LOCKED(TAG, "vTSL");
427429
leds.setPixelColor(0, 0x00, 0xFF, 0x00); // Green
428430
if (leds.canShow()) {
429431
leds.show();
@@ -479,26 +481,27 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
479481
// continue;
480482
// }
481483

482-
ESP_LOGD(TAG, "Logging Data:");
484+
LOGD_LOCKED(TAG, "Logging Data:");
483485

484-
ESP_LOGI(TAG, "FreeRTOS Tasks: %u", uxTaskGetNumberOfTasks());
486+
LOGI_LOCKED(TAG, "FreeRTOS Tasks: %u", uxTaskGetNumberOfTasks());
485487

486488
constexpr uint_fast8_t REC_BYTES_PER_TASK = 40;
487489
constexpr uint_fast8_t NUM_ESP_TASKS =
488490
11; // TODO: Why was this set to 8 and why did I need to increase it
489491
// by 3?
490492
constexpr uint_fast16_t STATS_BUFFER_SIZE =
491493
REC_BYTES_PER_TASK * (NUM_MAIN_TASKS + NUM_ESP_TASKS);
492-
char statsBuffer[STATS_BUFFER_SIZE] = {'\0'};
494+
static char statsBuffer[STATS_BUFFER_SIZE] = {'\0'};
493495
if (uxTaskGetNumberOfTasks() > NUM_MAIN_TASKS + NUM_ESP_TASKS) {
494-
ESP_LOGE(
496+
LOGE_LOCKED(
495497
TAG,
496498
"Number of tasks (%d) exceeds expected max (%d), skipping to "
497499
"prevent memory corruption",
498500
uxTaskGetNumberOfTasks(), NUM_MAIN_TASKS + NUM_ESP_TASKS);
501+
// Serial.flush();
499502
delay(LOG_ITEM_INTERVAL_MS);
500-
} else {
501-
// } else if (statsBuffer[0] == '\0') {
503+
// } else {
504+
} else if (statsBuffer[0] == '\0') {
502505
// Refresh stats buffer
503506

504507
// TODO: Not recommended in production
@@ -507,61 +510,78 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
507510
'\0'; // hard cap, avoid over-read
508511
// uxTaskGetSystemState();
509512
size_t usedBytes = strnlen(statsBuffer, STATS_BUFFER_SIZE);
510-
ESP_LOGD(TAG, "Task Buffer Used (%): %d",
511-
usedBytes * 100 / sizeof(statsBuffer));
512-
ESP_LOGD(TAG, "Stats Buffer Used: %d bytes", usedBytes);
513-
ESP_LOGD(TAG, "Stats Buffer Free: %d bytes",
514-
sizeof(statsBuffer) - usedBytes);
515-
ESP_LOGD(TAG, "Stats Buffer Size: %d", sizeof(statsBuffer));
516-
ESP_LOGI(TAG, "Task Run Time Stats:\n%s", statsBuffer);
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);
517520
// Serial.flush();
518521
delay(LOG_ITEM_INTERVAL_MS);
519522

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
547+
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);
554+
}
555+
delay(LOG_ITEM_INTERVAL_MS);
556+
557+
statsBuffer[0] = '\0';
558+
520559
for (TaskInfo *taskDesc : load.mainTaskDescriptions) {
521560
if (taskDesc == nullptr) {
522-
ESP_LOGE(TAG, "Caught null task description pointer!");
561+
LOGE_LOCKED(TAG, "Caught null task description pointer!");
523562
continue;
524563
}
525564

526565
if (taskDesc->pxHandle == nullptr) {
527-
ESP_LOGE(TAG, "Caught null task handle!");
566+
LOGE_LOCKED(TAG, "Caught null task handle!");
528567
continue;
529568
}
530569

531570
taskDesc->minFreeStack_Bytes =
532571
uxTaskGetStackHighWaterMark(taskDesc->pxHandle);
533-
ESP_LOGI(TAG, "T: %s, U: %u, F: %u", taskDesc->name,
534-
taskDesc->stackSize_bytes -
535-
taskDesc->minFreeStack_Bytes,
536-
taskDesc->minFreeStack_Bytes);
572+
LOGI_LOCKED(TAG, "T: %s, U: %u, F: %u", taskDesc->name,
573+
taskDesc->stackSize_bytes -
574+
taskDesc->minFreeStack_Bytes,
575+
taskDesc->minFreeStack_Bytes);
576+
// Serial.flush();
577+
delay(LOG_ITEM_INTERVAL_MS);
537578
}
538-
// Serial.flush();
539579
delay(LOG_ITEM_INTERVAL_MS);
540580
}
541-
// else {
542-
// // Print buffer
543-
// char *saveptr = nullptr;
544-
// char *line = strtok_r(statsBuffer, "\n", &saveptr);
545-
// delay(LOG_ITEM_INTERVAL_MS);
546-
// ESP_LOGI(TAG, "Task Info: %s:", line);
547-
// while (line != nullptr) {
548-
// line = strtok_r(nullptr, "\n", &saveptr);
549-
// ESP_LOGI(TAG, "Task Info: %s:", line);
550-
// }
551-
// delay(LOG_ITEM_INTERVAL_MS);
552-
553-
// for (TaskInfo *taskDesc : load.mainTaskDescriptions) {
554-
// taskDesc->minFreeStack_Bytes =
555-
// uxTaskGetStackHighWaterMark(taskDesc->pxHandle);
556-
// ESP_LOGI(TAG, "T: %s, U: %u, F: %u", taskDesc->name,
557-
// taskDesc->stackSize_bytes -
558-
// taskDesc->minFreeStack_Bytes,
559-
// taskDesc->minFreeStack_Bytes);
560-
// }
561-
// delay(LOG_ITEM_INTERVAL_MS);
562-
// }
563581

564-
ESP_LOGI(TAG, "Min, free heap: %u b", esp_get_minimum_free_heap_size());
582+
LOGI_LOCKED(TAG, "Min, free heap: %u b",
583+
esp_get_minimum_free_heap_size());
584+
// Serial.flush();
565585
delay(LOG_ITEM_INTERVAL_MS);
566586

567587
// Enable temperature sensor
@@ -574,12 +594,13 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
574594
constexpr int32_t MAX_EXT_TEMP = 105;
575595
constexpr int32_t MIN_EXT_TEMP = -40;
576596
if (tempTrunc_C > MAX_EXT_TEMP || tempTrunc_C < MIN_EXT_TEMP) {
577-
ESP_LOGE(TAG, "Temp. out of bounds: %d dC", tempTrunc_C);
597+
LOGE_LOCKED(TAG, "Temp. out of bounds: %d dC", tempTrunc_C);
578598
} else {
579-
ESP_LOGI(TAG, "Temp.: %d dC", tempTrunc_C);
599+
LOGI_LOCKED(TAG, "Temp.: %d dC", tempTrunc_C);
580600
}
581601
// Disable the temperature sensor if it is not needed and save the power
582602
ESP_ERROR_CHECK(temperature_sensor_disable(tempSensHandle));
603+
// Serial.flush();
583604
delay(LOG_ITEM_INTERVAL_MS);
584605

585606
// esp_wifi_get_bandwidth
@@ -592,6 +613,6 @@ constexpr uint32_t LOG_ITEM_INTERVAL_MS =
592613
* Arduino: put your main code here, to run repeatedly:
593614
*/
594615
void loop() {
595-
// ESP_LOGI(TAG, "Time: %llu", SyncedClock::getSystemTimer());
616+
// LOGI_LOCKED(TAG, "Time: %llu", SyncedClock::getSystemTimer());
596617
delay(1000);
597618
}

0 commit comments

Comments
 (0)