-
Notifications
You must be signed in to change notification settings - Fork 0
/
dl_err.cpp
1626 lines (1353 loc) · 60.7 KB
/
dl_err.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*===========================================================================
*
* File: DL_Err.CPP
* Author: Dave Humphrey (uesp@m0use.net)
* Created On: Tuesday, April 24, 2001
*
* Class CErrorDatabase and CErrorHandler for handling custom errors
* in an application. The database class supports any system/platform
* specific errors meaning that you only need to worry about handling
* errors with the global objects ErrorHandler and ErrorDatabase. For
* instance, if you were making a Windows, DirectDraw, COM application,
* you would normally have to worry about the system, Windows, DirectDraw
* and the COM specific errors, in addition to your own (thats 5 different
* error systems). All these methods can be 'hidden' by the error handling
* classes, making your code better looking.
*
* TODO: The ErrorHandler records error incidents in a singly linked list.
* Initially, the list simply grew until the program ended, but in
* some cases this caused problems (1000's of errors being saved).
* Now, up to MAX_ERROR_INCIDENTS are recorded. Once this limit
* is reached, the incidents are deleted except for the current
* one. Better methods could be used if so required.
*
* Version History
* -------------------------------------------------------------------------
* 2 December 2002 (Dave Humphrey)
* - Moved from regular char to TCHAR type to support wide characters
* under Windows. Successfully tested.
*
* 25 June 2003
* - Added the NotifyListCode() and NotifyListType() methods.
*
* 18 September 2003
* - Modified NotifyList() to output the error description from the
* database if it is available.
*
*=========================================================================*/
/* Include Files */
#include "dl_err.h"
#include "dl_str.h"
#if !defined(_WIN32_WCE)
#include <errno.h>
/* Include TC graphic error messages */
#if defined(_TCGRAPHERRORS)
#include <graphics.h>
#endif
#endif
/* Include Windows error messages/functions */
#if defined(_WIN32) || defined(__BCPLUSPLUS__)
#include "windows.h"
#endif
/*===========================================================================
*
* Begin Local Variables
*
*=========================================================================*/
DEFINE_FILE("DL_Err.h");
/*===========================================================================
* End of Local Variables
*=========================================================================*/
/*===========================================================================
*
* Begin Global Variables
*
*=========================================================================*/
CErrorDatabase ErrorDatabase;
CErrorHandler ErrorHandler(ErrorDatabase);
/*===========================================================================
* End of Global Variables
*=========================================================================*/
/*===========================================================================
*
* Class CErrorRecord Constructor
*
*=========================================================================*/
CErrorRecord::CErrorRecord (void) {
pMessage = NULL;
CustomErrFunction = NULL;
Code = ERR_NONE;
Level = ERRLEVEL_UNKNOWN;
pNext = NULL;
}
/*===========================================================================
* End of Class CErrorRecord Constructor
*=========================================================================*/
/*===========================================================================
*
* Class CErrorRecord Method - void Destroy (void);
*
* Deletes the contents of the object.
*
*=========================================================================*/
void CErrorRecord::Destroy (void) {
DEFINE_FUNCTION("CErrorRecord::Destroy()");
DestroyPointer(pMessage);
Code = ERR_NONE;
Level = ERRLEVEL_UNKNOWN;
CustomErrFunction = NULL;
/* Assumed the linked list structure is maintained by the parent */
pNext = NULL;
}
/*===========================================================================
* End of Class Method CErrorRecord::Destroy()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorRecord Method - TCHAR* GetMsg (SubCode) const;
*
* Attempts to retrieve the message for the error. If the custom error
* function is non-NULL, the message is returned from that function using
* the given error SubCode. Otherwise, the current message string of the
* object is returned.
*
*=========================================================================*/
TCHAR* CErrorRecord::GetMsg (const errcode_t SubCode) const {
//DEFINE_FUNCTION("CErrorRecord::GetMsg()");
/* Return the current message string for a regular error */
if (CustomErrFunction == NULL) return (pMessage);
/* Call the custom error function with the error's subcode */
return CustomErrFunction(SubCode);
}
/*===========================================================================
* End of Class Method CErrorRecord::GetMsg()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorDatabase Constructor
*
*=========================================================================*/
CErrorDatabase::CErrorDatabase (void) {
pHead = NULL;
AddedDefaultErrors = FALSE;
NumErrors = 0;
InitDefaultErrors();
}
/*===========================================================================
* End of Class CErrorDatabase Constructor
*=========================================================================*/
/*===========================================================================
*
* Class CErrorDatabase Method - void Destroy (void);
*
* Deletes the current contents of the object.
*
*=========================================================================*/
void CErrorDatabase::Destroy (void) {
//DEFINE_FUNCTION("CErrorDatabase::Destroy()");
/* Delete the singly linked list */
ClearErrors();
}
/*===========================================================================
* End of Class Method CErrorDatabase::Destroy()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorDatabase Method - void Add (Code, pMessage, Level);
*
* Attempts to add the given custom error definition to the singly
* linked list. The error Level is optional and defaults to ERRLEVEL_ERROR.
* On any error, an exception is thrown (no memory to allocate).
*
*=========================================================================*/
void CErrorDatabase::Add (const errcode_t Code, const TCHAR* pMessage, const errlevel_t Level) {
DEFINE_FUNCTION("CErrorDatabase::Add()");
CErrorRecord* pNewRecord;
/* Ensure valid input */
ASSERT(pMessage != NULL);
/* Attempt to allocate the new record */
CreatePointer(pNewRecord, CErrorRecord);
/* Attempt to initialize the new record */
pNewRecord->SetCode(Code);
pNewRecord->SetLevel(Level);
pNewRecord->SetMsg(pMessage);
/* Add the new error to the head of the linked list */
pNewRecord->SetNext(pHead);
pHead = pNewRecord;
NumErrors++;
}
/*===========================================================================
* End of Class Method CErrorDatabase::Add()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorDatabase Method - void AddCustomError (Code, ErrFunction);
*
* Adds a custom error function with the given error code to the database.
* Throws an exception if memory could not be allocated.
*
*=========================================================================*/
void CErrorDatabase::AddCustomError (const errcode_t Code, PERR_CUSTOM_FUNCTION ErrFunction) {
DEFINE_FUNCTION("CErrorDatabase::AddCustomError()");
CErrorRecord* pNewRecord;
/* Ensure valid input */
ASSERT(ErrFunction != NULL);
/* Attempt to allocate the new record */
CreatePointer(pNewRecord, CErrorRecord);
/* Attempt to initialize the new record */
pNewRecord->SetCode(Code);
pNewRecord->SetFunction(ErrFunction);
/* Add the new error to the head of the linked list */
pNewRecord->SetNext(pHead);
pHead = pNewRecord;
NumErrors++;
}
/*===========================================================================
* End of Class Method CErrorDatabase::AddCustomError()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorDatabase Method - void ClearErrors (void);
*
* Deletes the entire singly linked list.
*
*=========================================================================*/
void CErrorDatabase::ClearErrors (void) {
DEFINE_FUNCTION("CErrorDatabase::ClearErrors()");
CErrorRecord* pListPtr;
/* Delete all items in the singly linked list */
while (pHead != NULL) {
pListPtr = pHead->GetNext();
DestroyPointer(pHead);
pHead = pListPtr;
}
AddedDefaultErrors = FALSE;
NumErrors = 0;
}
/*===========================================================================
* End of Class Method CErrorDatabase::ClearErrors()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorDatabase Method - CErrorRecord* Find (Code);
*
* Attempt to find the custom error record with the given error code.
* Returns NULL if the given record could not be found.
*
*=========================================================================*/
CErrorRecord* CErrorDatabase::Find (const errcode_t Code) {
DEFINE_FUNCTION("CErrorDatabase::Find()");
CErrorRecord* pSearchPtr;
/* Search the entire linked list */
for (pSearchPtr = pHead; pSearchPtr != NULL; pSearchPtr = pSearchPtr->GetNext()) {
if (pSearchPtr->GetCode() == Code) return (pSearchPtr);
}
/* No record was found */
SystemLog.DebugPrintf(_T("%s - Failed to find error record with code %ld!"), ThisFunction, Code);
return (NULL);
}
/*===========================================================================
* End of Class Method CErrorDatabase::Find()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorDatabase Method - void InitDefaultErrors (void);
*
* Adds the default custom errors the the database. Will not add the
* errors if they were previously added.
*
*=========================================================================*/
void CErrorDatabase::InitDefaultErrors (void) {
//DEFINE_FUNCTION("CErrorDatabase::InitDefaultErrors()");
/* Ignore if the default errors have already been added */
if (AddedDefaultErrors) return;
/* Add the regular custom errors */
Add (ERR_NONE, _T("No error generated."), ERRLEVEL_INFO);
Add (ERR_MEM, _T("Failed to allocate memory!"), ERRLEVEL_CRITICAL);
Add (ERR_OPENFILE, _T("Failed to open file!"), ERRLEVEL_ERROR);
Add (ERR_READFILE, _T("Failed to read from file!"), ERRLEVEL_ERROR);
Add (ERR_WRITEFILE, _T("Failed to write to file!"), ERRLEVEL_ERROR);
Add (ERR_BADARRAYINDEX,_T("Index exceeds array limits!"), ERRLEVEL_WARNING);
Add (ERR_MAXINDEX, _T("Array has reached it's maximum size!"), ERRLEVEL_WARNING);
Add (ERR_BADINPUT, _T("Invalid input was received!"), ERRLEVEL_WARNING);
Add (ERR_OVERFLOW, _T("Received input that would result in an overflow!"), ERRLEVEL_ERROR);
Add (ERR_CUSTOM, _T("Custom application error!"), ERRLEVEL_ERROR);
/* Add the system error messages */
AddCustomError (ERR_SYSTEM, SystemErrorFunction);
/* Add the graphic error messages under DOS if required */
#if defined(_TCGRAPHERRORS)
AddCustomError(ERR_TCGRAPH, TCGraphErrorFunction);
#endif
/* Add the windows error messages under Windows if required */
#if defined(_WIN32) || defined(__BCPLUSPLUS__)
AddCustomError(ERR_WINDOWS, WindowsErrorFunction);
#endif
AddedDefaultErrors = TRUE;
}
/*===========================================================================
* End of Class Method CErrorDatabase::InitDefaultErrors()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorIncident Constructor
*
*=========================================================================*/
CErrorIncident::CErrorIncident (void) {
pNext = NULL;
pMessage = NULL;
Code = ERR_NONE;
SubCode = ERR_NONE;
}
/*===========================================================================
* End of Class CErrorIncident Constructor
*=========================================================================*/
/*===========================================================================
*
* Class CErrorIncident Method - void Destroy (void);
*
* Clears the contents of the object.
*
*=========================================================================*/
void CErrorIncident::Destroy (void) {
DEFINE_FUNCTION("CErrorIncident::Destroy()");
DestroyPointer(pMessage);
Code = ERR_NONE;
SubCode = ERR_NONE;
/* Assumes that the linked list structure is maintained by the parent */
pNext = NULL;
}
/*===========================================================================
* End of Class Method CErrorIncident::Destroy()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorIncident Method - void SetMsg (pString, Args);
*
* Sets the incident message string using a variable list of arguments,
* as in the vprintf() type functions. Messages are limited to a
* size of MAX_ERROR_MESSAGESIZE bytes.
*
*=========================================================================*/
void CErrorIncident::SetMsg (const TCHAR* pString, va_list Args) {
DEFINE_FUNCTION("CErrorIncident::SetMsg(TCHAR*, va_list)");
TCHAR MessageBuffer[MAX_ERROR_MESSAGESIZE*2];
int Result;
/* Attempt to create the message string */
Result = vsnprintf(MessageBuffer, MAX_ERROR_MESSAGESIZE, pString, Args);
/* Only set message if the message was successfully created */
if (Result >= 0) ReplaceString(&pMessage, MessageBuffer);
}
/*===========================================================================
* End of Class Method CErrorIncident::SetMsg()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Constructor
*
*=========================================================================*/
CErrorHandler::CErrorHandler (CErrorDatabase& ErrDB) : refErrorDatabase(ErrDB) {
pIncidentHead = NULL;
NumErrors = 0;
pNotifyFunc = NULL;
pHookFunc = NULL;
m_LastErrorCount = 0;
}
/*===========================================================================
* End of Class CErrorHandler Constructor
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void Destroy (void);
*
* Clears the contents of the object.
*
*=========================================================================*/
void CErrorHandler::Destroy (void) {
//DEFINE_FUNCTION("CErrorHandler::Destroy()");
/* Delete the current errors */
ClearErrors();
pNotifyFunc = NULL;
pHookFunc = NULL;
}
/*===========================================================================
* End of Class Method CErrorHandler::Destroy()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void AddError (Code, pString, ...);
*
* Adds the given error code and optional message to the top of the error
* list. Throws an exception on a memory allocation error. Accepts message
* input as per the printf() type functions. Message string should be
* limited to MAX_ERROR_MESSAGESIZE characters in size. Does not ensure
* that the message is correctly written.
*
*=========================================================================*/
void CErrorHandler::AddError (const errcode_t Code, const TCHAR* pString, ...) {
//DEFINE_FUNCTION("CErrorHandler::AddError(errcode_t, TCHAR*, ...)");
va_list Args;
va_start(Args, pString);
#if defined(_WIN32)
AddErrorV(Code, GetLastError(), pString, Args);
#else
AddErrorV(Code, ERR_NONE, pString, Args);
#endif
va_end(Args);
}
/*===========================================================================
* End of Class Method CErrorHandler::AddError()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void AddError (Code, SubCode, pString, ...);
*
* Adds the given error code and optional message to the top of the error
* list. Throws an exception on a memory allocation error. Accepts message
* input as per the printf() type functions. Message string should be
* limited to MAX_ERROR_MESSAGESIZE characters in size. Does not ensure
* that the message is correctly written.
*
*=========================================================================*/
void CErrorHandler::AddError (const errcode_t Code, const errcode_t SubCode, const TCHAR* pString, ...) {
//DEFINE_FUNCTION("CErrorHandler::AddError(errcode_t, errcode_t, TCHAR*, ...)");
va_list Args;
va_start(Args, pString);
AddErrorV(Code, SubCode, pString, Args);
va_end(Args);
}
/*===========================================================================
* End of Class Method CErrorHandler::AddError()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void AddErrorV (Code, SubCode, pString, Args);
*
* Adds the given error code and optional message to the top of the error
* list. Throws an exception on a memory allocation error. Accepts message
* input as per the vprintf() type functions. Message string should be
* limited to MAX_ERROR_MESSAGESIZE characters in size. Does not ensure
* that the message is correctly written.
*
*=========================================================================*/
void CErrorHandler::AddErrorV (const errcode_t Code, const errcode_t SubCode, const TCHAR* pString, va_list Args) {
DEFINE_FUNCTION("CErrorHandler::AddErrorV(errcode_t, errcode_t, TCHAR*, va_list)");
CErrorIncident* pNewIncident;
/* Ensure we don't exceed the maximum error limit */
if (NumErrors >= MAX_ERROR_INCIDENTS) {
SystemLog.Printf(_T("Clearing %u error incidents..."), NumErrors);
ClearErrors();
}
/* Attempt to allocate a new incident object */
CreatePointer(pNewIncident, CErrorIncident);
/* Initialize the new error incident */
pNewIncident->SetCode(Code);
pNewIncident->SetSubCode(SubCode);
if (pString != NULL) pNewIncident->SetMsg(pString, Args);
/* Add error to head of incident list */
pNewIncident->SetNext(pIncidentHead);
pIncidentHead = pNewIncident;
NumErrors++;
m_LastErrorCount++;
/* Call the user's hook function if required */
if (pHookFunc != NULL) pHookFunc(pHookData, pString, Args);
/* Output the error information to the log file */
OutputLastErrorToLog();
}
/*===========================================================================
* End of Class Method CErrorHandler::AddErrorV()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void ClearErrors (void);
*
* Deletes all the errors currently in the linked list.
*
*=========================================================================*/
void CErrorHandler::ClearErrors (void) {
DEFINE_FUNCTION("CErrorHandler::ClearErrors()");
CErrorIncident* pListPtr;
/* Delete all elements in the singly linked list */
while (pIncidentHead != NULL) {
pListPtr = pIncidentHead->GetNext();
DestroyPointer(pIncidentHead);
pIncidentHead = pListPtr;
}
NumErrors = 0;
m_LastErrorCount = 0;
}
/*===========================================================================
* End of Class Method CErrorHandler::ClearErrors()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void Exit (pTitle);
*
* Displays the last error message using the Notify() method and then
* aborts the program using exit() (end with cleanup code).
*
*=========================================================================*/
void CErrorHandler::Exit (const TCHAR* pTitle) {
DEFINE_FUNCTION("CErrorHandler::Exit()");
/* Display the error message to user */
Notify(pTitle);
/* End program calling cleanup code first */
exit(EXIT_FAILURE);
}
/*===========================================================================
* End of Class Method CErrorHandler::Exit()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void _Exit (pTitle);
*
* Displays the last error message using the Notify() method and then
* aborts the program using _exit() (end without cleanup code).
*
*=========================================================================*/
void CErrorHandler::_Exit (const TCHAR* pTitle) {
DEFINE_FUNCTION("CErrorHandler::_Exit()");
/* Display the error message to user */
Notify(pTitle);
/* End program immediately without calling any cleanup code */
_exit(EXIT_FAILURE);
}
/*===========================================================================
* End of Class Method CErrorHandler::_Exit()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - CErrorIncident* GetError (Index);
*
* Attempts to return the specified error from the linked list. ASSERTs
* if the given index is invalid. The specified error record is not
* removed from the incident list (unlike the PopError() method).
*
*=========================================================================*/
CErrorIncident* CErrorHandler::GetError (const int Index) {
DEFINE_FUNCTION("CErrorHandler::GetError()");
CErrorIncident* pListPtr;
int ListCounter;
/* Ensure the input index is valid */
ASSERT(Index >= 0);
ASSERT(Index < NumErrors);
/* Move to the specified element in the list */
for (pListPtr = pIncidentHead, ListCounter = 0;
pListPtr != NULL && ListCounter < Index;
pListPtr = pListPtr->GetNext(), ListCounter++) {
}
/* Ensure a valid list element was found */
ASSERT(pListPtr != NULL);
return (pListPtr);
}
/*===========================================================================
* End of Class Method CErrorHandler::GetError()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - TCHAR* GetLastErrorMsg (void);
*
* Returns the error message of the most recent incident. If no incidents
* have been recorded, returns a standard 'no errors' type string. If the
* incident's message is NULL, it attempts to return the database
* message for that error. Always returns a valid string, never NULL.
*
*=========================================================================*/
TCHAR* CErrorHandler::GetLastErrorMsg (void) {
//DEFINE_FUNCTION("CErrorHandler::GetLastErrorMsg()");
/* Ensure there is at least one incident recorded in list */
if (NumErrors == 0) return (_T("No recorded errors."));
/* Check for a valid incident message */
if (pIncidentHead->GetMsg() != NULL) return (pIncidentHead->GetMsg());
/* Return the message from the error database */
return (GetLastErrorDBMsg());
}
/*===========================================================================
* End of Class Method CErrorHandler::GetLastErrorMsg()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - TCHAR* GetLastDBErrorMsg (void);
*
* Returns the database error message of the most recent incident. If
* no incidents have been recorded, returns a standard 'no errors' type
* string. Always returns a valid string, never NULL.
*
*=========================================================================*/
TCHAR* CErrorHandler::GetLastErrorDBMsg (void) {
//DEFINE_FUNCTION("CErrorHandler::GetLastDBErrorMsg()");
CErrorRecord* pErrRecord;
/* Ensure there is at least one incident recorded in list */
if (NumErrors == 0) return (_T("No recorded errors."));
/* Attempt to retrieve database message for error */
pErrRecord = refErrorDatabase.Find(pIncidentHead->GetCode());
if (pErrRecord != NULL) return (pErrRecord->GetMsg(pIncidentHead->GetSubCode()));
/* No error message available for the incident! */
SystemLog.Printf(_T("No message available for error %ld/%ld"), pIncidentHead->GetCode(), pIncidentHead->GetSubCode());
return (_T("No message available for the error!"));
}
/*===========================================================================
* End of Class Method CErrorHandler::GetLastDBErrorMsg()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void Notify (pTitle);
*
* Notifies the user of the most recent error. Uses the Printf() method
* to output results depending on the current compiled system.
* Nothing happens if there are no current errors in the list.
*
*=========================================================================*/
void CErrorHandler::Notify (const TCHAR* pTitle) {
//DEFINE_FUNCTION("CErrorHandler::Notify()");
CErrorRecord* pErrorRecord;
/* Ignore if there are no errors to output */
if (pIncidentHead == NULL) return;
/* Attempt to find the error record in the database of custom errors */
pErrorRecord = refErrorDatabase.Find(pIncidentHead->GetCode());
if (pErrorRecord == NULL) {
Printf(pTitle, _T("Unknown error code %ld!\n\r%s\n\r"), pIncidentHead->GetCode(),
pIncidentHead->GetMsg());
}
else {
Printf(pTitle, _T("%s\r\n%s\r\n"), pErrorRecord->GetMsg(pIncidentHead->GetSubCode()),
pIncidentHead->GetMsg());
}
}
/*===========================================================================
* End of Class Method CErrorHandler::Notify()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void Notify (pMsg, pTitle);
*
* Same as the Notify() function but outputs the given user message
* instead of the recorded error message.
*
*=========================================================================*/
void CErrorHandler::Notify (const TCHAR* pMsg, const TCHAR* pTitle) {
CErrorRecord* pErrorRecord;
/* Ignore if there are no errors to output */
if (pIncidentHead == NULL) return;
/* Attempt to find the error record in the database of custom errors */
pErrorRecord = refErrorDatabase.Find(pIncidentHead->GetCode());
if (pErrorRecord == NULL) {
Printf(pTitle, _T("Unknown error code %ld!\n\r%s\n\r"), pIncidentHead->GetCode(),
pIncidentHead->GetMsg());
}
else if (pMsg != NULL) {
Printf(pTitle, _T("%s\r\n%s\r\n"), pMsg,
pIncidentHead->GetMsg());
}
else {
Printf(pTitle, _T("%s\r\n%s\r\n"), pErrorRecord->GetMsg(pIncidentHead->GetSubCode()),
pIncidentHead->GetMsg());
}
}
/*===========================================================================
* End of Class Method CErrorHandler::Notify()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void NotifyList (pMsg, pTitle);
*
* Same as the Notify() function but outputs the given user message
* instead of the recorded error message. Outputs all the errors
* recorded by the m_LastErrorCount member. Outputs 10 errors at most.
*
*=========================================================================*/
void CErrorHandler::NotifyList (const TCHAR* pMsg, const TCHAR* pTitle) {
CErrorIncident* pError;
CErrorRecord* pErrorRecord;
TCHAR ErrorBuffer[MAX_ERROR_MESSAGESIZE+1];
int OutputErrors;
int Length;
/* Ignore if there are no errors to output */
if (pIncidentHead == NULL) return;
/* Print the error message description */
Length = snprintf(ErrorBuffer, MAX_ERROR_MESSAGESIZE, _T("%s\n\r\n\r"), pMsg);
OutputErrors = 0;
pError = pIncidentHead;
/* Output all required errors */
while (pError != NULL && OutputErrors < MAX_ERROR_NOTIFYLIST && OutputErrors < m_LastErrorCount) {
pErrorRecord = refErrorDatabase.Find(pError->GetCode());
if (pErrorRecord == NULL) {
Length += snprintf(ErrorBuffer+Length, MAX_ERROR_MESSAGESIZE-Length, _T(" %d) %s\n\r"), OutputErrors+1,
pError->GetMsg());
}
else {
Length += snprintf(ErrorBuffer+Length, MAX_ERROR_MESSAGESIZE-Length, _T(" %d) %s\n\r\t%s\n\r"), OutputErrors+1,
pError->GetMsg(), pErrorRecord->GetMsg(pError->GetSubCode()));
}
pError = pError->GetNext();
OutputErrors++;
}
/* Output the message */
Printf(pTitle, ErrorBuffer, NULL);
}
/*===========================================================================
* End of Class Method CErrorHandler::NotifyList()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void NotifyListCode (ErrCode, pMsg, pTitle);
*
* Same as the Notify() function but outputs the given user message
* instead of the recorded error message. Outputs all the errors
* recorded by the m_LastErrorCount member that have the given error code.
* Outputs 10 errors at most.
*
*=========================================================================*/
void CErrorHandler::NotifyListCode (const int ErrCode, const TCHAR* pMsg, const TCHAR* pTitle) {
CErrorIncident* pError;
CErrorRecord* pErrorRecord;
TCHAR ErrorBuffer[MAX_ERROR_MESSAGESIZE+1];
int OutputErrors;
int Length;
/* Ignore if there are no errors to output */
if (pIncidentHead == NULL) return;
/* Print the error message description */
Length = snprintf(ErrorBuffer, MAX_ERROR_MESSAGESIZE, _T("%s\n\r"), pMsg);
OutputErrors = 0;
pError = pIncidentHead;
/* Output all required errors */
while (pError != NULL && OutputErrors < MAX_ERROR_NOTIFYLIST && OutputErrors < m_LastErrorCount) {
pErrorRecord = refErrorDatabase.Find(pError->GetCode());
/* Ignore all errors that don't have the given error code */
if (pError->GetCode() != ErrCode) {
pError = pError->GetNext();
continue;
}
Length += snprintf(ErrorBuffer+Length, MAX_ERROR_MESSAGESIZE-Length, _T(" %d) %s\n\r"), OutputErrors+1,
pError->GetMsg());
pError = pError->GetNext();
OutputErrors++;
}
/* Output the message if required */
if (OutputErrors > 0) Printf(pTitle, ErrorBuffer, NULL);
}
/*===========================================================================
* End of Class Method CErrorHandler::NotifyListCode()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void NotifyListType (ErrType, pMsg, pTitle);
*
* Same as the Notify() function but outputs the given user message
* instead of the recorded error message. Outputs all the errors
* recorded by the m_LastErrorCount member that have at least the
* given error level type. Outputs 10 errors at most.
*
*=========================================================================*/
void CErrorHandler::NotifyListType (const int ErrType, const TCHAR* pMsg, const TCHAR* pTitle) {
CErrorIncident* pError;
CErrorRecord* pErrorRecord;
TCHAR ErrorBuffer[MAX_ERROR_MESSAGESIZE+1];
int OutputErrors;
int Length;
/* Ignore if there are no errors to output */
if (pIncidentHead == NULL) return;
/* Print the error message description */
Length = snprintf(ErrorBuffer, MAX_ERROR_MESSAGESIZE, _T("%s\n\r"), pMsg);
OutputErrors = 0;
pError = pIncidentHead;
/* Output all required errors */
while (pError != NULL && OutputErrors < MAX_ERROR_NOTIFYLIST && OutputErrors < m_LastErrorCount) {
pErrorRecord = refErrorDatabase.Find(pError->GetCode());
/* Ignore all errors that don't have a valid error level */
if (pErrorRecord != NULL && pErrorRecord->GetLevel() < ErrType) {
pError = pError->GetNext();
continue;
}
Length += snprintf(ErrorBuffer+Length, MAX_ERROR_MESSAGESIZE-Length, _T(" %d) %s\n\r"), OutputErrors+1,
pError->GetMsg());
pError = pError->GetNext();
OutputErrors++;
}
/* Output the message if required */
if (OutputErrors > 0) Printf(pTitle, ErrorBuffer, NULL);
}
/*===========================================================================
* End of Class Method CErrorHandler::NotifyListType()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - void OutputLastErrorToLog (void);
*
* Protectd class method which outputs the topmost error incident to the
* SystemLog.
*
*=========================================================================*/
void CErrorHandler::OutputLastErrorToLog (void) {
//DEFINE_FUNCTION("CErrorHandler::OutputLastErrorToLog()");
CErrorRecord* pErrorRecord;
/* Ensure there is a valid error in the list */
if (pIncidentHead == NULL) return;
/* Attempt to get the database entry for the error */
pErrorRecord = refErrorDatabase.Find(pIncidentHead->GetCode());
SystemLog.Printf (_T("Error (%ld / %ld): %s"), pIncidentHead->GetCode(), pIncidentHead->GetSubCode(),
(pErrorRecord == NULL) ? _T("No database entry for error found!") : pErrorRecord->GetMsg(pIncidentHead->GetSubCode()) );
SystemLog.Printf (_T("\t\t User Message: %s"), pIncidentHead->GetMsg());
}
/*===========================================================================
* End of Class Method CErrorHandler::OutputLastErrorToLog()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - CErrorIncident* PopError (void);
*
* Returns the most recently added error incident in the list, removing
* it from the incident list. Returns NULL if no errors currently exist.
*
* See Also: GetError(int), PeekError()
*
*=========================================================================*/
CErrorIncident* CErrorHandler::PopError (void) {
//DEFINE_FUNCTION("CErrorHandler::PopError()");
CErrorIncident* pLastError;
/* Check if there are any errors left to return */
if (pIncidentHead == NULL) return (NULL);
/* Remove the head from the singly linked list */
pLastError = pIncidentHead;
pIncidentHead = pLastError->GetNext();
NumErrors--;
return (pLastError);
}
/*===========================================================================
* End of Class Method CErrorHandler::PopError()
*=========================================================================*/
/*===========================================================================
*
* Class CErrorHandler Method - CErrorIncident* PeekError (void);
*
* Returns the most recently added error incident in the list.
* Returns NULL if no errors currently exist.
*
* See Also: GetError(int), PopError()
*
*=========================================================================*/
CErrorIncident* CErrorHandler::PeekError (void) {
//DEFINE_FUNCTION("CErrorHandler::PopError()");
/* Check if there are any errors left to return */
if (pIncidentHead == NULL) return (NULL);
return (pIncidentHead);
}
/*===========================================================================
* End of Class Method CErrorHandler::PeekError()