1 /* 2 * Copyright (c) 2018 Nordic Semiconductor ASA 3 * 4 * SPDX-License-Identifier: Apache-2.0 5 */ 6 7 #ifndef SHELL_LOG_BACKEND_H__ 8 #define SHELL_LOG_BACKEND_H__ 9 10 #include <zephyr/kernel.h> 11 #include <zephyr/logging/log_backend.h> 12 #include <zephyr/logging/log_output.h> 13 #include <zephyr/sys/mpsc_pbuf.h> 14 #include <zephyr/sys/atomic.h> 15 #ifdef __cplusplus 16 extern "C" { 17 #endif 18 19 extern const struct log_backend_api log_backend_shell_api; 20 21 /** @brief Shell log backend states. */ 22 enum shell_log_backend_state { 23 SHELL_LOG_BACKEND_UNINIT, 24 SHELL_LOG_BACKEND_ENABLED, 25 SHELL_LOG_BACKEND_DISABLED, 26 SHELL_LOG_BACKEND_PANIC, 27 }; 28 29 /** @brief Shell log backend control block (RW data). */ 30 struct shell_log_backend_control_block { 31 atomic_t dropped_cnt; 32 enum shell_log_backend_state state; 33 }; 34 35 /** @brief Shell log backend instance structure (RO data). */ 36 struct shell_log_backend { 37 const struct log_backend *backend; 38 const struct log_output *log_output; 39 struct shell_log_backend_control_block *control_block; 40 uint32_t timeout; 41 const struct mpsc_pbuf_buffer_config *mpsc_buffer_config; 42 struct mpsc_pbuf_buffer *mpsc_buffer; 43 }; 44 45 /** @brief Shell log backend message structure. */ 46 struct shell_log_backend_msg { 47 struct log_msg *msg; 48 uint32_t timestamp; 49 }; 50 51 /** @brief Prototype of function outputting processed data. */ 52 int z_shell_log_backend_output_func(uint8_t *data, size_t length, void *ctx); 53 54 /** @def Z_SHELL_LOG_BACKEND_DEFINE 55 * @brief Macro for creating instance of shell log backend. 56 * 57 * @param _name Shell name. 58 * @param _buf Output buffer. 59 * @param _size Output buffer size. 60 * @param _queue_size Log message queue size. 61 * @param _timeout Timeout in milliseconds for pending on queue full. 62 * Message is dropped on timeout. 63 */ 64 /** @def Z_SHELL_LOG_BACKEND_PTR 65 * @brief Macro for retrieving pointer to the instance of shell log backend. 66 * 67 * @param _name Shell name. 68 */ 69 #ifdef CONFIG_SHELL_LOG_BACKEND 70 #define Z_SHELL_LOG_BACKEND_DEFINE(_name, _buf, _size, _queue_size, _timeout) \ 71 LOG_BACKEND_DEFINE(_name##_backend, log_backend_shell_api, false); \ 72 LOG_OUTPUT_DEFINE(_name##_log_output, z_shell_log_backend_output_func,\ 73 _buf, _size); \ 74 static struct shell_log_backend_control_block _name##_control_block; \ 75 static uint32_t __aligned(Z_LOG_MSG_ALIGNMENT) \ 76 _name##_buf[_queue_size / sizeof(uint32_t)]; \ 77 const struct mpsc_pbuf_buffer_config _name##_mpsc_buffer_config = { \ 78 .buf = _name##_buf, \ 79 .size = ARRAY_SIZE(_name##_buf), \ 80 .notify_drop = NULL, \ 81 .get_wlen = log_msg_generic_get_wlen, \ 82 .flags = MPSC_PBUF_MODE_OVERWRITE, \ 83 }; \ 84 struct mpsc_pbuf_buffer _name##_mpsc_buffer; \ 85 static const struct shell_log_backend _name##_log_backend = { \ 86 .backend = &_name##_backend, \ 87 .log_output = &_name##_log_output, \ 88 .control_block = &_name##_control_block, \ 89 .timeout = _timeout, \ 90 .mpsc_buffer_config = &_name##_mpsc_buffer_config, \ 91 .mpsc_buffer = &_name##_mpsc_buffer, \ 92 } 93 94 #define Z_SHELL_LOG_BACKEND_PTR(_name) (&_name##_log_backend) 95 #else /* CONFIG_LOG */ 96 #define Z_SHELL_LOG_BACKEND_DEFINE(_name, _buf, _size, _queue_size, _timeout) 97 #define Z_SHELL_LOG_BACKEND_PTR(_name) NULL 98 #endif /* CONFIG_LOG */ 99 100 /** @brief Enable shell log backend. 101 * 102 * @param backend Shell log backend instance. 103 * @param ctx Pointer to shell instance. 104 * @param init_log_level Initial log level set to all logging sources. 105 */ 106 void z_shell_log_backend_enable(const struct shell_log_backend *backend, 107 void *ctx, uint32_t init_log_level); 108 109 /** @brief Disable shell log backend. 110 * 111 * @param backend Shell log backend instance. 112 */ 113 void z_shell_log_backend_disable(const struct shell_log_backend *backend); 114 115 /** @brief Trigger processing of one log entry. 116 * 117 * @param backend Shell log backend instance. 118 * 119 * @return True if message was processed, false if FIFO was empty 120 */ 121 bool z_shell_log_backend_process(const struct shell_log_backend *backend); 122 123 #ifdef __cplusplus 124 } 125 #endif 126 127 #endif /* SHELL_LOG_BACKEND_H__ */ 128