Skip to content

Commit

Permalink
Optionally remove logging to stdout for embedded platforms
Browse files Browse the repository at this point in the history
  • Loading branch information
Sympatron committed Oct 30, 2024
1 parent 3780b77 commit 631583c
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,11 @@ logger_t default_logger = {
.flags = LOGGER_FLAG_NONE,
.log_level = LOG_INFO,
.name = "GLOBAL",
#if defined(NO_OS)
.puts = NULL,
#else
.puts = puts,
#endif
.root_path = NULL,
.cb = NULL,
};
Expand All @@ -59,6 +63,10 @@ static const char *log_level_names[LOG_MAX_LEVEL] = {

static inline void logger_log_set_color(logger_t *ctx, const char *color)
{
#if defined(NO_OS)
ARG_UNUSED(ctx);
ARG_UNUSED(color);
#else
size_t ret, len;

if (ctx->flags & LOGGER_FLAG_NO_COLORS)
Expand All @@ -70,6 +78,7 @@ static inline void logger_log_set_color(logger_t *ctx, const char *color)
assert(ret == len);
ARG_UNUSED(ret); /* squash warning in Release builds */
}
#endif
}

static const char *get_tstamp()
Expand Down Expand Up @@ -134,10 +143,17 @@ int __logger_log(logger_t *ctx, int log_level, const char *file, unsigned long l
ctx->cb(log_level, file, line, buf);
} else {
logger_log_set_color(ctx, log_level_colors[log_level]);
#if defined(NO_OS)
if (ctx->puts)
ctx->puts(buf);
else
return -1;
#else
if (ctx->file)
fputs(buf, ctx->file);
else
ctx->puts(buf);
#endif
logger_log_set_color(ctx, RESET);
}

Expand Down

0 comments on commit 631583c

Please sign in to comment.