From c101c4467b092ce4c49180fa013aaf15a79b6099 Mon Sep 17 00:00:00 2001 From: Kyunghwan Kwon Date: Sun, 28 Jul 2024 06:38:22 +0900 Subject: [PATCH] Refactor logger (#17) --- .github/memsize.baseline | 2 +- .gitignore | 2 ++ README.md | 28 ++++++++++++++++++++++++++++ external/libmcu | 2 +- include/logger.h | 22 ++++++++++++++++++++++ include/logging.h | 22 ---------------------- src/logger.c | 38 ++++++++++++++++++++++++++++++++++++++ src/logging.c | 31 ------------------------------- src/main.c | 6 ++---- 9 files changed, 94 insertions(+), 59 deletions(-) create mode 100644 README.md create mode 100644 include/logger.h delete mode 100644 include/logging.h create mode 100644 src/logger.c delete mode 100644 src/logging.c diff --git a/.github/memsize.baseline b/.github/memsize.baseline index 08c5d51..476219f 100644 --- a/.github/memsize.baseline +++ b/.github/memsize.baseline @@ -1,2 +1,2 @@ text data bss dec hex filename - 191909 66512 504429 762850 ba3e2 build/template.elf + 192025 66512 504357 762894 ba40e build/template.elf diff --git a/.gitignore b/.gitignore index 9785597..af633f7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ build .cache +llvm +.cscope* diff --git a/README.md b/README.md new file mode 100644 index 0000000..7a96960 --- /dev/null +++ b/README.md @@ -0,0 +1,28 @@ +## Directory Structure + +``` +. +├── docs +├── external +│   └── libmcu +├── include +├── ports +│   └── esp-idf +├── src +└── tests + ├── fakes + ├── mocks + ├── runners + ├── src + └── stubs +``` + +| Directory | Desc. | +| --------- | ------------------------------------------------------------------- | +| docs | Documentation | +| external | Third-party libraries or SDK. e.g. ESP-IDF | +| include | The project header files | +| ports | Wrappers or glue code to bring third-party drivers into the project | +| src | Application code. No hardware or platform-specific code | +| tests | Test codes | + diff --git a/external/libmcu b/external/libmcu index 20d3459..59f56ef 160000 --- a/external/libmcu +++ b/external/libmcu @@ -1 +1 @@ -Subproject commit 20d3459b26c9971d441cbb5824e1fd570516af50 +Subproject commit 59f56ef4557a1ce5a8be93b582957be5d9d70548 diff --git a/include/logger.h b/include/logger.h new file mode 100644 index 0000000..0567c28 --- /dev/null +++ b/include/logger.h @@ -0,0 +1,22 @@ +/* + * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon + * + * SPDX-License-Identifier: MIT + */ + +#ifndef LOGGER_H +#define LOGGER_H + +#if defined(__cplusplus) +extern "C" { +#endif + +#include "libmcu/logging.h" + +void logger_init(void); + +#if defined(__cplusplus) +} +#endif + +#endif /* LOGGER_H */ diff --git a/include/logging.h b/include/logging.h deleted file mode 100644 index 0be737b..0000000 --- a/include/logging.h +++ /dev/null @@ -1,22 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon - * - * SPDX-License-Identifier: MIT - */ - -#ifndef LOGGING_H -#define LOGGING_H - -#if defined(__cplusplus) -extern "C" { -#endif - -#include "libmcu/logging.h" - -void logging_stdout_backend_init(void); - -#if defined(__cplusplus) -} -#endif - -#endif /* LOGGING_H */ diff --git a/src/logger.c b/src/logger.c new file mode 100644 index 0000000..0fb0f01 --- /dev/null +++ b/src/logger.c @@ -0,0 +1,38 @@ +/* + * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon + * + * SPDX-License-Identifier: MIT + */ + +#include "logger.h" +#include +#include "libmcu/board.h" + +static size_t logger_writer(const void *data, size_t size) +{ + unused(size); + static char buf[LOGGING_MESSAGE_MAXLEN]; + size_t len = logging_stringify(buf, sizeof(buf)-2, data); + + buf[len++] = '\n'; + buf[len] = '\0'; + + const size_t rc = fwrite(buf, len, 1, stdout); + + return rc == 0? len : 0; +} + +static void initialize_backend_stdout(void) +{ + static struct logging_backend log_console = { + .write = logger_writer, + }; + + logging_add_backend(&log_console); +} + +void logger_init(void) +{ + logging_init(board_get_time_since_boot_ms); + initialize_backend_stdout(); +} diff --git a/src/logging.c b/src/logging.c deleted file mode 100644 index 85f1315..0000000 --- a/src/logging.c +++ /dev/null @@ -1,31 +0,0 @@ -/* - * SPDX-FileCopyrightText: 2023 Kyunghwan Kwon - * - * SPDX-License-Identifier: MIT - */ - -#include "logging.h" -#include - -static size_t logging_stdout_writer(const void *data, size_t size) -{ - unused(size); - static char buf[LOGGING_MESSAGE_MAXLEN]; - size_t len = logging_stringify(buf, sizeof(buf)-1, data); - - buf[len++] = '\n'; - buf[len] = '\0'; - - const size_t rc = fwrite(buf, len, 1, stdout); - - return rc == 0? len : 0; -} - -void logging_stdout_backend_init(void) -{ - static struct logging_backend log_console = { - .write = logging_stdout_writer, - }; - - logging_add_backend(&log_console); -} diff --git a/src/main.c b/src/main.c index e4789fd..ba6afee 100644 --- a/src/main.c +++ b/src/main.c @@ -9,14 +9,12 @@ #include "libmcu/gpio.h" #include "pinmap.h" -#include "logging.h" +#include "logger.h" int main(void) { board_init(); /* should be called very first. */ - - logging_init(board_get_time_since_boot_ms); - logging_stdout_backend_init(); + logger_init(); const board_reboot_reason_t reboot_reason = board_get_reboot_reason(); info("[%s] %s %s", board_get_reboot_reason_string(reboot_reason),