• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2007-2008 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 #include "android/utils/debug.h"
15 
16 #include <fcntl.h>
17 #include <pthread.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <sys/syscall.h>
21 #include <sys/types.h>
22 #include <unistd.h>
23 
24 uint64_t android_verbose = 0;
25 
26 void
dprint(const char * format,...)27 dprint( const char*  format,  ... )
28 {
29     va_list  args;
30     va_start( args, format );
31     fprintf( stdout, "emulator: ");
32     vfprintf( stdout, format, args );
33     fprintf( stdout, "\n" );
34     va_end( args );
35 }
36 
37 
38 void
dprintn(const char * format,...)39 dprintn( const char*  format, ... )
40 {
41     va_list  args;
42     va_start( args, format );
43     vfprintf( stdout, format, args );
44     va_end( args );
45 }
46 
47 void
dprintnv(const char * format,va_list args)48 dprintnv( const char*  format, va_list args )
49 {
50     vfprintf( stdout, format, args );
51 }
52 
53 
54 void
dwarning(const char * format,...)55 dwarning( const char*  format, ... )
56 {
57     va_list  args;
58     va_start( args, format );
59     dprintn( "emulator: WARNING: " );
60     dprintnv( format, args );
61     dprintn( "\n" );
62     va_end( args );
63 }
64 
65 
66 void
derror(const char * format,...)67 derror( const char*  format, ... )
68 {
69     va_list  args;
70     va_start( args, format );
71     dprintn( "emulator: ERROR: " );
72     dprintnv( format, args );
73     dprintn( "\n" );
74     va_end( args );
75 }
76 
77 void
android_tid_function_print(bool use_emulator_prefix,const char * function,const char * format,...)78 android_tid_function_print(
79         bool use_emulator_prefix,
80         const char* function,
81         const char* format, ... )
82 {
83     int tid = syscall(SYS_gettid);
84     va_list args;
85     va_start(args, format);
86     const char* prefix = use_emulator_prefix ? "emulator: " : "";
87     if (function) {
88         printf("%stid=0x%x: %s: ", prefix, tid,
89                function);
90     } else {
91         printf("%stid=0x%x: ", prefix, tid);
92     }
93 
94     vprintf(format, args);
95     printf("\n");
96     va_end(args);
97 }
98 
99 /** STDOUT/STDERR REDIRECTION
100  **
101  ** allows you to shut temporarily shutdown stdout/stderr
102  ** this is useful to get rid of debug messages from ALSA and esd
103  ** on Linux.
104  **/
105 static int    stdio_disable_count;
106 static int    stdio_save_out_fd;
107 static int    stdio_save_err_fd;
108 
109 #ifdef _WIN32
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( "NUL", _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 #else
142 extern void
stdio_disable(void)143 stdio_disable( void )
144 {
145     if (++stdio_disable_count == 1) {
146         int  null_fd, out_fd, err_fd;
147         fflush(stdout);
148         out_fd = fileno(stdout);
149         err_fd = fileno(stderr);
150         stdio_save_out_fd = dup(out_fd);
151         stdio_save_err_fd = dup(err_fd);
152         null_fd = open( "/dev/null", O_WRONLY );
153         dup2(null_fd, out_fd);
154         dup2(null_fd, err_fd);
155         close(null_fd);
156     }
157 }
158 
159 extern void
stdio_enable(void)160 stdio_enable( void )
161 {
162     if (--stdio_disable_count == 0) {
163         int  out_fd, err_fd;
164         fflush(stdout);
165         out_fd = fileno(stdout);
166         err_fd = fileno(stderr);
167         dup2(stdio_save_out_fd, out_fd);
168         dup2(stdio_save_err_fd, err_fd);
169         close(stdio_save_out_fd);
170         close(stdio_save_err_fd);
171     }
172 }
173 #endif
174