diff --git a/core/lib/include/log.h b/core/lib/include/log.h index 1fb65f4bae..3485208fd7 100644 --- a/core/lib/include/log.h +++ b/core/lib/include/log.h @@ -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__ */ /** diff --git a/tests/sys/log_unit/Makefile b/tests/sys/log_unit/Makefile new file mode 100644 index 0000000000..6836f89966 --- /dev/null +++ b/tests/sys/log_unit/Makefile @@ -0,0 +1,6 @@ +include ../Makefile.sys_common + +# Enable debug log level +CFLAGS += -DLOG_LEVEL=4 + +include $(RIOTBASE)/Makefile.include diff --git a/tests/sys/log_unit/log_with_unit.c b/tests/sys/log_unit/log_with_unit.c new file mode 100644 index 0000000000..283a0ce04b --- /dev/null +++ b/tests/sys/log_unit/log_with_unit.c @@ -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 + */ + +#define LOG_UNIT "log_unit" +#include "log.h" + +void log_with_unit(void) +{ + LOG_INFO("Hello!\n"); + LOG_INFO("Hello %s!\n", "world"); +} diff --git a/tests/sys/log_unit/log_without_unit.c b/tests/sys/log_unit/log_without_unit.c new file mode 100644 index 0000000000..772a8bb967 --- /dev/null +++ b/tests/sys/log_unit/log_without_unit.c @@ -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 + */ +#include "log.h" + +void log_without_unit(void) +{ + LOG_INFO("Hello!\n"); + LOG_INFO("Hello %s!\n", "world"); +} diff --git a/tests/sys/log_unit/main.c b/tests/sys/log_unit/main.c new file mode 100644 index 0000000000..024512f64c --- /dev/null +++ b/tests/sys/log_unit/main.c @@ -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 + */ + +void log_with_unit(void); +void log_without_unit(void); + +int main(void) +{ + log_with_unit(); + log_without_unit(); + + return 0; +} diff --git a/tests/sys/log_unit/tests/01-run.py b/tests/sys/log_unit/tests/01-run.py new file mode 100755 index 0000000000..0eedb7e44e --- /dev/null +++ b/tests/sys/log_unit/tests/01-run.py @@ -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))