Bug 1769628 [wpt PR 34081] - Update wpt metadata, a=testonly
[gecko.git] / mozglue / misc / Debug.h
blobf8f05c62990440f1c8d34a24e25690dfa9a3ecb9
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
7 #ifndef mozilla_glue_Debug_h
8 #define mozilla_glue_Debug_h
10 /* This header file intends to supply debugging utilities for use in code
11 * that cannot use XPCOM debugging facilities like nsDebug.h.
12 * e.g. mozglue, browser/app
14 * NB: printf_stderr() is in the global namespace, so include this file with
15 * care; avoid including from header files.
18 #include <io.h>
19 #if defined(XP_WIN)
20 # include <windows.h>
21 #endif // defined(XP_WIN)
22 #include "mozilla/Attributes.h"
23 #include "mozilla/Sprintf.h"
25 #if defined(MOZILLA_INTERNAL_API)
26 # error Do not include this file from XUL sources.
27 #endif
29 // Though this is a separate implementation than nsDebug's, we want to make the
30 // declarations compatible to avoid confusing the linker if both headers are
31 // included.
32 #ifdef __cplusplus
33 extern "C" {
34 #endif // __cplusplus
36 void printf_stderr(const char* fmt, ...) MOZ_FORMAT_PRINTF(1, 2);
37 inline void printf_stderr(const char* fmt, ...) {
38 #if defined(XP_WIN)
39 if (IsDebuggerPresent()) {
40 char buf[2048];
41 va_list args;
42 va_start(args, fmt);
43 VsprintfLiteral(buf, fmt, args);
44 va_end(args);
45 OutputDebugStringA(buf);
47 #endif // defined(XP_WIN)
49 // stderr is unbuffered by default so we open a new FILE (which is buffered)
50 // so that calls to printf_stderr are not as likely to get mixed together.
51 int fd = _fileno(stderr);
52 if (fd == -2) return;
54 FILE* fp = _fdopen(_dup(fd), "a");
55 if (!fp) return;
57 va_list args;
58 va_start(args, fmt);
59 vfprintf(fp, fmt, args);
60 va_end(args);
62 fclose(fp);
65 #ifdef __cplusplus
67 #endif // __cplusplus
69 #endif // mozilla_glue_Debug_h