mirror of
https://github.com/RIOT-OS/RIOT.git
synced 2025-12-15 01:23:49 +01:00
core/log: add optional log unit prefix
This commit is contained in:
parent
40e5627981
commit
23d02583e4
@ -19,6 +19,9 @@
|
|||||||
* This header offers a bunch of "LOG_*" functions that, with the default
|
* This header offers a bunch of "LOG_*" functions that, with the default
|
||||||
* implementation, just use printf, but honour a verbosity level.
|
* 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
|
* 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
|
* instead the default printf-based implementation. In order to do so, the log
|
||||||
* module has to
|
* module has to
|
||||||
@ -70,6 +73,17 @@ enum {
|
|||||||
#define LOG_LEVEL LOG_INFO
|
#define LOG_LEVEL LOG_INFO
|
||||||
#endif
|
#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
|
* @brief Log message if level <= LOG_LEVEL
|
||||||
*/
|
*/
|
||||||
@ -77,11 +91,11 @@ enum {
|
|||||||
#define LOG(level, ...) do { \
|
#define LOG(level, ...) do { \
|
||||||
_Pragma("clang diagnostic push") \
|
_Pragma("clang diagnostic push") \
|
||||||
_Pragma("clang diagnostic ignored \"-Wtautological-compare\"") \
|
_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")
|
_Pragma("clang diagnostic pop")
|
||||||
#else
|
#else
|
||||||
#define LOG(level, ...) do { \
|
#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__ */
|
#endif /* __clang__ */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
6
tests/sys/log_unit/Makefile
Normal file
6
tests/sys/log_unit/Makefile
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
include ../Makefile.sys_common
|
||||||
|
|
||||||
|
# Enable debug log level
|
||||||
|
CFLAGS += -DLOG_LEVEL=4
|
||||||
|
|
||||||
|
include $(RIOTBASE)/Makefile.include
|
||||||
20
tests/sys/log_unit/log_with_unit.c
Normal file
20
tests/sys/log_unit/log_with_unit.c
Normal 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");
|
||||||
|
}
|
||||||
18
tests/sys/log_unit/log_without_unit.c
Normal file
18
tests/sys/log_unit/log_without_unit.c
Normal 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
22
tests/sys/log_unit/main.c
Normal 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;
|
||||||
|
}
|
||||||
19
tests/sys/log_unit/tests/01-run.py
Executable file
19
tests/sys/log_unit/tests/01-run.py
Executable 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))
|
||||||
Loading…
x
Reference in New Issue
Block a user