logging: printk: Fix LOG_PRINTK for v2

Fixed a dependency from printk.h to logging headers which in
certain configurations could lead to circular dependencies.
Cleaned up printk.c to call z_log_vprintk from vprintk.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2022-01-19 14:43:00 +01:00 committed by Carles Cufí
commit a40ca6fd1c
4 changed files with 56 additions and 66 deletions

View file

@ -256,18 +256,16 @@ extern "C" {
* @brief Writes an formatted string to the log. * @brief Writes an formatted string to the log.
* *
* @details Conditionally compiled (see CONFIG_LOG_PRINTK). Function provides * @details Conditionally compiled (see CONFIG_LOG_PRINTK). Function provides
* printk functionality. It is inefficient compared to standard logging * printk functionality.
* because string formatting is performed in the call context and not deferred *
* to the log processing context (@ref log_process). * It is less efficient compared to standard logging because static packaging
* cannot be used. When CONFIG_LOG1 is used string formatting is performed in the
* call context and not deferred to the log processing context (@ref log_process).
* *
* @param fmt Formatted string to output. * @param fmt Formatted string to output.
* @param ap Variable parameters. * @param ap Variable parameters.
*/ */
void z_log_printk(const char *fmt, va_list ap); void z_log_vprintk(const char *fmt, va_list ap);
static inline void log_printk(const char *fmt, va_list ap)
{
z_log_printk(fmt, ap);
}
/** @brief Copy transient string to a buffer from internal, logger pool. /** @brief Copy transient string to a buffer from internal, logger pool.
* *

View file

@ -12,9 +12,6 @@
#include <stddef.h> #include <stddef.h>
#include <stdarg.h> #include <stdarg.h>
#include <inttypes.h> #include <inttypes.h>
#if defined(CONFIG_LOG_PRINTK) && defined(CONFIG_LOG2)
#include <logging/log.h>
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
@ -47,18 +44,8 @@ extern "C" {
*/ */
#ifdef CONFIG_PRINTK #ifdef CONFIG_PRINTK
#if defined(CONFIG_LOG_PRINTK) && defined(CONFIG_LOG2)
#define printk(...) Z_LOG_PRINTK(__VA_ARGS__)
static inline __printf_like(1, 0) void vprintk(const char *fmt, va_list ap)
{
z_log_msg2_runtime_vcreate(CONFIG_LOG_DOMAIN_ID, NULL,
LOG_LEVEL_INTERNAL_RAW_STRING, NULL, 0,
fmt, ap);
}
#else
extern __printf_like(1, 2) void printk(const char *fmt, ...); extern __printf_like(1, 2) void printk(const char *fmt, ...);
extern __printf_like(1, 0) void vprintk(const char *fmt, va_list ap); extern __printf_like(1, 0) void vprintk(const char *fmt, va_list ap);
#endif /* defined(CONFIG_LOG_PRINTK) && defined(CONFIG_LOG) */
#else #else
static inline __printf_like(1, 2) void printk(const char *fmt, ...) static inline __printf_like(1, 2) void printk(const char *fmt, ...)

View file

@ -27,8 +27,7 @@
#define CONFIG_PRINTK_BUFFER_SIZE 0 #define CONFIG_PRINTK_BUFFER_SIZE 0
#endif #endif
#if defined(CONFIG_PRINTK_SYNC) && \ #if defined(CONFIG_PRINTK_SYNC)
!(defined(CONFIG_LOG_PRINTK) && defined(CONFIG_LOG2))
static struct k_spinlock lock; static struct k_spinlock lock;
#endif #endif
@ -78,10 +77,7 @@ void *__printk_get_hook(void)
{ {
return _char_out; return _char_out;
} }
#endif /* CONFIG_PRINTK */
#if defined(CONFIG_PRINTK) && \
!(defined(CONFIG_LOG_PRINTK) && defined(CONFIG_LOG2))
struct buf_out_context { struct buf_out_context {
int count; int count;
unsigned int buf_count; unsigned int buf_count;
@ -121,6 +117,11 @@ static int char_out(int c, void *ctx_p)
void vprintk(const char *fmt, va_list ap) void vprintk(const char *fmt, va_list ap)
{ {
if (IS_ENABLED(CONFIG_LOG_PRINTK)) {
z_log_vprintk(fmt, ap);
return;
}
if (k_is_user_context()) { if (k_is_user_context()) {
struct buf_out_context ctx = { 0 }; struct buf_out_context ctx = { 0 };
@ -195,16 +196,11 @@ void printk(const char *fmt, ...)
va_start(ap, fmt); va_start(ap, fmt);
if (IS_ENABLED(CONFIG_LOG_PRINTK)) { vprintk(fmt, ap);
log_printk(fmt, ap);
} else {
vprintk(fmt, ap);
}
va_end(ap); va_end(ap);
} }
#endif /* defined(CONFIG_PRINTK) && \ #endif /* defined(CONFIG_PRINTK) */
* !(defined(CONFIG_LOG_PRINTK) && defined(CONFIG_LOG2))
*/
struct str_context { struct str_context {
char *str; char *str;

View file

@ -370,42 +370,51 @@ void log_hexdump(const char *str, const void *data, uint32_t length,
} }
} }
void z_log_printk(const char *fmt, va_list ap) void z_log_vprintk(const char *fmt, va_list ap)
{ {
if (IS_ENABLED(CONFIG_LOG_PRINTK)) { if (!IS_ENABLED(CONFIG_LOG_PRINTK)) {
union { return;
struct log_msg_ids structure; }
uint32_t value;
} src_level_union = {
{
.level = LOG_LEVEL_INTERNAL_RAW_STRING
}
};
if (k_is_user_context()) { if (!IS_ENABLED(CONFIG_LOG1)) {
uint8_t str[CONFIG_LOG_PRINTK_MAX_STRING_LENGTH + 1]; z_log_msg2_runtime_vcreate(CONFIG_LOG_DOMAIN_ID, NULL,
LOG_LEVEL_INTERNAL_RAW_STRING, NULL, 0,
fmt, ap);
return;
}
vsnprintk(str, sizeof(str), fmt, ap); union {
struct log_msg_ids structure;
z_log_string_from_user(src_level_union.value, str); uint32_t value;
} else if (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) { } src_level_union = {
log_generic(src_level_union.structure, fmt, ap, {
LOG_STRDUP_SKIP); .level = LOG_LEVEL_INTERNAL_RAW_STRING
} else {
uint8_t str[CONFIG_LOG_PRINTK_MAX_STRING_LENGTH + 1];
struct log_msg *msg;
int length;
length = vsnprintk(str, sizeof(str), fmt, ap);
length = MIN(length, sizeof(str));
msg = log_msg_hexdump_create(NULL, str, length);
if (msg == NULL) {
return;
}
msg_finalize(msg, src_level_union.structure);
} }
};
if (k_is_user_context()) {
uint8_t str[CONFIG_LOG_PRINTK_MAX_STRING_LENGTH + 1];
vsnprintk(str, sizeof(str), fmt, ap);
z_log_string_from_user(src_level_union.value, str);
} else if (IS_ENABLED(CONFIG_LOG_MODE_IMMEDIATE)) {
log_generic(src_level_union.structure, fmt, ap,
LOG_STRDUP_SKIP);
} else {
uint8_t str[CONFIG_LOG_PRINTK_MAX_STRING_LENGTH + 1];
struct log_msg *msg;
int length;
length = vsnprintk(str, sizeof(str), fmt, ap);
length = MIN(length, sizeof(str));
msg = log_msg_hexdump_create(NULL, str, length);
if (msg == NULL) {
return;
}
msg_finalize(msg, src_level_union.structure);
} }
} }