2018-05-16 14:50:33 +08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018 Nordic Semiconductor ASA
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
2018-09-14 20:48:54 +08:00
|
|
|
|
|
|
|
#include <logging/log_backend.h>
|
2018-05-16 14:50:33 +08:00
|
|
|
#include <logging/log_core.h>
|
|
|
|
#include <logging/log_msg.h>
|
|
|
|
#include <logging/log_output.h>
|
|
|
|
#include <device.h>
|
|
|
|
#include <uart.h>
|
2018-08-01 20:57:41 +08:00
|
|
|
#include <assert.h>
|
2018-05-16 14:50:33 +08:00
|
|
|
|
2018-09-19 20:38:23 +08:00
|
|
|
static int char_out(u8_t *data, size_t length, void *ctx)
|
2018-05-16 14:50:33 +08:00
|
|
|
{
|
|
|
|
struct device *dev = (struct device *)ctx;
|
|
|
|
|
|
|
|
for (size_t i = 0; i < length; i++) {
|
|
|
|
uart_poll_out(dev, data[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
return length;
|
|
|
|
}
|
|
|
|
|
|
|
|
static u8_t buf;
|
|
|
|
|
2018-08-01 20:57:41 +08:00
|
|
|
LOG_OUTPUT_DEFINE(log_output, char_out, &buf, 1);
|
2018-05-16 14:50:33 +08:00
|
|
|
|
|
|
|
static void put(const struct log_backend *const backend,
|
|
|
|
struct log_msg *msg)
|
|
|
|
{
|
|
|
|
log_msg_get(msg);
|
|
|
|
|
2018-09-26 20:18:55 +08:00
|
|
|
u32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
|
2018-05-16 14:50:33 +08:00
|
|
|
|
2018-09-14 14:52:16 +08:00
|
|
|
if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
|
2018-05-16 14:50:33 +08:00
|
|
|
flags |= LOG_OUTPUT_FLAG_COLORS;
|
|
|
|
}
|
|
|
|
|
2018-09-14 14:52:16 +08:00
|
|
|
if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
|
2018-05-16 14:50:33 +08:00
|
|
|
flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
|
|
|
|
}
|
|
|
|
|
2018-08-01 20:57:41 +08:00
|
|
|
log_output_msg_process(&log_output, msg, flags);
|
2018-05-16 14:50:33 +08:00
|
|
|
|
|
|
|
log_msg_put(msg);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-09-19 20:38:23 +08:00
|
|
|
static void log_backend_uart_init(void)
|
2018-05-16 14:50:33 +08:00
|
|
|
{
|
2018-08-01 20:57:41 +08:00
|
|
|
struct device *dev;
|
|
|
|
|
|
|
|
dev = device_get_binding(CONFIG_UART_CONSOLE_ON_DEV_NAME);
|
|
|
|
assert(dev);
|
|
|
|
|
|
|
|
log_output_ctx_set(&log_output, dev);
|
2018-05-16 14:50:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void panic(struct log_backend const *const backend)
|
|
|
|
{
|
2019-01-04 15:52:45 +08:00
|
|
|
log_output_flush(&log_output);
|
2018-05-16 14:50:33 +08:00
|
|
|
}
|
|
|
|
|
2018-12-07 17:35:02 +08:00
|
|
|
static void dropped(const struct log_backend *const backend, u32_t cnt)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(backend);
|
|
|
|
|
|
|
|
log_output_dropped_process(&log_output, cnt);
|
|
|
|
}
|
|
|
|
|
2019-01-10 15:38:33 +08:00
|
|
|
static void sync_string(const struct log_backend *const backend,
|
|
|
|
struct log_msg_ids src_level, u32_t timestamp,
|
|
|
|
const char *fmt, va_list ap)
|
|
|
|
{
|
|
|
|
u32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
|
|
|
|
u32_t key;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
|
|
|
|
flags |= LOG_OUTPUT_FLAG_COLORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
|
|
|
|
flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
log_output_string(&log_output, src_level, timestamp, fmt, ap, flags);
|
|
|
|
irq_unlock(key);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sync_hexdump(const struct log_backend *const backend,
|
|
|
|
struct log_msg_ids src_level, u32_t timestamp,
|
|
|
|
const char *metadata, const u8_t *data, u32_t length)
|
|
|
|
{
|
|
|
|
u32_t flags = LOG_OUTPUT_FLAG_LEVEL | LOG_OUTPUT_FLAG_TIMESTAMP;
|
|
|
|
u32_t key;
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_LOG_BACKEND_SHOW_COLOR)) {
|
|
|
|
flags |= LOG_OUTPUT_FLAG_COLORS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_LOG_BACKEND_FORMAT_TIMESTAMP)) {
|
|
|
|
flags |= LOG_OUTPUT_FLAG_FORMAT_TIMESTAMP;
|
|
|
|
}
|
|
|
|
|
|
|
|
key = irq_lock();
|
|
|
|
log_output_hexdump(&log_output, src_level, timestamp,
|
|
|
|
metadata, data, length, flags);
|
|
|
|
irq_unlock(key);
|
|
|
|
}
|
|
|
|
|
2018-05-16 14:50:33 +08:00
|
|
|
const struct log_backend_api log_backend_uart_api = {
|
2019-01-10 15:38:33 +08:00
|
|
|
.put = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? NULL : put,
|
|
|
|
.put_sync_string = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ?
|
|
|
|
sync_string : NULL,
|
|
|
|
.put_sync_hexdump = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ?
|
|
|
|
sync_hexdump : NULL,
|
2018-05-16 14:50:33 +08:00
|
|
|
.panic = panic,
|
2018-09-14 20:31:26 +08:00
|
|
|
.init = log_backend_uart_init,
|
2019-01-10 15:38:33 +08:00
|
|
|
.dropped = IS_ENABLED(CONFIG_LOG_IMMEDIATE) ? NULL : dropped,
|
2018-05-16 14:50:33 +08:00
|
|
|
};
|
2018-09-14 20:48:54 +08:00
|
|
|
|
2018-10-11 12:28:18 +08:00
|
|
|
LOG_BACKEND_DEFINE(log_backend_uart, log_backend_uart_api, true);
|