1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2025-12-14 17:13:50 +01:00

core/log: add optional log unit prefix

This commit is contained in:
Mihai Renea 2025-07-16 14:25:26 +02:00
parent 40e5627981
commit 23d02583e4
6 changed files with 101 additions and 2 deletions

View File

@ -19,6 +19,9 @@
* This header offers a bunch of "LOG_*" functions that, with the default
* implementation, just use printf, but honour a verbosity level.
*
* If you want a logging unit name to be prefixed to the logs, define LOG_UNIT
* in the source file before including this header.
*
* If desired, it is possible to implement a log module which then will be used
* instead the default printf-based implementation. In order to do so, the log
* module has to
@ -70,6 +73,17 @@ enum {
#define LOG_LEVEL LOG_INFO
#endif
/**
* @brief Log with/without unit.
*
* @note Internal use only, use @ref LOG() instead.
*/
#ifdef LOG_UNIT
# define LOG_WRITE(level, fmt, ...) log_write((level), "%s: "fmt, LOG_UNIT, ##__VA_ARGS__)
#else
# define LOG_WRITE(level, ...) log_write((level), __VA_ARGS__)
#endif
/**
* @brief Log message if level <= LOG_LEVEL
*/
@ -77,11 +91,11 @@ enum {
#define LOG(level, ...) do { \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wtautological-compare\"") \
if ((level) <= LOG_LEVEL) log_write((level), __VA_ARGS__); } while (0U) \
if ((level) <= LOG_LEVEL) LOG_WRITE((level), __VA_ARGS__); } while (0U) \
_Pragma("clang diagnostic pop")
#else
#define LOG(level, ...) do { \
if ((level) <= LOG_LEVEL) log_write((level), __VA_ARGS__); } while (0U)
if ((level) <= LOG_LEVEL) LOG_WRITE((level), __VA_ARGS__); } while (0U)
#endif /* __clang__ */
/**

View File

@ -0,0 +1,6 @@
include ../Makefile.sys_common
# Enable debug log level
CFLAGS += -DLOG_LEVEL=4
include $(RIOTBASE)/Makefile.include

View File

@ -0,0 +1,20 @@
/*
* SPDX-FileCopyrightText: 2025 ML!PA Consulting GmbH
* SPDX-License-Identifier: LGPL-2.1-only
*/
/**
* @file
* @brief Test logging with and log unit.
*
* @author Mihai Renea <mihai.renea@ml-pa.com>
*/
#define LOG_UNIT "log_unit"
#include "log.h"
void log_with_unit(void)
{
LOG_INFO("Hello!\n");
LOG_INFO("Hello %s!\n", "world");
}

View File

@ -0,0 +1,18 @@
/*
* SPDX-FileCopyrightText: 2025 ML!PA Consulting GmbH
* SPDX-License-Identifier: LGPL-2.1-only
*/
/**
* @file
* @brief Test logging with and without log unit.
*
* @author Mihai Renea <mihai.renea@ml-pa.com>
*/
#include "log.h"
void log_without_unit(void)
{
LOG_INFO("Hello!\n");
LOG_INFO("Hello %s!\n", "world");
}

22
tests/sys/log_unit/main.c Normal file
View File

@ -0,0 +1,22 @@
/*
* SPDX-FileCopyrightText: 2025 ML!PA Consulting GmbH
* SPDX-License-Identifier: LGPL-2.1-only
*/
/**
* @file
* @brief Test logging with and without log unit.
*
* @author Mihai Renea <mihai.renea@ml-pa.com>
*/
void log_with_unit(void);
void log_without_unit(void);
int main(void)
{
log_with_unit();
log_without_unit();
return 0;
}

View File

@ -0,0 +1,19 @@
#!/usr/bin/env python3
import sys
from testrunner import run
def testfunc(child):
child.expect_exact("log_unit: Hello!")
child.expect_exact("log_unit: Hello world!")
# no unit name - match the beginning of the line
child.expect("\n")
child.expect("^Hello!")
child.expect("\n")
child.expect("^Hello world!")
if __name__ == "__main__":
sys.exit(run(testfunc))