Skip to content

Commit

Permalink
feat(port): fake ESP-IDF logging
Browse files Browse the repository at this point in the history
Working in log levels and semi-semihosting

#58
  • Loading branch information
malachib committed Dec 8, 2024
1 parent 96c3e64 commit 15e7040
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 7 deletions.
38 changes: 38 additions & 0 deletions src/estd/internal/utility.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
#include <stdarg.h>
#include <stdio.h>

#include "platform.h"
#include "utility.h"

#include "../port/esp-idf/fake/esp_log.h"

#ifndef FEATURE_CPP_INLINE_STATIC
namespace estd { namespace internal {

Expand All @@ -9,3 +14,36 @@ CONSTEXPR has_member_base::no has_member_base::no_value = { { 0 }, { 1 } };

}}
#endif

// ESP-IDF build chain doesn't pick this file up anyway, so this is just an extra protection
// in case we change our mind about that
#ifndef ESP_PLATFORM

static vprintf_like_t esp_log_writer = vprintf;

vprintf_like_t esp_log_set_vprintf(vprintf_like_t func)
{
// TODO: Consider swap operation here
vprintf_like_t previous = esp_log_writer;
esp_log_writer = func;
return previous;
}

void esp_log_write(esp_log_level_t level, const char* tag, const char* format, ...)
{
if(level <= LOG_LOCAL_LEVEL)
{
static va_list empty_list{};

// TODO: Write out tag

va_list args;
va_start(args, format);
esp_log_writer(format, args);
va_end(args);

//esp_log_writer("\n", empty_list);
}
}

#endif
11 changes: 7 additions & 4 deletions src/estd/port/esp-idf/fake/esp_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,6 @@ extern "C" {

#include <stdio.h>

//#define LOG_LOCAL_LEVEL

//#define ESP_LOG_LEVEL

enum esp_log_level_t
{
ESP_LOG_NONE,
Expand All @@ -22,6 +18,10 @@ enum esp_log_level_t
ESP_LOG_VERBOSE
};

#define LOG_LOCAL_LEVEL ESP_LOG_INFO

//#define ESP_LOG_LEVEL

#define ESP_LOGE(tag, fmt...) { printf("E %s: ", tag); printf(fmt); puts(""); }
#define ESP_LOGW(tag, fmt...) { printf("W %s: ", tag); printf(fmt); puts(""); }
#define ESP_LOGI(tag, fmt...) { printf("I %s: ", tag); printf(fmt); puts(""); }
Expand All @@ -30,7 +30,10 @@ enum esp_log_level_t

#define ESP_LOG_BUFFER_HEX(tag, buffer, len)

typedef int (*vprintf_like_t)(const char*, va_list);

void esp_log_write(esp_log_level_t level, const char *tag, const char *format, ...);
vprintf_like_t esp_log_set_vprintf(vprintf_like_t func);

#ifdef __cplusplus
}
Expand Down
1 change: 1 addition & 0 deletions test/catch/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ else()
misc-test.cpp
optional-test.cpp
ostream-test.cpp
port-test.cpp
priority-queue-test.cpp
queue-test.cpp
stack-test.cpp
Expand Down
3 changes: 0 additions & 3 deletions test/catch/experimental-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
//#include <estd/locale.h>
#include "estd/exp/unique_handle.h"

// DEBT: Move this out to a 'port.cpp' test
#include <estd/port/esp-idf/fake/esp_log.h>

using namespace estd::test;

struct Test
Expand Down
25 changes: 25 additions & 0 deletions test/catch/port-test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include <catch2/catch.hpp>

#include <estd/string.h>

// DEBT: Move this out to a 'port.cpp' test
#include <estd/port/esp-idf/fake/esp_log.h>

static estd::layer1::string<256> fake_log_out;

static int fake_log_write(const char* fmt, va_list args)
{
return vsnprintf(fake_log_out.data(), fake_log_out.max_size(), fmt, args);
}

TEST_CASE("port (cross plat specific) tests")
{
esp_log_set_vprintf(fake_log_write);

SECTION("fake ESP-IDF")
{
esp_log_write(ESP_LOG_INFO, "Tag1", "hello: %s", "2u");

REQUIRE(fake_log_out == "hello: 2u");
}
}

0 comments on commit 15e7040

Please sign in to comment.