Jelajahi Sumber

perf(log): return early from WriteToFile for filtered messages

WriteToFile() called localtime(), strftime(), std::replace() and built
several std::string copies before checking whether the message passes
the log file level. Debug messages are logged at many places in the flow
(PSRAM allocations, CNN steps, ...), and with the default log level all
of that work was thrown away.

Return at the top when the message would be dropped by both the log
file (level > loglevel) and the console (level > esp_log_level_get(tag)).
Measured on an ESP32-CAM with LogLevel = 3 (INFO): 230 filtered debug
messages per round cost 81 ms before and 9 ms after. With the default
LogLevel = 1 more messages are filtered, so the saving is larger.

Console and file output are unchanged: ESP_LOG_LEVEL() applies the same
esp_log_level_get(tag) filter internally.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Victor Gladkikh 1 bulan lalu
induk
melakukan
5c39a25741
1 mengubah file dengan 6 tambahan dan 0 penghapusan
  1. 6 0
      code/components/jomjol_logfile/ClassLogFile.cpp

+ 6 - 0
code/components/jomjol_logfile/ClassLogFile.cpp

@@ -141,6 +141,12 @@ std::string fileNameDate;
 
 void ClassLogFile::WriteToFile(esp_log_level_t level, std::string tag, std::string message, bool _time)
 {
+    // Return before doing any formatting work (localtime, strftime, string copies) if the message
+    // would be dropped by both the log file and the console (ESP log level of the tag)
+    if ((level > loglevel) && (level > esp_log_level_get(tag.c_str()))) {
+        return;
+    }
+
     time_t rawtime;
     struct tm* timeinfo;
     std::string fileNameDateNew;