Skip to content

Commit

Permalink
Replace all printf and println
Browse files Browse the repository at this point in the history
  • Loading branch information
hhvrc committed Feb 4, 2025
1 parent b78f410 commit bfff974
Show file tree
Hide file tree
Showing 9 changed files with 21 additions and 104 deletions.
14 changes: 6 additions & 8 deletions include/Logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@
#include <esp_ota_ops.h>
#include <esp_system.h>

extern "C" int log_printf(const char* fmt, ...);

template<std::size_t N>
constexpr const char* openshockPathToFileName(const char (&path)[N]) {
std::size_t pos = 0;
Expand All @@ -32,37 +30,37 @@ constexpr const char* openshockPathToFileName(const char (&path)[N]) {
#define OPENSHOCK_LOG_FORMAT(letter, format) "[%lli][" #letter "][%s:%u] %s(): " format "\r\n", OpenShock::millis(), openshockPathToFileName(__FILE__), __LINE__, __FUNCTION__

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_VERBOSE
#define OS_LOGV(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(V, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGV(TAG, format, ...) esp_log_write(ESP_LOG_VERBOSE, TAG, OPENSHOCK_LOG_FORMAT(V, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGV(TAG, format, ...) do {} while(0)
#endif

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_DEBUG
#define OS_LOGD(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(D, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGD(TAG, format, ...) esp_log_write(ESP_LOG_DEBUG, TAG, OPENSHOCK_LOG_FORMAT(D, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGD(TAG, format, ...) do {} while(0)
#endif

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_INFO
#define OS_LOGI(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(I, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGI(TAG, format, ...) esp_log_write(ESP_LOG_INFO, TAG, OPENSHOCK_LOG_FORMAT(I, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGI(TAG, format, ...) do {} while(0)
#endif

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_WARN
#define OS_LOGW(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(W, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGW(TAG, format, ...) esp_log_write(ESP_LOG_WARN, TAG, OPENSHOCK_LOG_FORMAT(W, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGW(TAG, format, ...) do {} while(0)
#endif

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_ERROR
#define OS_LOGE(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(E, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGE(TAG, format, ...) esp_log_write(ESP_LOG_ERROR, TAG, OPENSHOCK_LOG_FORMAT(E, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGE(TAG, format, ...) do {} while(0)
#endif

#if OPENSHOCK_LOG_LEVEL >= OPENSHOCK_LOG_LEVEL_NONE
#define OS_LOGN(TAG, format, ...) log_printf(OPENSHOCK_LOG_FORMAT(E, "[%s] " format), TAG, ##__VA_ARGS__)
#define OS_LOGN(TAG, format, ...) esp_log_write(ESP_LOG_NONE, TAG, OPENSHOCK_LOG_FORMAT(E, "[%s] " format), TAG, ##__VA_ARGS__)
#else
#define OS_LOGN(TAG, format, ...) do {} while(0)
#endif
Expand Down
6 changes: 0 additions & 6 deletions include/serial/Serial.h

This file was deleted.

4 changes: 2 additions & 2 deletions include/serial/command_handlers/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "Logging.h"

#include <Arduino.h>
#include <cstdio>

#define SERPR_SYS(format, ...) ::Serial.printf("$SYS$|" format "\n", ##__VA_ARGS__)
#define SERPR_SYS(format, ...) printf("$SYS$|" format "\n", ##__VA_ARGS__)
#define SERPR_RESPONSE(format, ...) SERPR_SYS("Response|" format, ##__VA_ARGS__)
#define SERPR_SUCCESS(format, ...) SERPR_SYS("Success|" format, ##__VA_ARGS__)
#define SERPR_ERROR(format, ...) SERPR_SYS("Error|" format, ##__VA_ARGS__)
Expand Down
5 changes: 0 additions & 5 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const char* const TAG = "main";
#include "GatewayConnectionManager.h"
#include "Logging.h"
#include "OtaUpdateManager.h"
#include "serial/Serial.h"
#include "serial/SerialInputHandler.h"
#include "util/TaskUtils.h"
#include "VisualStateManager.h"
Expand Down Expand Up @@ -93,10 +92,6 @@ void setup()
{
// esp_log_level_set(ESP_LOG_VERBOSE);

OpenShock::Serial::Init();

// esp_log_level_set(ESP_LOG_VERBOSE);

OpenShock::Config::Init();

if (!OpenShock::Events::Init()) {
Expand Down
75 changes: 0 additions & 75 deletions src/serial/Serial.cpp

This file was deleted.

9 changes: 5 additions & 4 deletions src/serial/SerialInputHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ const char* const TAG = "SerialInputHandler";
#include <cJSON.h>
#include <Esp.h>

#include <cstdio>
#include <cstring>
#include <string_view>
#include <unordered_map>
Expand Down Expand Up @@ -420,7 +421,7 @@ void _skipSerialWhitespaces(SerialBuffer& buffer)

void _echoBuffer(std::string_view buffer)
{
::Serial.printf(CLEAR_LINE "> %.*s", buffer.size(), buffer.data());
printf(CLEAR_LINE "> %.*s", buffer.size(), buffer.data());
}

void _echoHandleSerialInput(std::string_view buffer, bool hasData)
Expand Down Expand Up @@ -467,7 +468,7 @@ void _processSerialLine(std::string_view line)
line = line.substr(1);
} else if (s_echoEnabled) {
_echoBuffer(line);
::Serial.println();
putchar('\n');
}

auto parts = OpenShock::StringSplit(line, ' ', 1);
Expand Down Expand Up @@ -558,7 +559,7 @@ void _serialRxTask(void*)
_skipSerialWhitespaces(buffer);
break;
case SerialReadResult::AutoCompleteRequest:
::Serial.printf(CLEAR_LINE "> %.*s [AutoComplete is not implemented]", buffer.size(), buffer.data());
printf(CLEAR_LINE "> %.*s [AutoComplete is not implemented]", buffer.size(), buffer.data());
break;
case SerialReadResult::Data:
_echoHandleSerialInput(buffer, true);
Expand Down Expand Up @@ -590,7 +591,7 @@ bool SerialInputHandler::Init()

SerialInputHandler::PrintWelcomeHeader();
SerialInputHandler::PrintVersionInfo();
::Serial.println();
putchar('\n');

if (!Config::GetSerialInputConfigEchoEnabled(s_echoEnabled)) {
OS_LOGE(TAG, "Failed to get serial echo status from config");
Expand Down
6 changes: 4 additions & 2 deletions src/serial/command_handlers/factoryreset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

#include <esp_system.h>

#include <cstdio>

void _handleFactoryResetCommand(std::string_view arg, bool isAutomated)
{
(void)arg;

::Serial.println("Resetting to factory defaults...");
printf("Resetting to factory defaults...\n");
OpenShock::Config::FactoryReset();
::Serial.println("Restarting...");
printf("Restarting...\n");
esp_restart();
}

Expand Down
2 changes: 1 addition & 1 deletion src/serial/command_handlers/restart.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
void _handleRestartCommand(std::string_view arg, bool isAutomated) {
(void)arg;

::Serial.println("Restarting ESP...");
printf("Restarting ESP...\n");
esp_restart();
}

Expand Down
4 changes: 3 additions & 1 deletion src/serial/command_handlers/version.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@

#include "serial/SerialInputHandler.h"

#include <cstdio>
#include <vector>

void _handleVersionCommand(std::string_view arg, bool isAutomated) {
(void)arg;

::Serial.print("\n");
putchar('\n');

OpenShock::SerialInputHandler::PrintVersionInfo();
}

Expand Down

1 comment on commit bfff974

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cpp-Linter Report ⚠️

Some files did not pass the configured checks!

clang-format (v18.1.3) reports: 3 file(s) not formatted
  • include/Logging.h
  • src/serial/command_handlers/restart.cpp
  • src/serial/command_handlers/version.cpp

Have any feedback or feature suggestions? Share it here.

Please sign in to comment.