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) \
22 static bool enable_logging
= false;
23 static void log(const char* format
, ...) {
24 if (!enable_logging
) {
28 va_start(args
, format
);
29 vfprintf(stderr
, format
, 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
39 int fd
= open("/dev/null", O_WRONLY
);
40 for (int i
= 1; i
< fd
; i
++) {
41 if (output_pipe
!= i
) {
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.
71 va_start(args
, format
);
72 int max_added
= vsnprintf(test_buf
+ test_length
, remaining
, format
, args
);
75 if (max_added
>= remaining
) {
76 test_length
+= remaining
;
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() {
90 MOZ_UNUSED(write(output_pipe
, test_buf
, test_length
));
92 MOZ_UNUSED(write(LOG_PIPE
, test_buf
, test_length
));
98 #endif /* WIDGET_GTK_GFXINFO_h__ */