1 /* Copyright (C) 2007-2008 The Android Open Source Project
2 **
3 ** This software is licensed under the terms of the GNU General Public
4 ** License version 2, as published by the Free Software Foundation, and
5 ** may be copied, distributed, and modified under those terms.
6 **
7 ** This program is distributed in the hope that it will be useful,
8 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
9 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 ** GNU General Public License for more details.
11 */
12 #include "android/utils/debug.h"
13 #include <stdio.h>
14 #include <unistd.h>
15 #include <fcntl.h>
16
17 void
dprint(const char * format,...)18 dprint( const char* format, ... )
19 {
20 va_list args;
21 va_start( args, format );
22 fprintf( stdout, "emulator: ");
23 vfprintf( stdout, format, args );
24 fprintf( stdout, "\n" );
25 va_end( args );
26 }
27
28 void
dprintn(const char * format,...)29 dprintn( const char* format, ... )
30 {
31 va_list args;
32 va_start( args, format );
33 vfprintf( stdout, format, args );
34 va_end( args );
35 }
36
37 void
dprintnv(const char * format,va_list args)38 dprintnv( const char* format, va_list args )
39 {
40 vfprintf( stdout, format, args );
41 }
42
43
44 void
dwarning(const char * format,...)45 dwarning( const char* format, ... )
46 {
47 va_list args;
48 va_start( args, format );
49 dprintn( "emulator: WARNING: " );
50 dprintnv( format, args );
51 dprintn( "\n" );
52 va_end( args );
53 }
54
55
56 void
derror(const char * format,...)57 derror( const char* format, ... )
58 {
59 va_list args;
60 va_start( args, format );
61 dprintn( "emulator: ERROR: " );
62 dprintnv( format, args );
63 dprintn( "\n" );
64 va_end( args );
65 }
66
67 /** STDOUT/STDERR REDIRECTION
68 **
69 ** allows you to shut temporarily shutdown stdout/stderr
70 ** this is useful to get rid of debug messages from ALSA and esd
71 ** on Linux.
72 **/
73 static int stdio_disable_count;
74 static int stdio_save_out_fd;
75 static int stdio_save_err_fd;
76
77 #ifdef _WIN32
78 extern void
stdio_disable(void)79 stdio_disable( void )
80 {
81 if (++stdio_disable_count == 1) {
82 int null_fd, out_fd, err_fd;
83 fflush(stdout);
84 out_fd = _fileno(stdout);
85 err_fd = _fileno(stderr);
86 stdio_save_out_fd = _dup(out_fd);
87 stdio_save_err_fd = _dup(err_fd);
88 null_fd = _open( "NUL", _O_WRONLY );
89 _dup2(null_fd, out_fd);
90 _dup2(null_fd, err_fd);
91 close(null_fd);
92 }
93 }
94
95 extern void
stdio_enable(void)96 stdio_enable( void )
97 {
98 if (--stdio_disable_count == 0) {
99 int out_fd, err_fd;
100 fflush(stdout);
101 out_fd = _fileno(stdout);
102 err_fd = _fileno(stderr);
103 _dup2(stdio_save_out_fd, out_fd);
104 _dup2(stdio_save_err_fd, err_fd);
105 _close(stdio_save_out_fd);
106 _close(stdio_save_err_fd);
107 }
108 }
109 #else
110 extern void
stdio_disable(void)111 stdio_disable( void )
112 {
113 if (++stdio_disable_count == 1) {
114 int null_fd, out_fd, err_fd;
115 fflush(stdout);
116 out_fd = fileno(stdout);
117 err_fd = fileno(stderr);
118 stdio_save_out_fd = dup(out_fd);
119 stdio_save_err_fd = dup(err_fd);
120 null_fd = open( "/dev/null", O_WRONLY );
121 dup2(null_fd, out_fd);
122 dup2(null_fd, err_fd);
123 close(null_fd);
124 }
125 }
126
127 extern void
stdio_enable(void)128 stdio_enable( void )
129 {
130 if (--stdio_disable_count == 0) {
131 int out_fd, err_fd;
132 fflush(stdout);
133 out_fd = fileno(stdout);
134 err_fd = fileno(stderr);
135 dup2(stdio_save_out_fd, out_fd);
136 dup2(stdio_save_err_fd, err_fd);
137 close(stdio_save_out_fd);
138 close(stdio_save_err_fd);
139 }
140 }
141 #endif
142