logging: Refactor log_output module

Module refactored: splitted data into read-only part and control block,
adding macro for creating log_output instance

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
This commit is contained in:
Krzysztof Chruscinski 2018-08-01 14:57:41 +02:00 committed by Carles Cufí
commit 1fbea945f4
3 changed files with 155 additions and 91 deletions

View file

@ -36,30 +36,70 @@ extern "C" {
*
* @return Number of bytes processed.
*/
typedef int (*log_output_func_t)(u8_t *data, size_t length, void *ctx);
typedef int (*log_output_func_t)(u8_t *buf, size_t size, void *ctx);
struct log_output_ctx {
log_output_func_t func;
u8_t *data;
size_t length;
/* @brief Control block structure for log_output instance. */
struct log_output_control_block {
size_t offset;
void *ctx;
};
/** @brief Function for processing log messages to readable strings.
/** @brief Log_output instance structure. */
struct log_output {
log_output_func_t func;
struct log_output_control_block *control_block;
u8_t *buf;
size_t size;
};
/** @brief Create log_output instance.
*
* @param _name Instance name.
* @param _func Function for processing output data.
* @param _buf Pointer to the output buffer.
* @param _size Size of the output buffer.
*/
#define LOG_OUTPUT_DEFINE(_name, _func, _buf, _size) \
static struct log_output_control_block _name##_control_block; \
static const struct log_output _name = { \
.func = _func, \
.control_block = &_name##_control_block, \
.buf = _buf, \
.size = _size, \
}
/** @brief Process log messages to readable strings.
*
* Function is using provided context with the buffer and output function to
* process formatted string and output the data.
*
* @param log_output Pointer to the log output instance.
* @param msg Log message.
* @param ctx Context.
* @param flags Optional flags.
*/
void log_output_msg_process(struct log_msg *msg,
struct log_output_ctx *ctx,
void log_output_msg_process(const struct log_output *log_output,
struct log_msg *msg,
u32_t flags);
/** @brief Function for setting timestamp frequency.
/** @brief Flush output buffer.
*
* @param log_output Pointer to the log output instance.
*/
void log_output_flush(const struct log_output *log_output);
/** @brief Function for setting user context passed to the output function.
*
* @param log_output Pointer to the log output instance.
* @param ctx User context.
*/
static inline void log_output_ctx_set(const struct log_output *log_output,
void *ctx)
{
log_output->control_block->ctx = ctx;
}
/** @brief Set timestamp frequency.
*
* @param freq Frequency in Hz.
*/