Skip to content

Commit

Permalink
logging: log_output: Move flushing and writing to the header
Browse files Browse the repository at this point in the history
Move log_output_flush and log_output_write (renamed internal
buffer_write() function) to the header as inline functions.
Those function are used by log_output_dict.c and there are cases
when log_output.c is not compiled in.

Signed-off-by: Krzysztof Chruściński <krzysztof.chruscinski@nordicsemi.no>
  • Loading branch information
nordic-krch authored and fabiobaltieri committed Oct 9, 2024
1 parent f4335d2 commit df683fd
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 46 deletions.
25 changes: 24 additions & 1 deletion include/zephyr/logging/log_output.h
Original file line number Diff line number Diff line change
Expand Up @@ -190,11 +190,34 @@ void log_output_msg_syst_process(const struct log_output *log_output,
*/
void log_output_dropped_process(const struct log_output *output, uint32_t cnt);

/** @brief Write to the output buffer.
*
* @param outf Output function.
* @param buf Buffer.
* @param len Buffer length.
* @param ctx Context passed to the %p outf.
*/
static inline void log_output_write(log_output_func_t outf, uint8_t *buf, size_t len, void *ctx)
{
int processed;

while (len != 0) {
processed = outf(buf, len, ctx);
len -= processed;
buf += processed;
}
}

/** @brief Flush output buffer.
*
* @param output Pointer to the log output instance.
*/
void log_output_flush(const struct log_output *output);
static inline void log_output_flush(const struct log_output *output)
{
log_output_write(output->func, output->buf, output->control_block->offset,
output->control_block->ctx);
output->control_block->offset = 0;
}

/** @brief Function for setting user context passed to the output function.
*
Expand Down
30 changes: 3 additions & 27 deletions subsys/logging/log_output.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,28 +150,6 @@ static int print_formatted(const struct log_output *output,
return length;
}

static void buffer_write(log_output_func_t outf, uint8_t *buf, size_t len,
void *ctx)
{
int processed;

while (len != 0) {
processed = outf(buf, len, ctx);
len -= processed;
buf += processed;
}
}


void log_output_flush(const struct log_output *output)
{
buffer_write(output->func, output->buf,
output->control_block->offset,
output->control_block->ctx);

output->control_block->offset = 0;
}

static inline bool is_leap_year(uint32_t year)
{
return (((year % 4 == 0) && (year % 100 != 0)) || (year % 400 == 0));
Expand Down Expand Up @@ -785,11 +763,9 @@ void log_output_dropped_process(const struct log_output *output, uint32_t cnt)
cnt = MIN(cnt, 9999);
len = snprintk(buf, sizeof(buf), "%d", cnt);

buffer_write(outf, (uint8_t *)prefix, sizeof(prefix) - 1,
output->control_block->ctx);
buffer_write(outf, buf, len, output->control_block->ctx);
buffer_write(outf, (uint8_t *)postfix, sizeof(postfix) - 1,
output->control_block->ctx);
log_output_write(outf, (uint8_t *)prefix, sizeof(prefix) - 1, output->control_block->ctx);
log_output_write(outf, buf, len, output->control_block->ctx);
log_output_write(outf, (uint8_t *)postfix, sizeof(postfix) - 1, output->control_block->ctx);
}

void log_output_timestamp_freq_set(uint32_t frequency)
Expand Down
24 changes: 6 additions & 18 deletions subsys/logging/log_output_dict.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,6 @@
#include <zephyr/sys/__assert.h>
#include <zephyr/sys/util.h>

static void buffer_write(log_output_func_t outf, uint8_t *buf, size_t len,
void *ctx)
{
int processed;

do {
processed = outf(buf, len, ctx);
len -= processed;
buf += processed;
} while (len != 0);
}

void log_dict_output_msg_process(const struct log_output *output,
struct log_msg *msg, uint32_t flags)
{
Expand All @@ -40,19 +28,19 @@ void log_dict_output_msg_process(const struct log_output *output,

output_hdr.source = (source != NULL) ? log_source_id(source) : 0U;

buffer_write(output->func, (uint8_t *)&output_hdr, sizeof(output_hdr),
(void *)output->control_block->ctx);
log_output_write(output->func, (uint8_t *)&output_hdr, sizeof(output_hdr),
(void *)output->control_block->ctx);

size_t len;
uint8_t *data = log_msg_get_package(msg, &len);

if (len > 0U) {
buffer_write(output->func, data, len, (void *)output->control_block->ctx);
log_output_write(output->func, data, len, (void *)output->control_block->ctx);
}

data = log_msg_get_data(msg, &len);
if (len > 0U) {
buffer_write(output->func, data, len, (void *)output->control_block->ctx);
log_output_write(output->func, data, len, (void *)output->control_block->ctx);
}

log_output_flush(output);
Expand All @@ -65,6 +53,6 @@ void log_dict_output_dropped_process(const struct log_output *output, uint32_t c
msg.type = MSG_DROPPED_MSG;
msg.num_dropped_messages = MIN(cnt, 9999);

buffer_write(output->func, (uint8_t *)&msg, sizeof(msg),
(void *)output->control_block->ctx);
log_output_write(output->func, (uint8_t *)&msg, sizeof(msg),
(void *)output->control_block->ctx);
}

0 comments on commit df683fd

Please sign in to comment.