【完善】日志缓冲区溢出检查处理。

Signed-off-by: armink <armink.ztl@gmail.com>
pull/32/head
armink 7 years ago
parent 2dfba8ee3a
commit 8c957b1719

@ -1,7 +1,7 @@
/* /*
* This file is part of the EasyLogger Library. * This file is part of the EasyLogger Library.
* *
* Copyright (c) 2015-2017, Armink, <armink.ztl@gmail.com> * Copyright (c) 2015-2018, Armink, <armink.ztl@gmail.com>
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
@ -50,7 +50,7 @@ extern "C" {
#define ELOG_LVL_TOTAL_NUM 6 #define ELOG_LVL_TOTAL_NUM 6
/* EasyLogger software version number */ /* EasyLogger software version number */
#define ELOG_SW_VERSION "2.0.2" #define ELOG_SW_VERSION "2.0.3"
/* EasyLogger assert for developer. */ /* EasyLogger assert for developer. */
#ifdef ELOG_ASSERT_ENABLE #ifdef ELOG_ASSERT_ENABLE

@ -1,7 +1,7 @@
/* /*
* This file is part of the EasyLogger Library. * This file is part of the EasyLogger Library.
* *
* Copyright (c) 2015-2016, Armink, <armink.ztl@gmail.com> * Copyright (c) 2015-2018, Armink, <armink.ztl@gmail.com>
* *
* Permission is hereby granted, free of charge, to any person obtaining * Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the * a copy of this software and associated documentation files (the
@ -485,41 +485,52 @@ void elog_output(uint8_t level, const char *tag, const char *file, const char *f
log_len += elog_strcpy(log_len, log_buf + log_len, ")"); log_len += elog_strcpy(log_len, log_buf + log_len, ")");
} }
/* package other log data to buffer. '\0' must be added in the end by vsnprintf. */ /* package other log data to buffer. '\0' must be added in the end by vsnprintf. */
fmt_result = vsnprintf(log_buf + log_len, ELOG_LINE_BUF_SIZE - log_len - newline_len + 1, format, args); fmt_result = vsnprintf(log_buf + log_len, ELOG_LINE_BUF_SIZE - log_len, format, args);
va_end(args); va_end(args);
/* calculate log length */
if ((log_len + fmt_result <= ELOG_LINE_BUF_SIZE) && (fmt_result > -1)) {
log_len += fmt_result;
} else {
/* using max length */
log_len = ELOG_LINE_BUF_SIZE;
}
/* overflow check and reserve some space for CSI end sign and newline sign */
#ifdef ELOG_COLOR_ENABLE #ifdef ELOG_COLOR_ENABLE
/* add CSI end sign */ if (log_len + (sizeof(CSI_END) - 1) + newline_len > ELOG_LINE_BUF_SIZE) {
if (elog.text_color_enabled) { /* using max length */
log_len += elog_strcpy(log_len, log_buf + log_len + fmt_result, CSI_END); log_len = ELOG_LINE_BUF_SIZE;
/* reserve some space for CSI end sign */
log_len -= (sizeof(CSI_END) - 1);
#else
if (log_len + newline_len > ELOG_LINE_BUF_SIZE) {
/* using max length */
log_len = ELOG_LINE_BUF_SIZE;
#endif /* ELOG_COLOR_ENABLE */
/* reserve some space for newline sign */
log_len -= newline_len;
} }
#endif
/* keyword filter */ /* keyword filter */
if (elog.filter.keyword[0] != '\0') { if (elog.filter.keyword[0] != '\0') {
/* add string end sign */ /* add string end sign */
if (fmt_result > -1) log_buf[log_len] = '\0';
{ /* find the keyword */
log_buf[log_len + fmt_result] = '\0'; if (!strstr(log_buf, elog.filter.keyword)) {
}
if (!strstr(log_buf, elog.filter.keyword))
{
/* unlock output */ /* unlock output */
elog_output_unlock(); elog_output_unlock();
return; return;
} }
} }
/* package newline sign */
if ((fmt_result > -1) && (fmt_result + log_len + newline_len <= ELOG_LINE_BUF_SIZE)) { #ifdef ELOG_COLOR_ENABLE
log_len += fmt_result; /* add CSI end sign */
log_len += elog_strcpy(log_len, log_buf + log_len, ELOG_NEWLINE_SIGN); if (elog.text_color_enabled) {
} else { log_len += elog_strcpy(log_len, log_buf + log_len, CSI_END);
log_len = ELOG_LINE_BUF_SIZE;
/* copy newline sign */
strcpy(log_buf + ELOG_LINE_BUF_SIZE - (newline_len + 1), ELOG_NEWLINE_SIGN);
} }
#endif
/* package newline sign */
log_len += elog_strcpy(log_len, log_buf + log_len, ELOG_NEWLINE_SIGN);
/* output log */ /* output log */
#if defined(ELOG_ASYNC_OUTPUT_ENABLE) #if defined(ELOG_ASYNC_OUTPUT_ENABLE)
extern void elog_async_output(uint8_t level, const char *log, size_t size); extern void elog_async_output(uint8_t level, const char *log, size_t size);

Loading…
Cancel
Save