Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 3 additions & 4 deletions include/SDL3/SDL_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,9 @@
* - Others: standard error output (stderr)
*
* You don't need to have a newline (`\n`) on the end of messages, the
* functions will do that for you. For consistent behavior cross-platform, you
* shouldn't have any newlines in messages, such as to log multiple lines in
* one call; unusual platform-specific behavior can be observed in such usage.
* Do one log call per line instead, with no newlines in messages.
* functions will do that for you. You can log multiple lines per message, with
* newlines separating the lines, in which case the multiple lines are logged
* together atomically.
*
* Each log call is atomic, so you won't see log messages cut off one another
* when logging from multiple threads.
Expand Down
40 changes: 31 additions & 9 deletions src/SDL_log.c
Original file line number Diff line number Diff line change
Expand Up @@ -640,17 +640,39 @@ void SDL_LogMessageV(int category, SDL_LogPriority priority, SDL_PRINTF_FORMAT_S
message = stack_buf;
}

// Chop off final endline.
if ((len > 0) && (message[len - 1] == '\n')) {
message[--len] = '\0';
if ((len > 0) && (message[len - 1] == '\r')) { // catch "\r\n", too.
message[--len] = '\0';
}
}

SDL_LockMutex(SDL_log_function_lock);
{
SDL_log_function(SDL_log_userdata, category, priority, message);
char * const end = &message[len];
char *c = message;
char *line = message;
char term = '\0';
// Log lines with terminating newlines
while (c < end) {
if ((*c == '\r') || (*c == '\n')) {
term = *c;
*c = '\0';
SDL_log_function(SDL_log_userdata, category, priority, line);
c++;
if (c >= end) {
break;
}
if ((term == '\r') && (*c == '\n')) {
c++;
if (c >= end) {
break;
}
}
line = c;
term = '\0';
} else {
c++;
}
}
if (term == '\0') {
// If there's no newline on the last line, it won't be logged in
// the above loop, so log it here
SDL_log_function(SDL_log_userdata, category, priority, line);
}
}
SDL_UnlockMutex(SDL_log_function_lock);

Expand Down
Loading