mozglue/misc/Debug.h
author Emilio Cobos Álvarez <emilio@crisal.io>
Tue, 02 Jul 2019 18:50:04 +0000
changeset 480995 6525ac41c35455512bc1b681df331a5b74cb75cc
parent 472056 e1993a1f09ac53cd1a04fdf6a87f8cad8e44f73e
permissions -rw-r--r--
Bug 1562789 - SmallPointerArray should support moves, and have an IsEmpty() helper. r=froydnj This also implicitly deletes its copy-assignment operator and copy-constructor, which is great since it's a huge footgun. Differential Revision: https://phabricator.services.mozilla.com/D36549

/* -*- 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)

  FILE* fp = _fdopen(_dup(2), "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