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> // for open, O_WRONLY
15 #include <stdint.h> // for uint64_t
16 #include <stdio.h> // for fileno, fprintf, printf, stdout
17
18 #ifdef _MSC_VER
19 #include "msvc-posix.h"
20 #else
21 #include <sys/time.h>
22 #include <time.h> // for localtime, tm, time_t
23 #include <unistd.h> // for dup2, close, dup
24 #endif
25
26
27 // TODO(jansene): Some external libraries (nibmle) still rely on these, so we cannot remove them yet.
28 #undef dprint
29 #undef dinfo
30 #undef derror
31 #undef dwarning
32
33 uint64_t android_verbose = 0;
34 LogSeverity android_log_severity = EMULATOR_LOG_INFO;
35
dprint(const char * format,...)36 void dprint(const char* format, ...) {
37 va_list args;
38 va_start(args, format);
39 fdprintfnv(stdout, 0, format, args);
40 va_end(args);
41 }
42
fdprintf(FILE * fp,const char * format,...)43 void fdprintf(FILE* fp, const char* format, ...) {
44 va_list args;
45 va_start(args, format);
46 fdprintfnv(fp, 0, format, args);
47 va_end(args);
48 }
49
fdprintfnv(FILE * fp,const char * lvl,const char * format,va_list args)50 void fdprintfnv(FILE* fp, const char* lvl, const char* format, va_list args) {
51 if (VERBOSE_CHECK(time)) {
52 struct timeval tv;
53 gettimeofday(&tv, 0);
54 time_t now = tv.tv_sec;
55 struct tm* time = localtime(&now);
56 fprintf(fp, "%02d:%02d:%02d.%05ld ", time->tm_hour, time->tm_min,
57 time->tm_sec, tv.tv_usec);
58 }
59 fprintf(fp, "emulator: ");
60 if (lvl) {
61 fprintf(fp, "%s", lvl);
62 }
63 vfprintf(fp, format, args);
64 fprintf(fp, "\n");
65 }
66
dprintn(const char * format,...)67 void dprintn(const char* format, ...) {
68 va_list args;
69 va_start(args, format);
70 vfprintf(stdout, format, args);
71 va_end(args);
72 }
73
dprintnv(const char * format,va_list args)74 void dprintnv(const char* format, va_list args) {
75 vfprintf(stdout, format, args);
76 }
dinfo(const char * format,...)77 void dinfo(const char* format, ...) {
78 va_list args;
79 va_start(args, format);
80 fdprintfnv(stdout, "INFO: ", format, args);
81 va_end(args);
82 }
83
dwarning(const char * format,...)84 void dwarning(const char* format, ...) {
85 va_list args;
86 va_start(args, format);
87 fdprintfnv(stdout, "WARNING: ", format, args);
88 va_end(args);
89 }
90
derror(const char * format,...)91 void derror(const char* format, ...) {
92 va_list args;
93 va_start(args, format);
94 fdprintfnv(stdout, "ERROR: ", format, args);
95 va_end(args);
96 }
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
stdio_disable(void)110 extern void stdio_disable(void) {
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
stdio_enable(void)125 extern void stdio_enable(void) {
126 if (--stdio_disable_count == 0) {
127 int out_fd, err_fd;
128 fflush(stdout);
129 out_fd = _fileno(stdout);
130 err_fd = _fileno(stderr);
131 _dup2(stdio_save_out_fd, out_fd);
132 _dup2(stdio_save_err_fd, err_fd);
133 _close(stdio_save_out_fd);
134 _close(stdio_save_err_fd);
135 }
136 }
137 #else
stdio_disable(void)138 extern void stdio_disable(void) {
139 if (++stdio_disable_count == 1) {
140 int null_fd, out_fd, err_fd;
141 fflush(stdout);
142 out_fd = fileno(stdout);
143 err_fd = fileno(stderr);
144 stdio_save_out_fd = dup(out_fd);
145 stdio_save_err_fd = dup(err_fd);
146 null_fd = open("/dev/null", O_WRONLY);
147 dup2(null_fd, out_fd);
148 dup2(null_fd, err_fd);
149 close(null_fd);
150 }
151 }
152
stdio_enable(void)153 extern void stdio_enable(void) {
154 if (--stdio_disable_count == 0) {
155 int out_fd, err_fd;
156 fflush(stdout);
157 out_fd = fileno(stdout);
158 err_fd = fileno(stderr);
159 dup2(stdio_save_out_fd, out_fd);
160 dup2(stdio_save_err_fd, err_fd);
161 close(stdio_save_out_fd);
162 close(stdio_save_err_fd);
163 }
164 }
165 #endif
166