Skip to content

Commit

Permalink
fix: try to log the full spdk msg
Browse files Browse the repository at this point in the history
Signed-off-by: Ana Hobden <[email protected]>
  • Loading branch information
Hoverbear committed Feb 23, 2021
1 parent 05e1af4 commit 3eaf6ce
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions spdk-sys/logwrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,26 @@ void
maya_log(int level, const char *file, const int line, const char *func,
const char *format, va_list args)
{
char buf[1024] = {0};
unsigned int would_have_written = vsnprintf(buf, sizeof(buf), format, args);
logfn(level, file, line, func, &buf[0], ((would_have_written > sizeof(buf)) ? sizeof(buf) : would_have_written));
// There is a delicate balance here! This `buf` ideally should not be resized, since a realloc is expensive.
char buf[4096] = {0};
unsigned int should_have_written = vsnprintf(buf, sizeof(buf), format, args);

if (should_have_written > sizeof(buf)) {
logfn(level, file, line, func, &buf[0], sizeof(buf));
} else {
// If `should_have_written` is bigger than `buf`, then the message is too long.
// Instead, we'll try to malloc onto the heap and log with that instead.
char *dynamic_buf = malloc(should_have_written);
if (!dynamic_buf) {
// We are out of memory. Trying to allocate more is not going to work out ok.
// Since C strings need `\0` on the end, we'll do that.
buf[sizeof(buf) - 1] = '\0';
logfn(level, file, line, func, &buf[0], sizeof(buf));
} else {
vsnprintf(dynamic_buf, should_have_written, format, args);
logfn(level, file, line, func, &dynamic_buf[0], sizeof(dynamic_buf));
free(dynamic_buf);
}
}
}

0 comments on commit 3eaf6ce

Please sign in to comment.