Main Page | Modules | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

phError.cpp

Go to the documentation of this file.
00001 /* ---------------------------------------------------------------------------
00002     Phission :
00003         Realtime Vision Processing System
00004 
00005     Copyright (C) 2003-2006 Philip D.S. Thoren (pthoren@cs.uml.edu)
00006     University of Massachusetts at Lowell,
00007     Laboratory for Artificial Intelligence and Robotics
00008 
00009     This file is part of Phission.
00010 
00011     Phission is free software; you can redistribute it and/or modify
00012     it under the terms of the GNU Lesser General Public License as published by
00013     the Free Software Foundation; either version 2 of the License, or
00014     (at your option) any later version.
00015 
00016     Phission is distributed in the hope that it will be useful,
00017     but WITHOUT ANY WARRANTY; without even the implied warranty of
00018     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019     GNU Lesser General Public License for more details.
00020 
00021     You should have received a copy of the GNU Lesser General Public License
00022     along with Phission; if not, write to the Free Software
00023     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00024 
00025  ---------------------------------------------------------------------------*/
00026 #ifdef HAVE_CONFIG_H
00027     #include <phissionconfig.h>
00028 #endif
00029 
00030 #include <phStandard.h>
00031 
00032 #if defined(WIN32)
00033     #include <windows.h>
00034     #include <wchar.h>
00035 #endif
00036 
00037 #ifdef __ADSPBLACKFIN__
00038     #include <PhissionVDK.h>
00039 #endif
00040 
00041 #include <phError.h>
00042 #include <phMemory.h>
00043 
00044 #ifdef __cplusplus
00045 extern "C" {
00046 #endif
00047 
00048 /* ------------------------------------------------------------------------- *
00049  * There is no string lookup table for the errors in VDK. I'm creating one
00050  * here for debug/development purposes only. When making the production 
00051  * release, the table will be disabled and printing will happen instead. 
00052  * ------------------------------------------------------------------------- */
00053 #if phVDK_ERROR_LOOKUP_TABLE
00054 #define phVDK_TOTAL_ERRORS 58
00055 static char *phVDK_ERROR_TABLE[phVDK_TOTAL_ERRORS] = {
00056                                         "kNoError",
00057                                         "kUnknownThreadType",
00058                                         "kUnknownThread",
00059                                         "kInvalidThread",
00060                                         "kThreadCreationFailure",
00061                                         "kUnknownSemaphore",
00062                                         "kUnknownEventBit",
00063                                         "kUnknownEvent",
00064                                         "kInvalidPriority",
00065                                         "kInvalidDelay",
00066                                         "kSemaphoreTimeout",
00067                                         "kEventTimeout",
00068                                         "kBlockInInvalidRegion",
00069                                         "kDbgPossibleBlockInRegion",
00070                                         "kInvalidPeriod",
00071                                         "kAlreadyPeriodic",
00072                                         "kNonperiodicSemaphore",
00073                                         "kDbgPopUnderflow",
00074                                         "kBadIOID",
00075                                         "kBadDeviceDescriptor",
00076                                         "kOpenFailure",
00077                                         "kCloseFailure",
00078                                         "kReadFailure",
00079                                         "kWriteFailure",
00080                                         "kIOCtlFailure",
00081                                         "kInvalidDeviceFlag",
00082                                         "kDeviceTimeout",
00083                                         "kDeviceFlagCreationFailure",
00084                                         "kMaxCountExceeded",
00085                                         "kSemaphoreCreationFailure",
00086                                         "kSemaphoreDestructionFailure",
00087                                         "kPoolCreationFailure",
00088                                         "kInvalidBlockPointer",
00089                                         "kInvalidPoolParms",
00090                                         "kInvalidPoolID",
00091                                         "kErrorPoolNotEmpty",
00092                                         "kErrorMallocBlock",
00093                                         "kMessageCreationFailure",
00094                                         "kInvalidMessageID",
00095                                         "kInvalidMessageOwner",
00096                                         "kInvalidMessageChannel",
00097                                         "kInvalidMessageRecipient",
00098                                         "kMessageTimeout",
00099                                         "kMessageInQueue",
00100                                         "kInvalidTimeout",
00101                                         "kInvalidTargetDSP",
00102                                         "kIOCreateFailure",
00103                                         "kHeapInitialisationFailure",
00104                                         "kInvalidHeapID",
00105                                         "kNewFailure",
00106                                         "kInvalidMarshalledType",
00107                                         "kUncaughtException",
00108                                         "kAbort",
00109                                         "kInvalidMaskBit",
00110                                         "kInvalidThreadStatus",
00111                                         "kThreadStackOverflow",
00112                                         "kMaxIDExceeded",
00113                                         "kThreadDestroyedInInvalidRegion"
00114 };
00115 #endif /* #if phVDK_ERROR_LOOKUP_TABLE */
00116 
00117 /* ------------------------------------------------------------------------- *
00118  * phError retreives the error message to be printed and prints it.
00119  * On *nix/Cygwin this is as simple as perror.
00120  * In Win32, FormatMessage and GetLastError are used.
00121  *
00122  * TODO: Add the thread_id to this too 
00123  * ------------------------------------------------------------------------- */
00124 void phError( char *msg )
00125 {
00126 #if defined(WIN32)
00127     LPVOID  lpMsgBuf;
00128     DWORD   err         = GetLastError();
00129     DWORD   length      = 0;
00130 #endif
00131 #ifdef __ADSPBLACKFIN__
00132     const int err       = (int)VDK::GetLastThreadError();
00133     const int errval    = (int)VDK::GetLastThreadErrorValue();
00134     const int err_index = (err-INT_MIN)+1;
00135 #endif
00136 
00137 #if !defined(WIN32)
00138 
00139 #ifdef __ADSPBLACKFIN__
00140     if ((err < 0) && (err_index < phVDK_TOTAL_ERRORS))
00141     {
00142     #if phVDK_ERROR_LOOKUP_TABLE
00143         phPRINT("VDK Error(err:%d)(val:%d)(%s): %s\n", err,
00144                 errval,
00145                 phVDK_ERROR_TABLE[err_index],
00146                 msg);
00147     #else
00148         phPRINT("VDK Error (err:%d)(index:%d)(System Error): %s\n", 
00149                 err, err_index, msg );
00150     #endif
00151     }
00152     /* User error */
00153     else if (err > 0)
00154     {
00155         phPRINT("VDK Error (err:%d)(val:%d)(User error): %s\n",
00156                 err, errval, msg);
00157     }
00158     else
00159     {
00160         phPRINT("No VDK Error\n");
00161         perror( msg );
00162     }
00163 #else
00164     perror( msg );
00165 #endif
00166 
00167 #endif 
00168 
00169 #if defined(WIN32)
00170     if (!(length = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00171                                  FORMAT_MESSAGE_FROM_SYSTEM | 
00172                                  FORMAT_MESSAGE_IGNORE_INSERTS,
00173                                  NULL,
00174                                  err,
00175                                  MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
00176                                  (LPTSTR)&lpMsgBuf,
00177                                  0,
00178                                  NULL )))
00179     {
00180         printf( "\tError: Unable to retrieve Windows error.\n");
00181         printf( "\tError: FormatMessage: %u\n", (unsigned int)GetLastError());
00182     }
00183     else
00184     {
00185         #if defined(_MSC_VER)
00186         wchar_t *msgptr = (wchar_t *)lpMsgBuf;
00187         msgptr[length-1] = L'\0';
00188         wprintf( L"Windows Error(%u): %s",(unsigned int)err,msgptr );
00189         #else
00190         char *msgptr = (char *)lpMsgBuf;
00191         msgptr[length-1] = '\0';
00192         printf( "Windows Error(%u): %s",(unsigned int)err,msgptr );
00193         #endif
00194         LocalFree(lpMsgBuf);
00195     }
00196     
00197     printf("\n");
00198     fflush(stdout);
00199     
00200 #endif
00201 
00202     return;
00203 }
00204 
00205 /* ------------------------------------------------------------------------- */
00206 void phErrorString( ph_error errnum, ph_string *buf )
00207 {
00208     phFUNCTION("phErrorString")
00209 
00210     uint32_t  length    = 0;
00211     
00212 #if defined(WIN32)
00213     LPVOID  lpMsgBuf    = NULL;
00214 #elif defined(__ADSPBLACKFIN__)
00215     const int err_index = (errnum-INT_MIN)+1;
00216     char *error_string = 
00217         /* if errnum == 0 */
00218         (errnum == 0) ?
00219                "No VDK Error"              /* no error */
00220         /* else if err < 0 */
00221         : (errnum < 0) ?
00222                
00223         #if phVDK_ERROR_LOOKUP_TABLE
00224                phVDK_ERROR_TABLE[err_index]/* Get string from lookup table  */
00225         #else
00226                "System Error"              /* No lookup table: system error */
00227         #endif
00228         /* else */
00229         :      "User Error";               /* User error */
00230 #else
00231     char *error_string  = strerror( errnum );
00232 #endif
00233 
00234 
00235 #if !defined(WIN32)
00236     length = strlen(error_string);
00237 
00238     #if defined(__ADSPBLACKFIN__)
00239     (*buf) = (char *)phCalloc( length + 35, sizeof(char));
00240     phPRINT_PTR(*buf,"phCalloc","phCalloc failed to allocate buffer");
00241 
00242     sprintf( *buf, "VDK Error(err:%d): %s",error_string);
00243     #else
00244     (*buf) = (char *)phCalloc( length + 1, sizeof(char));
00245     phPRINT_PTR(*buf,"phCalloc","phCalloc failed to allocate buffer");
00246 
00247     sprintf( *buf, "%s", error_string );
00248     #endif
00249 #endif 
00250 
00251 #if defined(WIN32)
00252     if ((length = FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
00253                                  FORMAT_MESSAGE_FROM_SYSTEM | 
00254                                  FORMAT_MESSAGE_IGNORE_INSERTS,
00255                                  NULL,
00256                                  errnum,
00257                                  MAKELANGID(LANG_NEUTRAL,SUBLANG_DEFAULT),
00258                                  (LPTSTR)&lpMsgBuf,
00259                                  0,
00260                                  NULL )) > 0)
00261     {
00262         printf( "\tError: Unable to retrieve Windows error.\n");
00263         printf( "\tError: FormatMessage - %u\n", (unsigned int)GetLastError());
00264     }
00265     else
00266     {
00267         #if defined(_MSC_VER)
00268         wchar_t    *tempptr = (wchar_t *)lpMsgBuf;
00269         char       *msgptr  = NULL;
00270         
00271         tempptr[length - 1] = L'\0';
00272         msgptr = (char *)phMalloc(sizeof(char) * length);
00273 
00274         wcstombs(msgptr,tempptr,sizeof(char) * length);
00275         #else
00276         char       *msgptr  = (char *)lpMsgBuf;
00277 
00278         msgptr[length - 1]  = '\0';
00279         #endif
00280 
00281         (*buf) = (char *)phCalloc( length + 35, sizeof(char));
00282         phPRINT_PTR(*buf,"phCalloc","phCalloc failed to allocate buffer");
00283         
00284         sprintf( *buf, "Windows Error(%u): %s",(unsigned int)errnum, msgptr );
00285         LocalFree(lpMsgBuf);
00286 
00287         #if defined(_MSC_VER)
00288         phFree(msgptr);
00289         #endif
00290     }
00291 #endif
00292 
00293     return;
00294 }
00295 
00296 #ifdef __cplusplus
00297 } /* extern "C" { */
00298 #endif
00299 
00300 
00301 




Copyright (C) 2002 - 2007 Philip D.S. Thoren ( pthoren@users.sourceforge.net )
University Of Massachusetts at Lowell
Robotics Lab
SourceForge.net Logo

Generated on Sat Jun 16 02:44:02 2007 for phission by  doxygen 1.4.4