Jeff Muizelaar 702bd6eeec Bug 1680402. Use stderr in printf_stderr instead of reopening fd 2. r=glandium
Currently, printf_stderr doesn't show up when running with ./mach run.
This is because we run with -attach-console and that redirects stderr
to a different file descriptor using freopen in UseParentConsole.

The change from just using stderr directly happened in bug 340443 and was done
to avoid some linking issues. That problem doesn't seem to apply anymore so you'd
expect we'd be able to go back to the straightforward implemention that works even
if stderr has been redirected. Unfortunately, Windows takes not buffering
stderr very seriously and fprintf will write out the results character
by character. This can cause log output lines to be intermixed which
breaks log parsing in CI. We keep using fdopen to create a new FILE*
that's buffered but instead of hard coding fd 2, we get the actual fd
that corresponds to stderr using fileno.

The mozglue implementation was cargo culted from xpcom, so we update it
as well.

Differential Revision: https://phabricator.services.mozilla.com/D98550
2021-03-16 20:45:21 +00:00

69 lines
1.8 KiB
C

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
#ifndef mozilla_glue_Debug_h
#define mozilla_glue_Debug_h
/* This header file intends to supply debugging utilities for use in code
* that cannot use XPCOM debugging facilities like nsDebug.h.
* e.g. mozglue, browser/app
*
* NB: printf_stderr() is in the global namespace, so include this file with
* care; avoid including from header files.
*/
#include <io.h>
#if defined(XP_WIN)
# include <windows.h>
#endif // defined(XP_WIN)
#include "mozilla/Attributes.h"
#include "mozilla/Sprintf.h"
#if defined(MOZILLA_INTERNAL_API)
# error Do not include this file from XUL sources.
#endif
// Though this is a separate implementation than nsDebug's, we want to make the
// declarations compatible to avoid confusing the linker if both headers are
// included.
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
inline void printf_stderr(const char* fmt, ...) MOZ_FORMAT_PRINTF(1, 2) {
#if defined(XP_WIN)
if (IsDebuggerPresent()) {
char buf[2048];
va_list args;
va_start(args, fmt);
VsprintfLiteral(buf, fmt, args);
va_end(args);
OutputDebugStringA(buf);
}
#endif // defined(XP_WIN)
// 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;
va_list args;
va_start(args, fmt);
vfprintf(fp, fmt, args);
va_end(args);
fclose(fp);
}
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // mozilla_glue_Debug_h