Bug 1790873 - Use stderr directly in printf_stderr rather than opening a separate file r=glandium,nika

Differential Revision: https://phabricator.services.mozilla.com/D166853
This commit is contained in:
Steve Fink 2024-01-11 02:09:30 +00:00
Родитель 86e7e76297
Коммит 938a635e60
1 изменённых файлов: 24 добавлений и 19 удалений

Просмотреть файл

@ -19,7 +19,6 @@
#ifdef XP_WIN
# include <io.h>
# include <windows.h>
# include "mozilla/LateWriteChecks.h"
# include "mozilla/UniquePtr.h"
#endif
@ -231,6 +230,27 @@ void NS_MakeRandomString(char* aBuf, int32_t aBufLen) {
#endif
#ifndef ANDROID
void vprintf_stderr_buffered(const char* aFmt, va_list aArgs) {
// Avoid interleaving by writing to an on-stack buffer and then writing in one
// go with fputs, as long as the output fits into the buffer.
char buffer[1024];
va_list argsCpy;
va_copy(argsCpy, aArgs);
int n = VsprintfLiteral(buffer, aFmt, aArgs);
if (n < sizeof(buffer)) {
fputs(buffer, stderr);
} else {
// Message too long for buffer. Just print it, not worrying about
// interleaving. (We could malloc, but the underlying write() syscall could
// get interleaved if the output is too big anyway.)
vfprintf(stderr, aFmt, argsCpy);
}
va_end(argsCpy);
fflush(stderr);
}
#endif
#if defined(XP_WIN)
void vprintf_stderr(const char* aFmt, va_list aArgs) {
if (IsDebuggerPresent()) {
@ -249,22 +269,7 @@ void vprintf_stderr(const char* aFmt, va_list aArgs) {
}
}
// stderr is unbuffered by default so we open a new FILE (which is buffered)
// so that calls to printf_stderr are not as likely to get mixed together.
int fd = _fileno(stderr);
if (fd == -2) {
return;
}
FILE* fp = _fdopen(_dup(fd), "a");
if (!fp) {
return;
}
vfprintf(fp, aFmt, aArgs);
AutoSuspendLateWriteChecks suspend;
fclose(fp);
vprintf_stderr_buffered(aFmt, aArgs);
}
#elif defined(ANDROID)
@ -277,12 +282,12 @@ void vprintf_stderr(const char* aFmt, va_list aArgs) {
auto msgbuf = mozilla::Vsmprintf(aFmt, aArgs);
nyx_puts(msgbuf.get());
} else {
vfprintf(stderr, aFmt, aArgs);
vprintf_stderr_buffered(aFmt, aArgs);
}
}
#else
void vprintf_stderr(const char* aFmt, va_list aArgs) {
vfprintf(stderr, aFmt, aArgs);
vprintf_stderr_buffered(aFmt, aArgs);
}
#endif