File: Debug.h

package info (click to toggle)
firefox 149.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,767,760 kB
  • sloc: cpp: 7,416,064; javascript: 6,752,859; ansic: 3,774,850; python: 1,250,473; xml: 641,578; asm: 439,191; java: 186,617; sh: 56,634; makefile: 18,856; objc: 13,092; perl: 12,763; pascal: 5,960; yacc: 4,583; cs: 3,846; lex: 1,720; ruby: 1,002; php: 436; lisp: 258; awk: 105; sql: 66; sed: 53; csh: 10; exp: 6
file content (93 lines) | stat: -rw-r--r-- 3,487 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
/* -*- 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

#include "mozilla/Attributes.h"  // For MOZ_FORMAT_PRINTF
#include "mozilla/Types.h"       // For MFBT_API
#include "fmt/format.h"

#include <cstdarg>
#include <sstream>

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

#ifdef __cplusplus
extern "C" {
#endif  // __cplusplus

/**
 * printf_stderr(...) is much like fprintf(stderr, ...), except that:
 *  - on Android and Firefox OS, *instead* of printing to stderr, it
 *    prints to logcat.  (Newlines in the string lead to multiple lines
 *    of logcat, but each function call implicitly completes a line even
 *    if the string does not end with a newline.)
 *  - on Windows, if a debugger is present, it calls OutputDebugString
 *    in *addition* to writing to stderr
 */
MFBT_API void printf_stderr(const char* aFmt, ...) MOZ_FORMAT_PRINTF(1, 2);

/**
 * Same as printf_stderr, but taking va_list instead of varargs
 */
MFBT_API void vprintf_stderr(const char* aFmt, va_list aArgs)
    MOZ_FORMAT_PRINTF(1, 0);

/**
 * fprintf_stderr is like fprintf, except that if its file argument
 * is stderr, it invokes printf_stderr instead.
 *
 * This is useful for general debugging code that logs information to a
 * file, but that you would like to be useful on Android and Firefox OS.
 * If you use fprintf_stderr instead of fprintf in such debugging code,
 * then callers can pass stderr to get logging that works on Android and
 * Firefox OS (and also the other side-effects of using printf_stderr).
 *
 * Code that is structured this way needs to be careful not to split a
 * line of output across multiple calls to fprintf_stderr, since doing
 * so will cause it to appear in multiple lines in logcat output.
 * (Producing multiple lines at once is fine.)
 */
MFBT_API void fprintf_stderr(FILE* aFile, const char* aFmt, ...)
    MOZ_FORMAT_PRINTF(2, 3);

#ifdef __cplusplus
}
#endif  // __cplusplus

#ifdef __cplusplus
/*
 * print_stderr and fprint_stderr are like printf_stderr and fprintf_stderr,
 * except the stringstream versions deal with Android logcat line length
 * limitations. They do this by printing individual lines out of the provided
 * stringstream using separate calls to logcat.
 */
MFBT_API void print_stderr(std::stringstream&);
MFBT_API void fprint_stderr(FILE*, std::stringstream&);
MFBT_API void print_stderr(const std::string&);
MFBT_API void fprint_stderr(FILE*, const std::string&);

template <typename... Args>
inline void print_stderr(fmt::format_string<std::type_identity_t<Args>...> aFmt,
                         Args&&... aArgs) {
  print_stderr(fmt::format(aFmt, std::forward<Args>(aArgs)...));
}
template <typename... Args>
inline void fprint_stderr(
    FILE* aFile, fmt::format_string<std::type_identity_t<Args>...> aFmt,
    Args&&... aArgs) {
  fprint_stderr(aFile, fmt::format(aFmt, std::forward<Args>(aArgs)...));
}
#endif

#endif  // mozilla_glue_Debug_h