Main Page | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Class Members | File Members

aflibDebug.cc

Go to the documentation of this file.
00001     /*
00002 
00003     Copyright (C) 2000 Stefan Westerfeld
00004                        stefan@space.twc.de
00005 
00006     This library is free software; you can redistribute it and/or
00007     modify it under the terms of the GNU Library General Public
00008     License as published by the Free Software Foundation; either
00009     version 2 of the License, or (at your option) any later version.
00010   
00011     This library is distributed in the hope that it will be useful,
00012     but WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     Library General Public License for more details.
00015    
00016     You should have received a copy of the GNU Library General Public License
00017     along with this library; see the file COPYING.LIB.  If not, write to
00018     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
00019     Boston, MA 02111-1307, USA.
00020 
00021     */
00022 
00023 #include "aflibDebug.h"
00024 #include <stdlib.h>
00025 #include <stdarg.h>
00026 #include <stdio.h>
00027 #include <string.h>
00028 
00029 static int aflib_debug_level = ::aflibDebug::lInfo;
00030 static bool aflib_debug_abort = false;
00031 static const char *aflib_debug_prefix = "";
00032 static char *messageAppName = 0;
00033 
00034 
00035 /*
00036 static char* status_strs[] =
00037 {
00038    "Success",
00039    "Error Open",
00040    "Unsupported",
00041    "AFLIB_ERROR_INITIALIZATION_FAILURE",
00042    "AFLIB_NOT_FOUND",
00043    "AFLIB_END_OF_FILE",
00044    "AFLIB_NO_DATA",
00045     0
00046 };
00047 
00048 
00049 static char* data_size_strs[] =   
00050 {
00051    "UNDEFINED",
00052    "8 bit signed",
00053    "8 bit unsigned",
00054    "16 bit signed",
00055    "16 bit unsigned",
00056    "32 bit signed",
00057    "32 bit unsigned",
00058     0
00059 };
00060 
00061 static char* data_endian_strs[] =
00062 {
00063    "UNDEFINED",
00064    "ENDIAN_LITTLE",
00065    "ENDIAN_BIG",
00066     0
00067 };
00068 */
00069 
00070 /*
00071  * Call the graphical application to display a message, if
00072  * defined. Otherwise, send to standard error. Debug messages are
00073  * always sent to standard error because they tend to be very verbose.
00074  * Note that the external application is run in the background to
00075  * avoid blocking the sound server.
00076  */
00077 void output_message(::aflibDebug::Level level, const char *msg) {
00078    char buff[1024];
00079 
00080    /* default to text output if no message app is defined or if it is a debug message. */
00081     if (messageAppName == 0 || !strcmp(messageAppName, "") || (level == ::aflibDebug::lDebug))
00082     {
00083         fprintf(stderr, "%s\n", msg);
00084         return;
00085     }
00086 
00087     switch (level) {
00088         case ::aflibDebug::lFatal:
00089           sprintf(buff, "%s -e \"aflib fatal error:\n\n%s\" &", messageAppName, msg);
00090           break;
00091         case ::aflibDebug::lWarning:
00092           sprintf(buff, "%s -w \"aflib warning message:\n\n%s\" &", messageAppName, msg);
00093           break;
00094         case ::aflibDebug::lInfo:
00095           sprintf(buff, "%s -i \"aflib informational message:\n\n%s\" &", messageAppName, msg);
00096           break;
00097       default:
00098           break; // avoid compile warning
00099     }
00100     system(buff);
00101 }
00102 
00103 /*
00104  * Display a message using output_message. If the message is the same
00105  * as the previous one, just increment a count but don't display
00106  * it. This prevents flooding the user with duplicate warnings. If the
00107  * message is not the same as the previous one, then we report the
00108  * previously repeated message (if any) and reset the last message and
00109  * count.
00110  */
00111 void display_message(::aflibDebug::Level level, const char *msg) {
00112   static char lastMsg[1024];
00113   static ::aflibDebug::Level lastLevel;
00114   static int msgCount = 0;
00115 
00116     if (!strncmp(msg, lastMsg, 1024))
00117     {
00118         msgCount++;
00119     } else {
00120         if (msgCount > 0)
00121         {
00122             char buff[1024];
00123             sprintf(buff, "%s\n(The previous message was repeated %d times.)", lastMsg, msgCount);
00124             output_message(lastLevel, buff);
00125         }
00126     
00127         strncpy(lastMsg, msg, 1024);
00128         lastLevel = level;
00129         msgCount = 0;
00130         output_message(level, msg);
00131     }
00132 
00133 }
00134 
00135 static class DebugInitFromEnv {
00136 
00137    public:
00138    DebugInitFromEnv() {
00139         const char *env = getenv("AFLIB_DEBUG");
00140       if(env)
00141       {
00142          if(strcmp(env,"debug") == 0)
00143                 aflib_debug_level = ::aflibDebug::lDebug;
00144          else if(strcmp(env,"info") == 0)
00145             aflib_debug_level = ::aflibDebug::lInfo;
00146          else if(strcmp(env,"warning") == 0)
00147             aflib_debug_level = ::aflibDebug::lWarning;
00148          else if(strcmp(env,"quiet") == 0)
00149             aflib_debug_level = ::aflibDebug::lFatal;
00150          else
00151          {
00152             fprintf(stderr,
00153            "AFLIB_DEBUG must be one of debug,info,warning,quiet\n");
00154          }
00155       }
00156 
00157       env = getenv("AFLIB_DEBUG_ABORT");
00158       if(env)
00159          aflib_debug_abort = true;
00160    }
00161 } 
00162 
00163 debugInitFromEnv;
00164 
00165 
00166 void aflibDebug::init(const char *prefix, Level level)
00167 {
00168    aflib_debug_level = level;
00169    aflib_debug_prefix = prefix;
00170 }
00171 
00172 void aflibDebug::fatal(const char *fmt, ...)
00173 {
00174    char buff[1024];
00175    va_list ap;
00176    va_start(ap, fmt);
00177    vsprintf(buff, fmt, ap);
00178    va_end(ap);
00179    display_message(::aflibDebug::lFatal, buff);
00180 
00181    if(aflib_debug_abort) abort();
00182    exit(1);
00183 }
00184 
00185 void aflibDebug::warning(const char *fmt, ...)
00186 {
00187    if(lWarning >= aflib_debug_level)
00188    {
00189       char buff[1024];
00190       va_list ap;
00191       va_start(ap, fmt);
00192       vsprintf(buff, fmt, ap);
00193       va_end(ap);
00194       display_message(::aflibDebug::lWarning, buff);
00195    }
00196 }
00197 
00198 void aflibDebug::info(const char *fmt, ...)
00199 {
00200    if(lInfo >= aflib_debug_level)
00201    {
00202       char buff[1024];
00203       va_list ap;
00204       va_start(ap, fmt);
00205       vsprintf(buff, fmt, ap);
00206       va_end(ap);
00207       display_message(::aflibDebug::lInfo, buff);
00208    }
00209 }
00210 
00211 void aflibDebug::debug(const char *fmt, ...)
00212 {
00213    if(lDebug >= aflib_debug_level)
00214    {
00215       char buff[1024];
00216       va_list ap;
00217       va_start(ap, fmt);
00218       vsprintf(buff, fmt, ap);
00219       va_end(ap);
00220       display_message(::aflibDebug::lDebug, buff);
00221    }
00222 }
00223 
00224 void aflibDebug::messageApp(const char *appName)
00225 {
00226    messageAppName = (char*) realloc(messageAppName, strlen(appName)+1);
00227    strcpy(messageAppName, appName);
00228 }

Generated on Sat Oct 22 13:17:03 2005 for DarkIce by  doxygen 1.4.4