Skip to content

Commit

Permalink
logger: Increase lenght and fix log line termination case
Browse files Browse the repository at this point in the history
Signed-off-by: Siddharth Chandrasekaran <[email protected]>
  • Loading branch information
sidcha committed Jan 6, 2024
1 parent 2ad7b54 commit 66e9e75
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions src/logger.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,25 @@ static const char *get_tstamp()
return time_buf;
}

#define LOG_BUF_LEN 128
static int terminate_log_line(char *buf, int len)
{
while (len && buf[--len] == '\0');

if (buf[len] != '\n')
buf[++len] = '\n';
buf[++len] = '\0';
return len;
}

#define LOG_BUF_LEN 192

__attribute__((format(printf, 5, 6)))
int __logger_log(logger_t *ctx, int log_level, const char *file, unsigned long line,
const char *fmt, ...)
{
int len = 0;
va_list args;
char buf[LOG_BUF_LEN + 2]; /* +2 for tailing '\n' and '\0' */
char buf[LOG_BUF_LEN + 2] = {0}; /* +2 for tailing '\n' and '\0' */

if (!ctx)
ctx = &default_logger;
Expand Down Expand Up @@ -125,9 +135,7 @@ int __logger_log(logger_t *ctx, int log_level, const char *file, unsigned long l
len = LOG_BUF_LEN;

/* If the log line doesn't already end with a new line, add it */
if(buf[len - 1] != '\n')
buf[len++] = '\n';
buf[len] = '\0';
len = terminate_log_line(buf, len);

if (ctx->cb) {
ctx->cb(log_level, get_rel_path(ctx, file), line, buf);
Expand Down

0 comments on commit 66e9e75

Please sign in to comment.