shell: Extend shell as a log backend

Initial logger backend support added to shell.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2018-08-09 11:40:02 +02:00 committed by Anas Nashif
commit c71a5595dc
6 changed files with 378 additions and 4 deletions

View file

@ -11,7 +11,7 @@
#include <shell/shell_types.h>
#include <shell/shell_history.h>
#include <shell/shell_fprintf.h>
#include <logging/log_backend.h>
#include <shell/shell_log_backend.h>
#include <logging/log_instance.h>
#include <logging/log.h>
#include <misc/util.h>
@ -266,6 +266,19 @@ struct shell_transport {
void *ctx;
};
/** @brief Shell statistics structure. */
struct shell_stats {
u32_t log_lost_cnt; /*!< Lost log counter.*/
};
#if CONFIG_SHELL_STATS
#define SHELL_STATS_DEFINE(_name) static struct shell_stats _name##_stats
#define SHELL_STATS_PTR(_name) (&(_name##_stats))
#else
#define SHELL_STATS_DEFINE(_name)
#define SHELL_STATS_PTR(_name) NULL
#endif /* CONFIG_SHELL_STATS */
/*
* @internal @brief Flags for internal shell usage.
*/
@ -292,6 +305,7 @@ union shell_internal {
enum shell_signal {
SHELL_SIGNAL_RXRDY,
SHELL_SIGNAL_TXDONE,
SHELL_SIGNAL_LOG_MSG,
SHELL_SIGNAL_KILL,
SHELL_SIGNALS
};
@ -344,6 +358,10 @@ struct shell {
const struct shell_fprintf *fprintf_ctx;
struct shell_stats *stats;
const struct shell_log_backend *log_backend;
LOG_INSTANCE_PTR_DECLARE(log);
/*!< New line character, only allowed values: \\n and \\r.*/
@ -368,11 +386,14 @@ struct shell {
static const struct shell _name; \
static struct shell_ctx UTIL_CAT(_name, _ctx); \
static u8_t _name##_out_buffer[CONFIG_SHELL_PRINTF_BUFF_SIZE]; \
SHELL_LOG_BACKEND_DEFINE(_name, _name##_out_buffer, \
CONFIG_SHELL_PRINTF_BUFF_SIZE); \
SHELL_HISTORY_DEFINE(_name, 128, 8);/*todo*/ \
SHELL_FPRINTF_DEFINE(_name## _fprintf, &_name, _name##_out_buffer, \
CONFIG_SHELL_PRINTF_BUFF_SIZE, \
true, shell_print_stream); \
LOG_INSTANCE_REGISTER(shell, _name, CONFIG_SHELL_LOG_LEVEL); \
SHELL_STATS_DEFINE(_name); \
static K_THREAD_STACK_DEFINE(_name##_stack, CONFIG_SHELL_STACK_SIZE);\
static struct k_thread _name##_thread; \
static const struct shell _name = { \
@ -381,6 +402,8 @@ struct shell {
.ctx = &UTIL_CAT(_name, _ctx), \
.history = SHELL_HISTORY_PTR(_name), \
.fprintf_ctx = &_name##_fprintf, \
.stats = SHELL_STATS_PTR(_name), \
.log_backend = SHELL_LOG_BACKEND_PTR(_name), \
LOG_INSTANCE_PTR_INIT(log, shell, _name) \
.newline_char = newline_ch, \
.thread = &_name##_thread, \

View file

@ -0,0 +1,104 @@
/*
* Copyright (c) 2018 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: Apache-2.0
*/
#ifndef SHELL_LOG_BACKEND_H__
#define SHELL_LOG_BACKEND_H__
#include <zephyr.h>
#include <logging/log_backend.h>
#include <logging/log_output.h>
#include <atomic.h>
#ifdef __cplusplus
extern "C" {
#endif
extern const struct log_backend_api log_backend_shell_api;
/** @brief Shell log backend states. */
enum shell_log_backend_state {
SHELL_LOG_BACKEND_UNINIT,
SHELL_LOG_BACKEND_ENABLED,
SHELL_LOG_BACKEND_DISABLED,
SHELL_LOG_BACKEND_PANIC,
};
/** @brief Shell log backend control block (RW data). */
struct shell_log_backend_control_block {
atomic_t cnt;
enum shell_log_backend_state state;
};
/** @brief Shell log backend instance structure (RO data). */
struct shell_log_backend {
const struct log_backend *backend;
struct k_fifo *fifo;
const struct log_output *log_output;
struct shell_log_backend_control_block *control_block;
};
/** @brief Prototype of function outputing processed data. */
int shell_log_backend_output_func(u8_t *data, size_t length, void *ctx);
/** @def SHELL_LOG_BACKEND_DEFINE
* @brief Macro for creating instance of shell log backend.
*
* @param _name Shell name.
* @param _buf Output buffer.
* @param _size Output buffer size.
*/
/** @def SHELL_LOG_BACKEND_PTR
* @brief Macro for retrieving pointer to the instance of shell log backend.
*
* @param _name Shell name.
*/
#if CONFIG_LOG
#define SHELL_LOG_BACKEND_DEFINE(_name, _buf, _size) \
LOG_BACKEND_DEFINE(_name##_backend, log_backend_shell_api); \
K_FIFO_DEFINE(_name##_fifo); \
LOG_OUTPUT_DEFINE(_name##_log_output, shell_log_backend_output_func, \
_buf, _size); \
static struct shell_log_backend_control_block _name##_control_block; \
static const struct shell_log_backend _name##_log_backend = { \
.backend = &_name##_backend, \
.fifo = &_name##_fifo, \
.log_output = &_name##_log_output, \
.control_block = &_name##_control_block \
}
#define SHELL_LOG_BACKEND_PTR(_name) (&_name##_log_backend)
#else /* CONFIG_LOG */
#define SHELL_LOG_BACKEND_DEFINE(_name, _buf, _size) /* empty */
#define SHELL_LOG_BACKEND_PTR(_name) NULL
#endif /* CONFIG_LOG */
/** @brief Enable shell log backend.
*
* @param backend Shell log backend instance.
* @param ctx Pointer to shell instance.
* @param init_log_level Initial log level set to all logging sources.
*/
void shell_log_backend_enable(const struct shell_log_backend *backend,
void *ctx, u32_t init_log_level);
/** @brief Disable shell log backend.
*
* @param backend Shell log backend instance.
*/
void shell_log_backend_disable(const struct shell_log_backend *backend);
/** @brief Trigger processing of one log entry.
*
* @param backend Shell log backend instance.
*
* @return True if message was processed, false if FIFO was empty
*/
bool shell_log_backend_process(const struct shell_log_backend *backend);
#ifdef __cplusplus
}
#endif
#endif /* SHELL_LOG_BACKEND_H__ */