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
14 #include <fcntl.h>
15 #include <pthread.h>
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <sys/syscall.h>
19 #include <sys/types.h>
20 #include <unistd.h>
21
22 uint64_t android_verbose = 0;
23
24 void
dprint(const char * format,...)25 dprint( const char* format, ... )
26 {
27 va_list args;
28 va_start( args, format );
29 fprintf( stdout, "emulator: ");
30 vfprintf( stdout, format, args );
31 fprintf( stdout, "\n" );
32 va_end( args );
33 }
34
35
36 void
dprintn(const char * format,...)37 dprintn( const char* format, ... )
38 {
39 va_list args;
40 va_start( args, format );
41 vfprintf( stdout, format, args );
42 va_end( args );
43 }
44
45 void
dprintnv(const char * format,va_list args)46 dprintnv( const char* format, va_list args )
47 {
48 vfprintf( stdout, format, args );
49 }
50
51
52 void
dwarning(const char * format,...)53 dwarning( const char* format, ... )
54 {
55 va_list args;
56 va_start( args, format );
57 dprintn( "emulator: WARNING: " );
58 dprintnv( format, args );
59 dprintn( "\n" );
60 va_end( args );
61 }
62
63
64 void
derror(const char * format,...)65 derror( const char* format, ... )
66 {
67 va_list args;
68 va_start( args, format );
69 dprintn( "emulator: ERROR: " );
70 dprintnv( format, args );
71 dprintn( "\n" );
72 va_end( args );
73 }
74
75 void
android_tid_function_print(bool use_emulator_prefix,const char * function,const char * format,...)76 android_tid_function_print(
77 bool use_emulator_prefix,
78 const char* function,
79 const char* format, ... )
80 {
81 int tid = syscall(SYS_gettid);
82 va_list args;
83 va_start(args, format);
84 const char* prefix = use_emulator_prefix ? "emulator: " : "";
85 if (function) {
86 printf("%stid=0x%x: %s: ", prefix, tid,
87 function);
88 } else {
89 printf("%stid=0x%x: ", prefix, tid);
90 }
91
92 vprintf(format, args);
93 printf("\n");
94 va_end(args);
95 }
96
97 /** STDOUT/STDERR REDIRECTION
98 **
99 ** allows you to shut temporarily shutdown stdout/stderr
100 ** this is useful to get rid of debug messages from ALSA and esd
101 ** on Linux.
102 **/
103 static int stdio_disable_count;
104 static int stdio_save_out_fd;
105 static int stdio_save_err_fd;
106
107 #ifdef _WIN32
108 extern void
stdio_disable(void)109 stdio_disable( void )
110 {
111 if (++stdio_disable_count == 1) {
112 int null_fd, out_fd, err_fd;
113 fflush(stdout);
114 out_fd = _fileno(stdout);
115 err_fd = _fileno(stderr);
116 stdio_save_out_fd = _dup(out_fd);
117 stdio_save_err_fd = _dup(err_fd);
118 null_fd = _open( "NUL", _O_WRONLY );
119 _dup2(null_fd, out_fd);
120 _dup2(null_fd, err_fd);
121 close(null_fd);
122 }
123 }
124
125 extern void
stdio_enable(void)126 stdio_enable( void )
127 {
128 if (--stdio_disable_count == 0) {
129 int out_fd, err_fd;
130 fflush(stdout);
131 out_fd = _fileno(stdout);
132 err_fd = _fileno(stderr);
133 _dup2(stdio_save_out_fd, out_fd);
134 _dup2(stdio_save_err_fd, err_fd);
135 _close(stdio_save_out_fd);
136 _close(stdio_save_err_fd);
137 }
138 }
139 #else
140 extern void
stdio_disable(void)141 stdio_disable( void )
142 {
143 if (++stdio_disable_count == 1) {
144 int null_fd, out_fd, err_fd;
145 fflush(stdout);
146 out_fd = fileno(stdout);
147 err_fd = fileno(stderr);
148 stdio_save_out_fd = dup(out_fd);
149 stdio_save_err_fd = dup(err_fd);
150 null_fd = open( "/dev/null", O_WRONLY );
151 dup2(null_fd, out_fd);
152 dup2(null_fd, err_fd);
153 close(null_fd);
154 }
155 }
156
157 extern void
stdio_enable(void)158 stdio_enable( void )
159 {
160 if (--stdio_disable_count == 0) {
161 int out_fd, err_fd;
162 fflush(stdout);
163 out_fd = fileno(stdout);
164 err_fd = fileno(stderr);
165 dup2(stdio_save_out_fd, out_fd);
166 dup2(stdio_save_err_fd, err_fd);
167 close(stdio_save_out_fd);
168 close(stdio_save_err_fd);
169 }
170 }
171 #endif
172