aboutsummaryrefslogtreecommitdiffstats
path: root/android/utils/debug.c
blob: 32936d221d15b9588b32d72a3d9f599d867b16b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/* Copyright (C) 2007-2008 The Android Open Source Project
**
** This software is licensed under the terms of the GNU General Public
** License version 2, as published by the Free Software Foundation, and
** may be copied, distributed, and modified under those terms.
**
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
*/
#include "android/utils/debug.h"
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

void
dprint( const char*  format,  ... )
{
    va_list  args;
    va_start( args, format );
    fprintf( stdout, "emulator: ");
    vfprintf( stdout, format, args );
    fprintf( stdout, "\n" );
    va_end( args );
}

void
dprintn( const char*  format, ... )
{
    va_list  args;
    va_start( args, format );
    vfprintf( stdout, format, args );
    va_end( args );
}

void
dprintnv( const char*  format, va_list args )
{
    vfprintf( stdout, format, args );
}


void
dwarning( const char*  format, ... )
{
    va_list  args;
    va_start( args, format );
    dprintn( "emulator: WARNING: " );
    dprintnv( format, args );
    dprintn( "\n" );
    va_end( args );
}


void
derror( const char*  format, ... )
{
    va_list  args;
    va_start( args, format );
    dprintn( "emulator: ERROR: " );
    dprintnv( format, args );
    dprintn( "\n" );
    va_end( args );
}

/** STDOUT/STDERR REDIRECTION
 **
 ** allows you to shut temporarily shutdown stdout/stderr
 ** this is useful to get rid of debug messages from ALSA and esd
 ** on Linux.
 **/
static int    stdio_disable_count;
static int    stdio_save_out_fd;
static int    stdio_save_err_fd;

#ifdef _WIN32
extern void
stdio_disable( void )
{
    if (++stdio_disable_count == 1) {
        int  null_fd, out_fd, err_fd;
        fflush(stdout);
        out_fd = _fileno(stdout);
        err_fd = _fileno(stderr);
        stdio_save_out_fd = _dup(out_fd);
        stdio_save_err_fd = _dup(err_fd);
        null_fd = _open( "NUL", _O_WRONLY );
        _dup2(null_fd, out_fd);
        _dup2(null_fd, err_fd);
        close(null_fd);
    }
}

extern void
stdio_enable( void )
{
    if (--stdio_disable_count == 0) {
        int  out_fd, err_fd;
        fflush(stdout);
        out_fd = _fileno(stdout);
        err_fd = _fileno(stderr);
        _dup2(stdio_save_out_fd, out_fd);
        _dup2(stdio_save_err_fd, err_fd);
        _close(stdio_save_out_fd);
        _close(stdio_save_err_fd);
    }
}
#else
extern void
stdio_disable( void )
{
    if (++stdio_disable_count == 1) {
        int  null_fd, out_fd, err_fd;
        fflush(stdout);
        out_fd = fileno(stdout);
        err_fd = fileno(stderr);
        stdio_save_out_fd = dup(out_fd);
        stdio_save_err_fd = dup(err_fd);
        null_fd = open( "/dev/null", O_WRONLY );
        dup2(null_fd, out_fd);
        dup2(null_fd, err_fd);
        close(null_fd);
    }
}

extern void
stdio_enable( void )
{
    if (--stdio_disable_count == 0) {
        int  out_fd, err_fd;
        fflush(stdout);
        out_fd = fileno(stdout);
        err_fd = fileno(stderr);
        dup2(stdio_save_out_fd, out_fd);
        dup2(stdio_save_err_fd, err_fd);
        close(stdio_save_out_fd);
        close(stdio_save_err_fd);
    }
}
#endif