logger: Add native backend for native_posix

Add backend to be used in native_posix arch.

Signed-off-by: Andrei Emeltchenko <andrei.emeltchenko@intel.com>
This commit is contained in:
Andrei Emeltchenko 2018-09-13 12:52:32 +03:00 committed by Carles Cufí
commit e65ee6870e
6 changed files with 111 additions and 3 deletions

View file

@ -0,0 +1,21 @@
/*
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef LOG_BACKEND_NATIVE_POSIX_H
#define LOG_BACKEND_NATIVE_POSIX_H
#include <logging/log_backend.h>
#ifdef __cplusplus
extern "C" {
#endif
extern const struct log_backend_api log_backend_native_posix_api;
#ifdef __cplusplus
}
#endif
#endif /* LOG_BACKEND_NATIVE_POSIX_H */

View file

@ -13,3 +13,8 @@ zephyr_sources_ifdef(
CONFIG_LOG_BACKEND_UART
log_backend_uart.c
)
zephyr_sources_ifdef(
CONFIG_LOG_BACKEND_NATIVE_POSIX
log_backend_native_posix.c
)

View file

@ -270,18 +270,26 @@ config LOG_BACKEND_UART
help
When enabled backend is using UART to output logs.
config LOG_BACKEND_NATIVE_POSIX
bool "Enable native backend"
depends on ARCH_POSIX
default n
help
Enable backend in native_posix
config LOG_BACKEND_SHOW_COLOR
bool "Enable colors in the backend"
depends on LOG_BACKEND_UART
depends on LOG_BACKEND_UART || LOG_BACKEND_NATIVE_POSIX
default y
help
When enabled UART backend prints errors in red and warning in yellow.
config LOG_BACKEND_FORMAT_TIMESTAMP
bool "Enable timestamp formatting in the backend"
depends on LOG_BACKEND_UART
depends on LOG_BACKEND_UART || LOG_BACKEND_NATIVE_POSIX
default y
help
When enabled timestamp is formatted to hh:mm:ss:ms,us.
endif
endmenu

View file

@ -0,0 +1,61 @@
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
* Copyright (c) 2018 Intel Corporation
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdio.h>
#include <logging/log_backend_native_posix.h>
#include <logging/log_core.h>
#include <logging/log_msg.h>
#include <logging/log_output.h>
#include <device.h>
#include <uart.h>
static u8_t buf[2048];
int char_out(u8_t *data, size_t length, void *ctx)
{
for (size_t i = 0; i < length; i++) {
putchar(data[i]);
}
return length;
}
LOG_OUTPUT_DEFINE(log_output, char_out, buf, 1);
static void put(const struct log_backend *const backend,
struct log_msg *msg)
{
log_msg_get(msg);
u32_t flags = 0;
if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
if (posix_trace_over_tty(0)) {
flags |= LOG_OUTPUT_FLAG_COLORS;
}
}
if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
}
log_output_msg_process(&log_output, msg, flags);
log_msg_put(msg);
}
static void panic(struct log_backend const *const backend)
{
/* Nothing to be done, this backend can always process logs */
}
const struct log_backend_api log_backend_native_posix_api = {
.put = put,
.panic = panic,
};

View file

@ -10,6 +10,7 @@
#include <logging/log_ctrl.h>
#include <logging/log_output.h>
#include <logging/log_backend_uart.h>
#include <logging/log_backend_native_posix.h>
#include <misc/printk.h>
#include <assert.h>
#include <atomic.h>
@ -25,6 +26,13 @@ const struct log_backend *uart_backend = &log_backend_uart;
const struct log_backend *uart_backend;
#endif
#ifdef CONFIG_LOG_BACKEND_NATIVE_POSIX
LOG_BACKEND_DEFINE(log_backend_native_posix, log_backend_native_posix_api);
const struct log_backend *native_posix_backend = &log_backend_native_posix;
#else
#define native_posix_backend NULL
#endif
static struct log_list_t list;
static atomic_t initialized;
static bool panic_mode;
@ -265,6 +273,11 @@ void log_init(void)
log_backend_uart_init();
log_backend_activate(uart_backend, NULL);
}
if (IS_ENABLED(CONFIG_LOG_BACKEND_NATIVE_POSIX)) {
backend_filter_init(native_posix_backend);
log_backend_activate(native_posix_backend, NULL);
}
}
static void thread_set(k_tid_t process_tid)

View file

@ -69,7 +69,7 @@ static int print_formatted(const struct log_output *log_output,
int length = 0;
va_start(args, fmt);
#ifndef CONFIG_NEWLIB_LIBC
#if !defined(CONFIG_NEWLIB_LIBC) && !defined(CONFIG_ARCH_POSIX)
length = _prf(out_func, (void *)log_output, (char *)fmt, args);
#else
_vprintk(out_func, (void *)log_output, fmt, args);