Replace string-concatenation logging with printf-style F-variants

Converted log calls across src/, examples/, and test/ to use DEBUGF/TRACEF/ERRORF/etc. macros instead of std::string + operator+. This eliminates temporary heap allocations on every log call, which matters on memory-constrained MCUs.
This commit is contained in:
Chad Attermann committed 2026-03-01 10:59:33 -07:00
1 parent 3babdc001d
commit 79b75bbd4e
35 files changed
+595 -604

No files matched your search

@@ -66,7 +66,7 @@ delay(1500);
LoRa.setTxPower(power);
INFO("LoRa init succeeded.");
TRACE("LoRa bandwidth is " + std::to_string(Utilities::OS::round(_bitrate/1000.0, 2)) + " Kbps");
TRACEF("LoRa bandwidth is %s Kbps", std::to_string(Utilities::OS::round(_bitrate/1000.0, 2)).c_str());
#endif
_online = true;
@@ -111,10 +111,10 @@ void LoRaInterface::loop() {
}
/*virtual*/ void LoRaInterface::send_outgoing(const Bytes& data) {
DEBUG(toString() + ".on_outgoing: data: " + data.toHex());
DEBUGF("%s.on_outgoing: data: %s", toString().c_str(), data.toHex().c_str());
try {
if (_online) {
TRACE("LoRaInterface: sending " + std::to_string(data.size()) + " bytes...");
TRACEF("LoRaInterface: sending %zu bytes...", data.size());
// Send packet
#ifdef ARDUINO
@@ -142,12 +142,12 @@ void LoRaInterface::loop() {
InterfaceImpl::handle_outgoing(data);
}
catch (std::exception& e) {
ERROR("Could not transmit on " + toString() + ". The contained exception was: " + e.what());
ERRORF("Could not transmit on %s. The contained exception was: %s", toString().c_str(), e.what());
}
}
/*virtual*/ void LoRaInterface::on_incoming(const Bytes& data) {
DEBUG(toString() + ".on_incoming: data: " + data.toHex());
DEBUGF("%s.on_incoming: data: %s", toString().c_str(), data.toHex().c_str());
// Pass received data on to transport
InterfaceImpl::handle_incoming(data);
}
+16 -16
View File
@@ -64,14 +64,14 @@ UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : RNS::Interfa
}
_local_port = port;
_remote_port = port;
TRACE("UDPInterface: local host: " + _local_host);
TRACE("UDPInterface: local port: " + std::to_string(_local_port));
TRACE("UDPInterface: remote host: " + _remote_host);
TRACE("UDPInterface: remote port: " + std::to_string(_remote_port));
TRACEF("UDPInterface: local host: %s", _local_host.c_str());
TRACEF("UDPInterface: local port: %d", _local_port);
TRACEF("UDPInterface: remote host: %s", _remote_host.c_str());
TRACEF("UDPInterface: remote port: %d", _remote_port);
#ifdef ARDUINO
TRACE("UDPInterface: wifi ssid: " + _wifi_ssid);
TRACE("UDPInterface: wifi password: " + _wifi_password);
TRACEF("UDPInterface: wifi ssid: %s", _wifi_ssid.c_str());
TRACEF("UDPInterface: wifi password: %s", _wifi_password.c_str());
// Connect to the WiFi network
WiFi.begin(_wifi_ssid.c_str(), _wifi_password.c_str());
@@ -133,7 +133,7 @@ UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : RNS::Interfa
if (inet_aton(_local_host.c_str(), &local_addr) == 0) {
struct hostent* host_ent = gethostbyname(_local_host.c_str());
if (host_ent == nullptr || host_ent->h_addr_list[0] == nullptr) {
ERROR("Unable to resolve local host " + std::string(_local_host));
ERRORF("Unable to resolve local host %s", _local_host.c_str());
return false;
}
_local_address = *((in_addr_t*)(host_ent->h_addr_list[0]));
@@ -147,7 +147,7 @@ UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : RNS::Interfa
if (inet_aton(_remote_host.c_str(), &remote_addr) == 0) {
struct hostent* host_ent = gethostbyname(_remote_host.c_str());
if (host_ent == nullptr || host_ent->h_addr_list[0] == nullptr) {
ERROR("Unable to resolve remote host " + std::string(_remote_host));
ERRORF("Unable to resolve remote host %s", _remote_host.c_str());
return false;
}
_remote_address = *((in_addr_t*)(host_ent->h_addr_list[0]));
@@ -160,7 +160,7 @@ UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : RNS::Interfa
TRACE("Opening UDP socket");
_socket = socket( PF_INET, SOCK_DGRAM, 0 );
if (_socket < 0) {
ERROR("Unable to create socket with error " + std::to_string(errno));
ERRORF("Unable to create socket with error %d", errno);
return false;
}
@@ -176,7 +176,7 @@ UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : RNS::Interfa
#endif
// bind to interface for listening
INFO("Binding UDP socket " + std::to_string(_socket) + " to " + std::string(_local_host) + ":" + std::to_string(_local_port));
INFOF("Binding UDP socket %d to %s:%d", _socket, _local_host.c_str(), _local_port);
sockaddr_in bind_addr;
bind_addr.sin_family = AF_INET;
bind_addr.sin_addr.s_addr = _local_address;
@@ -184,7 +184,7 @@ UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : RNS::Interfa
if (bind(_socket, (struct sockaddr*)&bind_addr, sizeof(bind_addr)) == -1) {
close(_socket);
_socket = -1;
ERROR("Unable to bind socket with error " + std::to_string(errno));
ERRORF("Unable to bind socket with error %d", errno);
return false;
}
#endif
@@ -234,7 +234,7 @@ UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : RNS::Interfa
}
/*virtual*/ void UDPInterface::send_outgoing(const Bytes& data) {
DEBUG(toString() + ".on_outgoing: data: " + data.toHex());
DEBUGF("%s.on_outgoing: data: %s", toString().c_str(), data.toHex().c_str());
try {
if (_online) {
// Send packet
@@ -243,13 +243,13 @@ UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : RNS::Interfa
udp.write(data.data(), data.size());
udp.endPacket();
#else
TRACE("Sending UDP packet to " + std::string(_remote_host) + ":" + std::to_string(_remote_port));
TRACEF("Sending UDP packet to %s:%d", _remote_host.c_str(), _remote_port);
sockaddr_in sock_addr;
sock_addr.sin_family = AF_INET;
sock_addr.sin_addr.s_addr = _remote_address;
sock_addr.sin_port = htons(_remote_port);
int sent = sendto(_socket, data.data(), data.size(), 0, (struct sockaddr*)&sock_addr, sizeof(sock_addr));
TRACE("Sent " + std::to_string(sent) + " bytes to " + std::string(_remote_host) + ":" + std::to_string(_remote_port));
TRACEF("Sent %d bytes to %s:%d", sent, _remote_host.c_str(), _remote_port);
#endif
}
@@ -257,12 +257,12 @@ UDPInterface::UDPInterface(const char* name /*= "UDPInterface"*/) : RNS::Interfa
InterfaceImpl::handle_outgoing(data);
}
catch (std::exception& e) {
ERROR("Could not transmit on " + toString() + ". The contained exception was: " + e.what());
ERRORF("Could not transmit on %s. The contained exception was: %s", toString().c_str(), e.what());
}
}
void UDPInterface::on_incoming(const Bytes& data) {
DEBUG(toString() + ".on_incoming: data: " + data.toHex());
DEBUGF("%s.on_incoming: data: %s", toString().c_str(), data.toHex().c_str());
// Pass received data on to transport
InterfaceImpl::handle_incoming(data);
}
@@ -121,7 +121,7 @@ void UniversalFileSystem::listDir(const char* dir) {
return true;
}
else {
ERROR("file_exists: failed to open file " + std::string(file_path));
ERRORF("file_exists: failed to open file %s", file_path);
return false;
}
}
@@ -155,9 +155,9 @@ void UniversalFileSystem::listDir(const char* dir) {
//size_t read = fread(data.writable(size), size, 1, file);
read = fread(data.writable(size), 1, size, file);
#endif
TRACE("read_file: read " + std::to_string(read) + " bytes from file " + std::string(file_path));
TRACEF("read_file: read %zu bytes from file %s", read, file_path);
if (read != size) {
ERROR("read_file: failed to read file " + std::string(file_path));
ERRORF("read_file: failed to read file %s", file_path);
data.clear();
}
//TRACE("read_file: closing input file");
@@ -173,7 +173,7 @@ void UniversalFileSystem::listDir(const char* dir) {
#endif
}
else {
ERROR("read_file: failed to open input file " + std::string(file_path));
ERRORF("read_file: failed to open input file %s", file_path);
}
return read;
}
@@ -201,9 +201,9 @@ void UniversalFileSystem::listDir(const char* dir) {
//size_t wrote = fwrite(data.data(), data.size(), 1, file);
wrote = fwrite(data.data(), 1, data.size(), file);
#endif
TRACE("write_file: wrote " + std::to_string(wrote) + " bytes to file " + std::string(file_path));
TRACEF("write_file: wrote %zu bytes to file %s", wrote, file_path);
if (wrote < data.size()) {
WARNING("write_file: not all data was written to file " + std::string(file_path));
WARNINGF("write_file: not all data was written to file %s", file_path);
}
//TRACE("write_file: closing output file");
#ifdef ARDUINO
@@ -218,7 +218,7 @@ void UniversalFileSystem::listDir(const char* dir) {
#endif
}
else {
ERROR("write_file: failed to open output file " + std::string(file_path));
ERRORF("write_file: failed to open output file %s", file_path);
}
return wrote;
}
@@ -348,7 +348,7 @@ void UniversalFileSystem::listDir(const char* dir) {
}
/*virtua*/ bool UniversalFileSystem::directory_exists(const char* directory_path) {
TRACE("directory_exists: checking for existence of directory " + std::string(directory_path));
TRACEF("directory_exists: checking for existence of directory %s", directory_path);
#ifdef ARDUINO
#ifdef BOARD_ESP32
File file = SPIFFS.open(directory_path, FILE_READ);
@@ -382,13 +382,13 @@ void UniversalFileSystem::listDir(const char* dir) {
#ifdef ARDUINO
#ifdef BOARD_ESP32
if (!SPIFFS.mkdir(directory_path)) {
ERROR("create_directory: failed to create directorty " + std::string(directory_path));
ERRORF("create_directory: failed to create directorty %s", directory_path);
return false;
}
return true;
#elif BOARD_NRF52
if (!InternalFS.mkdir(directory_path)) {
ERROR("create_directory: failed to create directorty " + std::string(directory_path));
ERRORF("create_directory: failed to create directorty %s", directory_path);
return false;
}
return true;
@@ -406,18 +406,18 @@ void UniversalFileSystem::listDir(const char* dir) {
}
/*virtua*/ bool UniversalFileSystem::remove_directory(const char* directory_path) {
TRACE("remove_directory: removing directory " + std::string(directory_path));
TRACEF("remove_directory: removing directory %s", directory_path);
#ifdef ARDUINO
#ifdef BOARD_ESP32
//if (!LittleFS.rmdir_r(directory_path)) {
if (!SPIFFS.rmdir(directory_path)) {
ERROR("remove_directory: failed to remove directorty " + std::string(directory_path));
ERRORF("remove_directory: failed to remove directorty %s", directory_path);
return false;
}
return true;
#elif BOARD_NRF52
if (!InternalFS.rmdir_r(directory_path)) {
ERROR("remove_directory: failed to remove directory " + std::string(directory_path));
ERRORF("remove_directory: failed to remove directory %s", directory_path);
return false;
}
return true;
@@ -431,7 +431,7 @@ void UniversalFileSystem::listDir(const char* dir) {
}
/*virtua*/ std::list<std::string> UniversalFileSystem::list_directory(const char* directory_path) {
TRACE("list_directory: listing directory " + std::string(directory_path));
TRACEF("list_directory: listing directory %s", directory_path);
std::list<std::string> files;
#ifdef ARDUINO
#ifdef BOARD_ESP32
@@ -440,7 +440,7 @@ void UniversalFileSystem::listDir(const char* dir) {
File root = InternalFS.open(directory_path);
#endif
if (!root) {
ERROR("list_directory: failed to open directory " + std::string(directory_path));
ERRORF("list_directory: failed to open directory %s", directory_path);
return files;
}
File file = root.openNextFile();