Changeset 636 for trunk/dll/error.c
- Timestamp:
- Apr 24, 2007, 3:08:46 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/dll/error.c
r616 r636 24 24 19 Apr 07 SHL Add DbgMsg 25 25 20 Apr 07 SHL Correct IDS_GENERR1TEXT formatting 26 23 Apr 07 SHL Add Win_Error_NoMsgBox. Rework others 26 27 27 28 ***********************************************************************/ … … 43 44 #pragma alloc_text(FMINPUT,Win_Error,Dos_Error,saymsg,showMsg) 44 45 46 static VOID formatWinError(PSZ pszBuf, UINT cBufBytes, HWND hwndErr, HWND hwndOwner, 47 PCSZ pszSrcFile, UINT uSrcLineNo, 48 PCSZ pszFmt, va_list pva); 49 45 50 static APIRET showMsg(ULONG mb_type, HWND hwnd, PCSZ pszTitle, PCSZ pszMsg, BOOL wantLog); 46 51 47 //== Win_Error: report Win...() error using passed message string === 48 49 VOID Win_Error(HWND hwndErr, HWND hwndOwner, PCSZ pszFileName, ULONG ulLineNo, 50 PCSZ pszFmt, ...) 51 { 52 PERRINFO pErrInfoBlk; /* Pointer to ERRINFO structure filled 53 by WinGetErrorInfo */ 54 PSZ pszOffset; /* Pointer to current error message returned 55 by WinGetErrorInfo */ 56 CHAR szMsg[4096]; 57 PSZ psz; 58 HAB hab; 59 va_list va; 60 61 if (hwndErr == NULLHANDLE) 62 hab = (HAB)0; 63 else 64 hab = WinQueryAnchorBlock(hwndErr); 65 66 // Format callers message 67 va_start(va, pszFmt); 68 szMsg[sizeof(szMsg) - 1] = 0; 69 vsprintf(szMsg, pszFmt, va); 70 va_end(va); 71 72 if (szMsg[sizeof(szMsg) - 1]) { 73 fprintf(stderr, "Buffer overflow in Win_Error - need %u bytes\n", strlen(szMsg) + 1); 74 fflush(stderr); 75 } 76 77 if (strchr(szMsg, ' ') == NULL) 78 strcat(szMsg, " failed."); // Assume simple function name 79 80 // Append file name and line number 81 sprintf(szMsg + strlen(szMsg), 82 GetPString(IDS_GENERR1TEXT), pszFileName, ulLineNo); 83 84 // Get last PM error for the current thread 85 pErrInfoBlk = WinGetErrorInfo(hab); 86 if (!pErrInfoBlk) { 87 psz = szMsg + strlen(szMsg); 88 strcpy(psz, " WinGetErrorInfo failed."); 89 } 90 else { 91 if (!hwndOwner) 92 hwndOwner = HWND_DESKTOP; 93 /* Find message offset in array of message offsets 94 Assume 1 message - fixme? 95 */ 96 pszOffset = ((PSZ) pErrInfoBlk) + pErrInfoBlk->offaoffszMsg; 97 /* Address error message in array of messages and 98 append error message to source code linenumber 99 */ 100 psz = szMsg + strlen(szMsg); 101 sprintf(psz, "#0x%04x \"", ERRORIDERROR(pErrInfoBlk->idError)); 102 psz += strlen(psz); 103 strcpy(psz, ((PSZ) pErrInfoBlk) + *(PSHORT) pszOffset); 104 psz += strlen(psz); 105 // Chop trailing mush 106 psz--; 107 while (*psz == '\r' || *psz == '\n' || *psz == ' ') 108 *psz-- = 0; 109 if (*psz) 110 psz++; 111 strcpy(psz, "\""); 112 WinFreeErrorInfo(pErrInfoBlk); // Free resource segment 113 } 114 115 showMsg(MB_ENTER | MB_ICONEXCLAMATION, hwndOwner, GetPString(IDS_GENERR2TEXT), 116 szMsg, TRUE); 117 118 } // Win_Error 119 120 //== Win_Error2: report Win...() error using passed message id === 121 122 VOID Win_Error2(HWND hwndErr, HWND hwndOwner, PCSZ pszFileName, 123 ULONG ulLineNo, UINT idMsg) 124 { 125 Win_Error(hwndErr, hwndOwner, pszFileName, ulLineNo, GetPString(idMsg)); 126 127 } // Win_Error2 52 //=== DbgMsg: output debug message stderr === 53 54 VOID DbgMsg(PCSZ pszSrcFile, UINT uSrcLineNo, PCSZ pszFmt, ...) 55 { 56 va_list va; 57 58 // OK for source file name to be null 59 fprintf(stderr, "%s %u", pszSrcFile ? pszSrcFile : "n/a", uSrcLineNo); 60 // If format null want just file and line 61 if (pszFmt) { 62 fputc(' ', stderr); 63 va_start(va, pszFmt); 64 vfprintf(stderr, pszFmt, va); 65 va_end(va); 66 } 67 fputc('\n', stderr); 68 fflush(stderr); 69 70 } // DbgMsg 128 71 129 72 //== Dos_Error: report Dos...() error using passed message string === 130 73 131 INT Dos_Error(ULONG mb_type, ULONG ulRC, HWND hwndOwner, PCSZ pszFileName,132 ULONG ulLineNo, PCSZ pszFmt, ...)133 { 134 CHAR szMsg[4096]; 135 ULONG Class; // Error class136 ULONG action; // Error action137 ULONG Locus; // Error location74 INT Dos_Error(ULONG mb_type, ULONG ulRC, HWND hwndOwner, 75 PCSZ pszSrcFile, UINT uSrcLineNo, PCSZ pszFmt, ...) 76 { 77 CHAR szMsg[4096]; 78 ULONG Class; // Error class 79 ULONG action; // Error action 80 ULONG Locus; // Error location 138 81 ULONG ulMsgLen; 139 82 CHAR *pszMsgStart; … … 156 99 157 100 if (strchr(szMsg, ' ') == NULL) 158 strcat(szMsg, " failed ."); // Assume simple function name101 strcat(szMsg, " failed"); // Assume simple function name 159 102 160 103 DosErrClass(ulRC, &Class, &action, &Locus); … … 162 105 sprintf(szMsg + strlen(szMsg), 163 106 GetPString(IDS_DOSERR1TEXT), 164 psz FileName,165 u lLineNo,107 pszSrcFile, 108 uSrcLineNo, 166 109 ulRC, 167 110 GetPString(IDS_ERRCLASS1TEXT + (Class - 1)), … … 203 146 szMsg, TRUE); 204 147 205 } 148 } // Dos_Error 206 149 207 150 //== Dos_Error2: report Dos...() error using passed message id === 208 151 209 INT Dos_Error2(ULONG mb_type, ULONG ulRC, HWND hwndOwner, PCSZ pszFileName,210 ULONG ulLineNo, UINT idMsg)211 { 212 return Dos_Error(mb_type, ulRC, hwndOwner, psz FileName, ulLineNo,152 INT Dos_Error2(ULONG mb_type, ULONG ulRC, HWND hwndOwner, 153 PCSZ pszSrcFile, UINT uSrcLineNo, UINT idMsg) 154 { 155 return Dos_Error(mb_type, ulRC, hwndOwner, pszSrcFile, uSrcLineNo, 213 156 GetPString(idMsg)); 214 } // Dos_Error2 157 } // Dos_Error2 158 159 /** 160 * Format last PM error into passed buffer 161 */ 162 163 static VOID formatWinError(PSZ pszBuf, UINT cBufBytes, 164 HWND hwndErr, HWND hwndOwner, 165 PCSZ pszSrcFile, UINT uSrcLineNo, 166 PCSZ pszFmt, va_list pva) 167 { 168 PERRINFO pErrInfoBlk; /* Pointer to ERRINFO structure filled 169 by WinGetErrorInfo */ 170 PSZ pszOffset; /* Pointer to current error message returned 171 by WinGetErrorInfo */ 172 PSZ psz; 173 HAB hab; 174 175 if (hwndErr == NULLHANDLE) 176 hab = (HAB)0; 177 else 178 hab = WinQueryAnchorBlock(hwndErr); 179 180 // Format callers message 181 pszBuf[cBufBytes - 1] = 0; 182 vsprintf(pszBuf, pszFmt, pva); 183 184 if (pszBuf[cBufBytes - 1]) { 185 fprintf(stderr, "Buffer overflow in formatWinError - need %u bytes\n", strlen(pszBuf) + 1); 186 fflush(stderr); 187 } 188 189 if (strchr(pszBuf, ' ') == NULL) 190 strcat(pszBuf, " failed"); // Assume simple function name 191 192 // Append file name and line number and trailing space 193 sprintf(pszBuf + strlen(pszBuf), 194 GetPString(IDS_GENERR1TEXT), pszSrcFile, uSrcLineNo); 195 196 // Get last PM error for the current thread 197 pErrInfoBlk = WinGetErrorInfo(hab); 198 if (!pErrInfoBlk) { 199 psz = pszBuf + strlen(pszBuf); 200 strcpy(psz, "WinGetErrorInfo failed"); 201 } 202 else { 203 if (!hwndOwner) 204 hwndOwner = HWND_DESKTOP; 205 /* Find message offset in array of message offsets 206 Assume 1 message - fixme? 207 */ 208 pszOffset = ((PSZ) pErrInfoBlk) + pErrInfoBlk->offaoffszMsg; 209 /* Address error message in array of messages and 210 append error message to source code linenumber 211 */ 212 psz = pszBuf + strlen(pszBuf); 213 sprintf(psz, "#0x%04x \"", ERRORIDERROR(pErrInfoBlk->idError)); 214 psz += strlen(psz); 215 strcpy(psz, ((PSZ) pErrInfoBlk) + *(PSHORT) pszOffset); 216 psz += strlen(psz); 217 // Chop trailing mush 218 psz--; 219 while (*psz == '\r' || *psz == '\n' || *psz == ' ') 220 *psz-- = 0; 221 if (*psz) 222 psz++; 223 strcpy(psz, "\""); 224 WinFreeErrorInfo(pErrInfoBlk); // Free resource segment 225 } 226 227 } // formatWinError 215 228 216 229 //== Runtime_Error: report runtime library error using passed message string === … … 234 247 235 248 if (strchr(szMsg, ' ') == NULL) 236 strcat(szMsg, " failed ."); // Assume simple function name249 strcat(szMsg, " failed"); // Assume simple function name 237 250 238 251 sprintf(szMsg + strlen(szMsg), … … 241 254 showMsg(MB_ICONEXCLAMATION, HWND_DESKTOP, DEBUG_STRING, szMsg, TRUE); 242 255 243 } 256 } // Runtime_Error 244 257 245 258 //== Runtime_Error2: report runtime library error using passed message id === … … 249 262 Runtime_Error(pszSrcFile, uSrcLineNo, GetPString(idMsg)); 250 263 251 } 264 } // Runtime_Error2 252 265 253 266 // fixme to be rename to Misc_Error … … 272 285 return showMsg(mb_type, hwnd, pszTitle, szMsg, FALSE); 273 286 274 } 287 } // saymsg 275 288 276 289 //=== showMsg: display error popup === 277 290 278 static APIRET showMsg(ULONG mb_type, HWND hwndOwner, PCSZ pszTitle, PCSZ pszMsg, BOOL wantLog) 291 static APIRET showMsg(ULONG mb_type, HWND hwndOwner, 292 PCSZ pszTitle, PCSZ pszMsg, BOOL wantLog) 279 293 { 280 294 if (wantLog) { … … 296 310 } // showMsg 297 311 298 //=== DbgMsg: output debug message stderr === 299 300 VOID DbgMsg(PCSZ pszSrcFile, UINT uSrcLineNo, PCSZ pszFmt, ...) 301 { 302 va_list va; 303 304 // OK for source file name to be null 305 fprintf(stderr, "%s %u", pszSrcFile ? pszSrcFile : "n/a", uSrcLineNo); 306 // If format null want just file and line 307 if (pszFmt) { 308 fputc(' ', stderr); 309 va_start(va, pszFmt); 310 vfprintf(stderr, pszFmt, va); 311 va_end(va); 312 } 312 //== Win_Error: report Win...() error using passed message string === 313 314 VOID Win_Error(HWND hwndErr, HWND hwndOwner, 315 PCSZ pszSrcFile, UINT uSrcLineNo, 316 PCSZ pszFmt, ...) 317 { 318 CHAR szMsg[4096]; 319 PSZ psz; 320 HAB hab; 321 va_list va; 322 323 // Format callers message 324 va_start(va, pszFmt); 325 formatWinError(szMsg, sizeof(szMsg), hwndErr, hwndOwner, pszSrcFile, uSrcLineNo, pszFmt, va); 326 va_end(va); 327 328 showMsg(MB_ENTER | MB_ICONEXCLAMATION, hwndOwner, GetPString(IDS_GENERR2TEXT), 329 szMsg, TRUE); 330 331 } // Win_Error 332 333 //== Win_Error2: report Win...() error using passed message id === 334 335 VOID Win_Error2(HWND hwndErr, HWND hwndOwner, 336 PCSZ pszSrcFile, UINT uSrcLineNo, UINT idMsg) 337 { 338 Win_Error(hwndErr, hwndOwner, pszSrcFile, uSrcLineNo, GetPString(idMsg)); 339 340 } // Win_Error2 341 342 /** 343 * Output PM error messsage to stderr 344 * This does to same reporting as Win_Error, but bypasses the 345 * message box popup. 346 * Use this version when the popup would hang PM. 347 */ 348 349 VOID Win_Error_NoMsgBox(HWND hwndErr, HWND hwndOwner, 350 PCSZ pszSrcFile, UINT uSrcLineNo, PCSZ pszFmt, ...) 351 { 352 CHAR szMsg[4096]; 353 va_list va; 354 355 // Format callers message 356 va_start(va, pszFmt); 357 formatWinError(szMsg, sizeof(szMsg), hwndErr, hwndOwner, pszSrcFile, uSrcLineNo, pszFmt, va); 358 va_end(va); 359 360 fputs(szMsg, stderr); 361 fputc('\n', stderr); 313 362 fputc('\n', stderr); 314 363 fflush(stderr); 315 364 316 } // DbgMsg 317 365 DosBeep(250, 100); 366 367 } // Win_Error_NoMsgBox
Note:
See TracChangeset
for help on using the changeset viewer.