es40_debug.cpp

Go to the documentation of this file.
00001 /*  ES40 emulator.
00002  *
00003  *  This file is based upon GXemul.
00004  *
00005  *  Copyright (C) 2004-2007  Anders Gavare.  All rights reserved.
00006  *
00007  *  Redistribution and use in source and binary forms, with or without
00008  *  modification, are permitted provided that the following conditions are met:
00009  *
00010  *  1. Redistributions of source code must retain the above copyright
00011  *     notice, this list of conditions and the following disclaimer.
00012  *  2. Redistributions in binary form must reproduce the above copyright  
00013  *     notice, this list of conditions and the following disclaimer in the 
00014  *     documentation and/or other materials provided with the distribution.
00015  *  3. The name of the author may not be used to endorse or promote products
00016  *     derived from this software without specific prior written permission.
00017  *
00018  *  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00019  *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021  *  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE   
00022  *  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00023  *  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00024  *  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00025  *  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00026  *  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00027  *  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  *  SUCH DAMAGE.
00029  */
00030 
00050 /*****************************************************************************
00051  *
00052  *  NOTE:  debug(), fatal(), and debug_indentation() are not re-entrant.
00053  *         The global variable quiet_mode can be used to suppress the output
00054  *         of debug(), but not the output of fatal().
00055  *
00056  *****************************************************************************/
00057 #include "StdAfx.h"
00058 
00059 //int verbose = 0;
00060 int         quiet_mode = 0;
00061 
00062 static int  debug_indent = 0;
00063 static int  debug_currently_at_start_of_line = 1;
00064 
00065 /*
00066  *  va_debug():
00067  *
00068  *  Used internally by debug() and fatal().
00069  */
00070 static void va_debug(va_list argp, char* fmt)
00071 {
00072   char    buf[DEBUG_BUFSIZE + 1];
00073   char*   s;
00074   int     i;
00075 
00076   buf[0] = buf[DEBUG_BUFSIZE] = 0;
00077 
00078   //vsnprintf(buf, DEBUG_BUFSIZE, fmt, argp);
00079   sprintf(buf, fmt, argp);
00080 
00081   s = buf;
00082   while(*s)
00083   {
00084     if(debug_currently_at_start_of_line)
00085     {
00086       for(i = 0; i < debug_indent; i++)
00087         printf(" ");
00088     }
00089 
00090     printf("%c", *s);
00091 
00092     debug_currently_at_start_of_line = 0;
00093     if(*s == '\n' || *s == '\r')
00094       debug_currently_at_start_of_line = 1;
00095     s++;
00096   }
00097 }
00098 
00099 /*
00100  *  debug_indentation():
00101  *
00102  *  Modify the debug indentation.
00103  */
00104 void debug_indentation(int diff)
00105 {
00106   debug_indent += diff;
00107   if(debug_indent < 0)
00108     fprintf(stderr, "WARNING: debug_indent less than 0!\n");
00109 }
00110 
00111 /*
00112  *  debug():
00113  *
00114  *  Debug output (ignored if quiet_mode is set).
00115  */
00116 void debug(char* fmt, ...)
00117 {
00118   va_list argp;
00119 
00120   if(quiet_mode)
00121     return;
00122 
00123   va_start(argp, fmt);
00124   va_debug(argp, fmt);
00125   va_end(argp);
00126 }
00127 
00128 /*
00129  *  fatal():
00130  *
00131  *  Fatal works like debug(), but doesn't care about the quiet_mode
00132  *  setting.
00133  */
00134 void fatal(char* fmt, ...)
00135 {
00136   va_list argp;
00137 
00138   va_start(argp, fmt);
00139   va_debug(argp, fmt);
00140   va_end(argp);
00141 }

SourceForge.net Logo
Project space on SourceForge.net