Home | History | Annotate | Download | only in utils
      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 unsigned long android_verbose;
     18 
     19 void
     20 dprint( const char*  format,  ... )
     21 {
     22     va_list  args;
     23     va_start( args, format );
     24     fprintf( stdout, "emulator: ");
     25     vfprintf( stdout, format, args );
     26     fprintf( stdout, "\n" );
     27     va_end( args );
     28 }
     29 
     30 void
     31 dprintn( const char*  format, ... )
     32 {
     33     va_list  args;
     34     va_start( args, format );
     35     vfprintf( stdout, format, args );
     36     va_end( args );
     37 }
     38 
     39 void
     40 dprintnv( const char*  format, va_list args )
     41 {
     42     vfprintf( stdout, format, args );
     43 }
     44 
     45 
     46 void
     47 dwarning( const char*  format, ... )
     48 {
     49     va_list  args;
     50     va_start( args, format );
     51     dprintn( "emulator: WARNING: " );
     52     dprintnv( format, args );
     53     dprintn( "\n" );
     54     va_end( args );
     55 }
     56 
     57 
     58 void
     59 derror( const char*  format, ... )
     60 {
     61     va_list  args;
     62     va_start( args, format );
     63     dprintn( "emulator: ERROR: " );
     64     dprintnv( format, args );
     65     dprintn( "\n" );
     66     va_end( args );
     67 }
     68 
     69 /** STDOUT/STDERR REDIRECTION
     70  **
     71  ** allows you to shut temporarily shutdown stdout/stderr
     72  ** this is useful to get rid of debug messages from ALSA and esd
     73  ** on Linux.
     74  **/
     75 static int    stdio_disable_count;
     76 static int    stdio_save_out_fd;
     77 static int    stdio_save_err_fd;
     78 
     79 #ifdef _WIN32
     80 extern void
     81 stdio_disable( void )
     82 {
     83     if (++stdio_disable_count == 1) {
     84         int  null_fd, out_fd, err_fd;
     85         fflush(stdout);
     86         out_fd = _fileno(stdout);
     87         err_fd = _fileno(stderr);
     88         stdio_save_out_fd = _dup(out_fd);
     89         stdio_save_err_fd = _dup(err_fd);
     90         null_fd = _open( "NUL", _O_WRONLY );
     91         _dup2(null_fd, out_fd);
     92         _dup2(null_fd, err_fd);
     93         close(null_fd);
     94     }
     95 }
     96 
     97 extern void
     98 stdio_enable( void )
     99 {
    100     if (--stdio_disable_count == 0) {
    101         int  out_fd, err_fd;
    102         fflush(stdout);
    103         out_fd = _fileno(stdout);
    104         err_fd = _fileno(stderr);
    105         _dup2(stdio_save_out_fd, out_fd);
    106         _dup2(stdio_save_err_fd, err_fd);
    107         _close(stdio_save_out_fd);
    108         _close(stdio_save_err_fd);
    109     }
    110 }
    111 #else
    112 extern void
    113 stdio_disable( void )
    114 {
    115     if (++stdio_disable_count == 1) {
    116         int  null_fd, out_fd, err_fd;
    117         fflush(stdout);
    118         out_fd = fileno(stdout);
    119         err_fd = fileno(stderr);
    120         stdio_save_out_fd = dup(out_fd);
    121         stdio_save_err_fd = dup(err_fd);
    122         null_fd = open( "/dev/null", O_WRONLY );
    123         dup2(null_fd, out_fd);
    124         dup2(null_fd, err_fd);
    125         close(null_fd);
    126     }
    127 }
    128 
    129 extern void
    130 stdio_enable( void )
    131 {
    132     if (--stdio_disable_count == 0) {
    133         int  out_fd, err_fd;
    134         fflush(stdout);
    135         out_fd = fileno(stdout);
    136         err_fd = fileno(stderr);
    137         dup2(stdio_save_out_fd, out_fd);
    138         dup2(stdio_save_err_fd, err_fd);
    139         close(stdio_save_out_fd);
    140         close(stdio_save_err_fd);
    141     }
    142 }
    143 #endif
    144