1 | // This file is a part of the Framsticks GDK library. |
---|
2 | // Copyright (C) 2002-2011 Szymon Ulatowski. See LICENSE.txt for details. |
---|
3 | // Refer to http://www.framsticks.com/ for further information. |
---|
4 | |
---|
5 | #ifndef _ERRMANAGER_H_ |
---|
6 | #define _ERRMANAGER_H_ |
---|
7 | |
---|
8 | #include "list.h" |
---|
9 | #include "sstring.h" |
---|
10 | #include "framsg.h" |
---|
11 | |
---|
12 | class ErrorHandlerBase; |
---|
13 | |
---|
14 | class ErrorManager |
---|
15 | { |
---|
16 | friend class ErrorHandlerBase; |
---|
17 | SListTempl<ErrorHandlerBase*> handlers; |
---|
18 | void send(int level,const char *o,const char *m,const char *bl,int w); |
---|
19 | public: |
---|
20 | int find(ErrorHandlerBase *r) {return handlers.find(r);} |
---|
21 | int add(ErrorHandlerBase *r); |
---|
22 | void remove(int i); |
---|
23 | void remove(ErrorHandlerBase *r); |
---|
24 | void removeAll(); |
---|
25 | void send(const char *o,const char *m,const char *bl,int w) |
---|
26 | {send(handlers.size()-1,o,m,bl,w);} |
---|
27 | ~ErrorManager() {removeAll();} |
---|
28 | }; |
---|
29 | |
---|
30 | extern ErrorManager globalErrorManager; |
---|
31 | |
---|
32 | //////////////////////////////////////// |
---|
33 | |
---|
34 | class ErrorHandlerBase |
---|
35 | { |
---|
36 | friend class ErrorManager; |
---|
37 | protected: |
---|
38 | ErrorManager* mgr; |
---|
39 | int options; |
---|
40 | |
---|
41 | public: |
---|
42 | |
---|
43 | enum HandlerOptions |
---|
44 | { DontBlock=1, CannotBeBlocked=2, DontEnable=4 }; |
---|
45 | |
---|
46 | void FMprintf(const char *o,const char *m,int w,const char *bl, ...); |
---|
47 | void send(const char *o,const char *m,const char *bl,int w); |
---|
48 | |
---|
49 | bool isEnabled() {return mgr?1:0;} |
---|
50 | void enable(); |
---|
51 | void disable(); |
---|
52 | |
---|
53 | ErrorHandlerBase(int opts=0):options(opts),mgr(0) |
---|
54 | {if (!(options&DontEnable)) enable();} |
---|
55 | ~ErrorHandlerBase() |
---|
56 | {disable();} |
---|
57 | |
---|
58 | virtual void handle(const char *o,const char *m,const char *bl,int w) {} |
---|
59 | }; |
---|
60 | |
---|
61 | /////////////////////////////////////////// |
---|
62 | |
---|
63 | class ErrorHandler: public ErrorHandlerBase |
---|
64 | { |
---|
65 | protected: |
---|
66 | int maxlevel,errcount,warncount,storlevel,storcount,infocount; |
---|
67 | SString msgs; |
---|
68 | |
---|
69 | public: |
---|
70 | |
---|
71 | void reset() {maxlevel=FMLV_INFO-1; errcount=warncount=storcount=infocount=0; msgs=0;} |
---|
72 | |
---|
73 | enum Options2 |
---|
74 | { StoreFirstMessage=8, StoreAllMessages=16 }; |
---|
75 | |
---|
76 | int getErrorCount() {return errcount;} |
---|
77 | int getWarningCount() {return warncount;} |
---|
78 | int getInfoCount() {return infocount;} |
---|
79 | int getStoredCount() {return storcount;} |
---|
80 | int getErrorLevel() {return maxlevel;} |
---|
81 | const SString& getMessages() {return msgs;} |
---|
82 | |
---|
83 | ErrorHandler(int opts=0,int store=FMLV_ERROR):ErrorHandlerBase(opts),storlevel(store) |
---|
84 | {reset();} |
---|
85 | |
---|
86 | void handle(const char *o,const char *m,const char *bl,int w); |
---|
87 | }; |
---|
88 | |
---|
89 | #endif |
---|
90 | |
---|