Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor logger #17

Merged
merged 3 commits into from
Jul 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/memsize.baseline
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
build
.cache
llvm
.cscope*
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -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 |

22 changes: 22 additions & 0 deletions include/logger.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <k@libmcu.org>
*
* 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 */
22 changes: 0 additions & 22 deletions include/logging.h

This file was deleted.

38 changes: 38 additions & 0 deletions src/logger.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* SPDX-FileCopyrightText: 2023 Kyunghwan Kwon <k@libmcu.org>
*
* SPDX-License-Identifier: MIT
*/

#include "logger.h"
#include <stdio.h>
#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();
}
31 changes: 0 additions & 31 deletions src/logging.c

This file was deleted.

6 changes: 2 additions & 4 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down