Skip to content

Commit 23d0258

Browse files
committed
core/log: add optional log unit prefix
1 parent 40e5627 commit 23d0258

File tree

6 files changed

+101
-2
lines changed

6 files changed

+101
-2
lines changed

core/lib/include/log.h

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
* This header offers a bunch of "LOG_*" functions that, with the default
2020
* implementation, just use printf, but honour a verbosity level.
2121
*
22+
* If you want a logging unit name to be prefixed to the logs, define LOG_UNIT
23+
* in the source file before including this header.
24+
*
2225
* If desired, it is possible to implement a log module which then will be used
2326
* instead the default printf-based implementation. In order to do so, the log
2427
* module has to
@@ -70,18 +73,29 @@ enum {
7073
#define LOG_LEVEL LOG_INFO
7174
#endif
7275

76+
/**
77+
* @brief Log with/without unit.
78+
*
79+
* @note Internal use only, use @ref LOG() instead.
80+
*/
81+
#ifdef LOG_UNIT
82+
# define LOG_WRITE(level, fmt, ...) log_write((level), "%s: "fmt, LOG_UNIT, ##__VA_ARGS__)
83+
#else
84+
# define LOG_WRITE(level, ...) log_write((level), __VA_ARGS__)
85+
#endif
86+
7387
/**
7488
* @brief Log message if level <= LOG_LEVEL
7589
*/
7690
#ifdef __clang__ /* following pragmas required for clang 3.8.0 */
7791
#define LOG(level, ...) do { \
7892
_Pragma("clang diagnostic push") \
7993
_Pragma("clang diagnostic ignored \"-Wtautological-compare\"") \
80-
if ((level) <= LOG_LEVEL) log_write((level), __VA_ARGS__); } while (0U) \
94+
if ((level) <= LOG_LEVEL) LOG_WRITE((level), __VA_ARGS__); } while (0U) \
8195
_Pragma("clang diagnostic pop")
8296
#else
8397
#define LOG(level, ...) do { \
84-
if ((level) <= LOG_LEVEL) log_write((level), __VA_ARGS__); } while (0U)
98+
if ((level) <= LOG_LEVEL) LOG_WRITE((level), __VA_ARGS__); } while (0U)
8599
#endif /* __clang__ */
86100

87101
/**

tests/sys/log_unit/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
include ../Makefile.sys_common
2+
3+
# Enable debug log level
4+
CFLAGS += -DLOG_LEVEL=4
5+
6+
include $(RIOTBASE)/Makefile.include

tests/sys/log_unit/log_with_unit.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 ML!PA Consulting GmbH
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
6+
/**
7+
* @file
8+
* @brief Test logging with and log unit.
9+
*
10+
* @author Mihai Renea <[email protected]>
11+
*/
12+
13+
#define LOG_UNIT "log_unit"
14+
#include "log.h"
15+
16+
void log_with_unit(void)
17+
{
18+
LOG_INFO("Hello!\n");
19+
LOG_INFO("Hello %s!\n", "world");
20+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 ML!PA Consulting GmbH
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
6+
/**
7+
* @file
8+
* @brief Test logging with and without log unit.
9+
*
10+
* @author Mihai Renea <[email protected]>
11+
*/
12+
#include "log.h"
13+
14+
void log_without_unit(void)
15+
{
16+
LOG_INFO("Hello!\n");
17+
LOG_INFO("Hello %s!\n", "world");
18+
}

tests/sys/log_unit/main.c

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* SPDX-FileCopyrightText: 2025 ML!PA Consulting GmbH
3+
* SPDX-License-Identifier: LGPL-2.1-only
4+
*/
5+
6+
/**
7+
* @file
8+
* @brief Test logging with and without log unit.
9+
*
10+
* @author Mihai Renea <[email protected]>
11+
*/
12+
13+
void log_with_unit(void);
14+
void log_without_unit(void);
15+
16+
int main(void)
17+
{
18+
log_with_unit();
19+
log_without_unit();
20+
21+
return 0;
22+
}

tests/sys/log_unit/tests/01-run.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3
2+
3+
import sys
4+
from testrunner import run
5+
6+
7+
def testfunc(child):
8+
child.expect_exact("log_unit: Hello!")
9+
child.expect_exact("log_unit: Hello world!")
10+
11+
# no unit name - match the beginning of the line
12+
child.expect("\n")
13+
child.expect("^Hello!")
14+
child.expect("\n")
15+
child.expect("^Hello world!")
16+
17+
18+
if __name__ == "__main__":
19+
sys.exit(run(testfunc))

0 commit comments

Comments
 (0)