/* * * Copyright (c) 1995 * Knowledge Science Institute, University of Calgary * * Permission to use, copy, modify, distribute and sell this software * and its documentation for any purpose is hereby granted without fee, * provided that the above copyright notice appear in all copies and * that both that copyright notice and this permission notice appear * in supporting documentation. The Knowledge Science Institute makes no * representations about the suitability of this software for any * purpose. It is provided "as is" without express or implied warranty. * */ #ifdef __BCPLUSPLUS__ //---Borland C++ #include #pragma hdrstop #endif #include #include #include #include #include #include #include #include #include /* #if defined(_Windows) && !defined(QUICKWIN) && !defined(__CONSOLE__) #include #include #else #include #endif #if defined(__DLL__) #include <_win.h> #endif #if defined(_Windows) && !defined(QUICKWIN) && !defined(__CONSOLE__) #if (WINVER >= 0x030a) #include #endif #endif */ #if defined(_Windows) && !defined(QUICKWIN) && !defined(__CONSOLE__) void METAUTIL_CLASS error(HWND post, char severity, const char _FAR* format, ...) #else void METAUTIL_CLASS error(int post, char severity, const char _FAR* format, ...) #endif { va_list ap; char buf[300]=" : "; char *message=buf+3; char program_name[MAXFILE+MAXEXT-1]; char log_name[MAXPATH]; static traceIndent=0; // constuct the error message *buf = severity; va_start(ap,format); vsprintf(message,format,ap); va_end(ap); // find the name of the current executable #if (WINVER > 0x31a) char moduleFile[MAXPATH]; if (GetModuleFileName(NULL,moduleFile,MAXPATH)) { fnsplit(moduleFile,NULL,NULL,program_name,NULL); } else strcpy(program_name,"error"); #elif !defined(__DLL__) if (!(fnsplit(*_argv,NULL,NULL,program_name,NULL)&FILENAME)) {} else strcpy(program_name,""); #elif (WINVER >= 0x030a && WINVER <= 0x31a) TASKENTRY te; te.dwSize = sizeof te; strcpy(te.szModule,""); TaskFindHandle(&te,GetCurrentTask()); strncpy(program_name,te.szModule,MAXFILE); #else strcpy(program_name,"error"); #endif if (!(severity=='Z' || severity=='z')) { //don't log Z severities //+ added rck 941111 FILE *log; // write out to the error log file: ".err" fnmerge(log_name,"C","\\",program_name,".log"); #pragma warn -pia if (log=fopen(log_name,"a")) { #pragma warn .pia time_t t; time(&t); // get a the current time in a time structure char *ct = ctime(&t); // current time in ascii ct[strlen(ct)-1] = '\0'; // strip the trailing newline if (toupper(severity)=='T') { if (severity=='t' && traceIndent>0) traceIndent--; fprintf(log,"[%s] %3.3s%*s%s\n",ct,buf,traceIndent,"",buf+3); if (severity=='T') traceIndent++; } else { fprintf(log,"[%s] %s\n",ct,buf); } fclose(log); } } // write out to stderr (or a message window if windows) if post isn't -1. #if defined(_Windows) && !defined(QUICKWIN) && !defined(__CONSOLE__) TApplication* app; ErrorHandler* eh=NULL; #pragma warn -pia if (app=GetApplicationObject()) { #pragma warn .pia eh=dynamic_cast(app); TFrameWindow* wind; #pragma warn -pia if (!eh && (wind=app->GetMainWindow())) eh=dynamic_cast(wind); #pragma warn .pia } int icon; switch (severity) { case 'I': icon = MB_ICONINFORMATION; break; case 'W': icon = MB_ICONEXCLAMATION; break; case 'E': icon = MB_ICONEXCLAMATION; break; case 'F': icon = MB_ICONSTOP; break; default: icon = MB_ICONQUESTION; break; } if (((int)post)!=-1) { if (severity == 'F') { char buf2[318]; sprintf(buf2,"%s\n\nAbort Program?",buf); if (MessageBox(post, buf2, program_name, icon | MB_YESNO)==IDYES) exit(1); OutputDebugString(buf2); try { DebugBreak(); } __except(1) {}; } #pragma warn -pia else { if (eh) { #pragma warn .pia if (eh->postError(severity,(toupper(severity)=='Z')?buf+3:buf)) { MessageBox(post, buf, program_name, icon | MB_OK); //on failure (postError() returns non-zero } } else MessageBox(post, buf, program_name, icon | MB_OK); } if (icon!=MB_ICONQUESTION) MessageBeep(icon); //unknown status give no beep (for example, 'T' for 'trace' shouldn't beep) } else if (eh) eh->postError((char)toupper(severity),buf); #else if (post!=-1) { cerr << '%' << program_name << "-" << buf << endl; } if (severity == 'F') { char c = ' '; while (!((c=='Y')||(c=='N'))) { cerr << "%Abort program? (Y/N)"; cin >> c; c = toupper(c); } if (c=='Y') exit(1); } #endif }