Main Page | Alphabetical List | Compound List | File List | Compound Members | File Members

ERRORLOG.C

Go to the documentation of this file.
00001 /****************************************************************************************/
00002 /*  ERRORLOG.C                                                                          */
00003 /*                                                                                      */
00004 /*  Author: Mike Sandige                                                                */
00005 /*  Description: Generic error logging system implementation                            */
00006 /*                                                                                      */
00007 /*  The contents of this file are subject to the Genesis3D Public License               */
00008 /*  Version 1.01 (the "License"); you may not use this file except in                   */
00009 /*  compliance with the License. You may obtain a copy of the License at                */
00010 /*  http://www.genesis3d.com                                                            */
00011 /*                                                                                      */
00012 /*  Software distributed under the License is distributed on an "AS IS"                 */
00013 /*  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied.  See                */
00014 /*  the License for the specific language governing rights and limitations              */
00015 /*  under the License.                                                                  */
00016 /*                                                                                      */
00017 /*  The Original Code is Genesis3D, released March 25, 1999.                            */
00018 /*Genesis3D Version 1.1 released November 15, 1999                            */
00019 /*  Copyright (C) 1999 WildTangent, Inc. All Rights Reserved           */
00020 /*                                                                                      */
00021 /****************************************************************************************/
00022 #define WIN32_LEAN_AND_MEAN
00023 #include <windows.h>
00024 #include <stdio.h>
00025 #include <assert.h>             // assert()     
00026 #include <stdlib.h>
00027 #include <string.h>             // memmove(), strncpy() strncat()
00028 
00029 #include "ErrorLog.h"   
00030 
00031 #define MAX_ERRORS 30  //  ...
00032 
00033 #define MAX_USER_NAME_LEN       128             // Needed just 10 more chars! (was 100) JP
00034 #define MAX_CONTEXT_LEN         128             // How big should this be?  Must be big enough to allow full paths to files, etc...
00035 
00036 typedef struct
00037 {
00038         geErrorLog_ErrorIDEnumType ErrorID;
00039         char String[MAX_USER_NAME_LEN+1];
00040         char Context[MAX_CONTEXT_LEN+1];
00041 } geErrorType;
00042 
00043 typedef struct
00044 {
00045         int ErrorCount;
00046         int MaxErrors;
00047         geErrorType ErrorList[MAX_ERRORS];
00048 } geErrorLogType;
00049 
00050 geErrorLogType geErrorLog_Locals = {0,MAX_ERRORS};
00051 
00052 GENESISAPI void geErrorLog_Clear(void)
00053         // clears error history
00054 {
00055         geErrorLog_Locals.ErrorCount = 0;
00056 }
00057         
00058 GENESISAPI int  geErrorLog_Count(void)
00059         // reports size of current error log
00060 {
00061         return  geErrorLog_Locals.ErrorCount;
00062 }
00063 
00064 
00065 GENESISAPI void geErrorLog_AddExplicit(geErrorLog_ErrorClassType Error, 
00066         const char *ErrorIDString,
00067         const char *ErrorFileString,
00068         int LineNumber,
00069         const char *UserString,
00070         const char *Context)
00071 {
00072         char    *SDst;
00073         char    *CDst;
00074 
00075         assert( geErrorLog_Locals.ErrorCount >= 0 );
00076 
00077         if (geErrorLog_Locals.ErrorCount>=MAX_ERRORS)
00078         {       // scoot list down by one (lose oldest error)
00079                 memmove(
00080                         (char *)(&( geErrorLog_Locals.ErrorList[0] )),
00081                         (char *)(&( geErrorLog_Locals.ErrorList[1] )),
00082                         sizeof(geErrorType) * (geErrorLog_Locals.MaxErrors-1) );
00083                 geErrorLog_Locals.ErrorCount = geErrorLog_Locals.MaxErrors-1;
00084         }
00085 
00086         assert( geErrorLog_Locals.ErrorCount < geErrorLog_Locals.MaxErrors );
00087 
00088         SDst = geErrorLog_Locals.ErrorList[geErrorLog_Locals.ErrorCount].String;
00089 
00090         // Copy new error info
00091         if (ErrorIDString != NULL)
00092                 {
00093                         strncpy(SDst,ErrorIDString,MAX_USER_NAME_LEN);
00094                 }
00095 
00096         strncat(SDst," ",MAX_USER_NAME_LEN);
00097 
00098         if (ErrorFileString!=NULL)
00099                 {
00100                         const char* pModule = strrchr(ErrorFileString, '\\');
00101                         if(!pModule)
00102                                 pModule = ErrorFileString;
00103                         else
00104                                 pModule++; // skip that backslash
00105                         strncat(SDst,pModule,MAX_USER_NAME_LEN);
00106                         strncat(SDst," ",MAX_USER_NAME_LEN);
00107                 }
00108         
00109         {
00110                 char Number[20];
00111                 itoa(LineNumber,Number,10);
00112                 strncat(SDst,Number,MAX_USER_NAME_LEN);
00113         }
00114         
00115         if (UserString != NULL)
00116                 {
00117                         if (UserString[0]!=0)
00118                                 {
00119                                         strncat(SDst," ",MAX_USER_NAME_LEN);
00120                                         strncat(SDst,UserString,MAX_USER_NAME_LEN);
00121                                 }
00122                 }
00123 
00124         CDst = geErrorLog_Locals.ErrorList[geErrorLog_Locals.ErrorCount].Context;
00125 
00126         // Clear the context string in the errorlog to prepare for a new one
00127         memset(CDst, 0, sizeof(char)*MAX_CONTEXT_LEN);
00128 
00129         if (Context != NULL)
00130         {
00131                 if (Context[0]!=0)
00132                 {
00133                         //strncat(SDst," ",MAX_USER_NAME_LEN);
00134                         strncat(SDst,Context,MAX_USER_NAME_LEN);
00135                 }
00136         }
00137 
00138         geErrorLog_Locals.ErrorCount++;
00139 
00140 #ifndef NDEBUG
00141 #pragma message ("Clean up the OutputDebugStrings in geErrorLog_AddExplicit")
00142 {
00143         char    buff[100];
00144         sprintf(buff, "ErrorLog: %d -", Error);
00145         OutputDebugString(buff);
00146         OutputDebugString(SDst);
00147         OutputDebugString("\r\n");
00148 }
00149 #endif
00150 }
00151 
00152 
00153 
00154 GENESISAPI geBoolean geErrorLog_AppendStringToLastError(const char *String)
00155 {
00156         char *SDst;
00157         if (String == NULL)
00158                 {
00159                         return GE_FALSE;
00160                 }
00161 
00162         if (geErrorLog_Locals.ErrorCount>0)
00163                 {
00164                         SDst = geErrorLog_Locals.ErrorList[geErrorLog_Locals.ErrorCount-1].String;
00165 
00166                         strncat(SDst,String,MAX_USER_NAME_LEN);
00167                         return GE_TRUE;
00168                 }
00169         else
00170                 {
00171                         return GE_FALSE;
00172                 }
00173 }
00174 
00175 GENESISAPI geBoolean geErrorLog_Report(int history, geErrorLog_ErrorClassType *error, const char **UserString)
00176 {
00177         assert( error != NULL );
00178 
00179         if ( (history > geErrorLog_Locals.ErrorCount) || (history < 0))
00180                 {
00181                         return GE_FALSE;
00182                 }
00183         
00184         
00185         *error = geErrorLog_Locals.ErrorList[history].ErrorID;
00186         *UserString = geErrorLog_Locals.ErrorList[history].String;
00187         return GE_TRUE;
00188 }
00189 

Generated on Tue Sep 30 12:35:42 2003 for GTestAndEngine by doxygen 1.3.2