Backed out 3 changesets (bug 1870106, bug 1845276) for causing doc generate failures...
[gecko.git] / widget / gtk / GfxInfoUtils.h
blobf4d96604e3a7035fad1e78094e9a7dac21909b53
1 /* vim: se cin sw=2 ts=2 et : */
2 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
4 * This Source Code Form is subject to the terms of the Mozilla Public
5 * License, v. 2.0. If a copy of the MPL was not distributed with this
6 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #ifndef WIDGET_GTK_GFXINFO_UTILS_h__
9 #define WIDGET_GTK_GFXINFO_UTILS_h__
11 // An alternative to mozilla::Unused for use in (a) C code and (b) code where
12 // linking with unused.o is difficult.
13 #define MOZ_UNUSED(expr) \
14 do { \
15 if (expr) { \
16 (void)0; \
17 } \
18 } while (0)
20 #define LOG_PIPE 2
22 static bool enable_logging = false;
23 static void log(const char* format, ...) {
24 if (!enable_logging) {
25 return;
27 va_list args;
28 va_start(args, format);
29 vfprintf(stderr, format, args);
30 va_end(args);
33 static int output_pipe = 1;
34 static void close_logging() {
35 // we want to redirect to /dev/null stdout, stderr, and while we're at it,
36 // any PR logging file descriptors. To that effect, we redirect all positive
37 // file descriptors up to what open() returns here. In particular, 1 is stdout
38 // and 2 is stderr.
39 int fd = open("/dev/null", O_WRONLY);
40 for (int i = 1; i < fd; i++) {
41 if (output_pipe != i) {
42 dup2(fd, i);
45 close(fd);
48 // C++ standard collides with C standard in that it doesn't allow casting void*
49 // to function pointer types. So the work-around is to convert first to size_t.
50 // http://www.trilithium.com/johan/2004/12/problem-with-dlsym/
51 template <typename func_ptr_type>
52 static func_ptr_type cast(void* ptr) {
53 return reinterpret_cast<func_ptr_type>(reinterpret_cast<size_t>(ptr));
56 #define BUFFER_SIZE_STEP 4000
58 static char* test_buf = nullptr;
59 static int test_bufsize = 0;
60 static int test_length = 0;
62 static void record_value(const char* format, ...) {
63 if (!test_buf || test_length + BUFFER_SIZE_STEP / 2 > test_bufsize) {
64 test_bufsize += BUFFER_SIZE_STEP;
65 test_buf = (char*)realloc(test_buf, test_bufsize);
67 int remaining = test_bufsize - test_length;
69 // Append the new values to the buffer, not to exceed the remaining space.
70 va_list args;
71 va_start(args, format);
72 int max_added = vsnprintf(test_buf + test_length, remaining, format, args);
73 va_end(args);
75 if (max_added >= remaining) {
76 test_length += remaining;
77 } else {
78 test_length += max_added;
82 #define record_error(str_, ...) record_value("ERROR\n" str_ "\n", ##__VA_ARGS__)
83 #define record_warning(str_, ...) \
84 record_value("WARNING\n" str_ "\n", ##__VA_ARGS__)
86 static void record_flush() {
87 if (!test_buf) {
88 return;
90 MOZ_UNUSED(write(output_pipe, test_buf, test_length));
91 if (enable_logging) {
92 MOZ_UNUSED(write(LOG_PIPE, test_buf, test_length));
94 free(test_buf);
95 test_buf = nullptr;
98 #endif /* WIDGET_GTK_GFXINFO_h__ */