1 | // This file is a part of the Framsticks GDK. |
---|
2 | // Copyright (C) 1999-2014 Maciej Komosinski and 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 <frams/util/list.h> |
---|
9 | #include <frams/util/sstring.h> |
---|
10 | #include <common/framsg.h> |
---|
11 | #include <common/threads.h> |
---|
12 | |
---|
13 | class ErrorHandlerBase; |
---|
14 | |
---|
15 | class ErrorManager |
---|
16 | { |
---|
17 | friend class ErrorHandlerBase; |
---|
18 | SListTempl<ErrorHandlerBase*> handlers; |
---|
19 | void send(int level,const char *o,const char *m,const char *bl,int w); |
---|
20 | public: |
---|
21 | int find(ErrorHandlerBase *r) {return handlers.find(r);} |
---|
22 | int add(ErrorHandlerBase *r); |
---|
23 | void remove(int i); |
---|
24 | void remove(ErrorHandlerBase *r); |
---|
25 | void removeAll(); |
---|
26 | void send(const char *o,const char *m,const char *bl,int w) |
---|
27 | {send(handlers.size()-1,o,m,bl,w);} |
---|
28 | ~ErrorManager() {removeAll();} |
---|
29 | }; |
---|
30 | |
---|
31 | extern THREAD_LOCAL_DECL(ErrorManager,errmgr_instance); |
---|
32 | |
---|
33 | //////////////////////////////////////// |
---|
34 | |
---|
35 | class ErrorHandlerBase |
---|
36 | { |
---|
37 | friend class ErrorManager; |
---|
38 | protected: |
---|
39 | ErrorManager* mgr; |
---|
40 | int options; |
---|
41 | |
---|
42 | public: |
---|
43 | |
---|
44 | enum HandlerOptions |
---|
45 | { DontBlock=1, CannotBeBlocked=2, DontEnable=4 }; |
---|
46 | |
---|
47 | void FMprintf(const char *o,const char *m,int w,const char *bl, ...); |
---|
48 | void send(const char *o,const char *m,const char *bl,int w); |
---|
49 | |
---|
50 | bool isEnabled() {return mgr?1:0;} |
---|
51 | void enable(); |
---|
52 | void disable(); |
---|
53 | |
---|
54 | ErrorHandlerBase(int opts=0):mgr(0),options(opts) |
---|
55 | {if (!(options&DontEnable)) enable();} |
---|
56 | virtual ~ErrorHandlerBase() |
---|
57 | {disable();} |
---|
58 | |
---|
59 | virtual void handle(const char *o,const char *m,const char *bl,int w) {} |
---|
60 | }; |
---|
61 | |
---|
62 | /////////////////////////////////////////// |
---|
63 | |
---|
64 | class ErrorHandler: public ErrorHandlerBase |
---|
65 | { |
---|
66 | protected: |
---|
67 | int maxlevel,errcount,warncount,storlevel,storcount,infocount; |
---|
68 | SString msgs; |
---|
69 | |
---|
70 | public: |
---|
71 | |
---|
72 | void reset() {maxlevel=FMLV_INFO-1; errcount=warncount=storcount=infocount=0; msgs=0;} |
---|
73 | |
---|
74 | enum Options2 |
---|
75 | { StoreFirstMessage=8, StoreAllMessages=16 }; |
---|
76 | |
---|
77 | int getErrorCount() {return errcount;} |
---|
78 | int getWarningCount() {return warncount;} |
---|
79 | int getInfoCount() {return infocount;} |
---|
80 | int getStoredCount() {return storcount;} |
---|
81 | int getErrorLevel() {return maxlevel;} |
---|
82 | const SString& getMessages() {return msgs;} |
---|
83 | |
---|
84 | ErrorHandler(int opts=0,int store=FMLV_ERROR):ErrorHandlerBase(opts),storlevel(store) |
---|
85 | {reset();} |
---|
86 | |
---|
87 | void handle(const char *o,const char *m,const char *bl,int w); |
---|
88 | }; |
---|
89 | |
---|
90 | class ErrorRedirector: public ErrorHandlerBase |
---|
91 | { |
---|
92 | ErrorManager *other_mgr; |
---|
93 | public: |
---|
94 | ErrorRedirector(ErrorManager *om) |
---|
95 | :ErrorHandlerBase(),other_mgr(om) {} |
---|
96 | |
---|
97 | void handle(const char *o,const char *m,const char *bl,int w) |
---|
98 | { |
---|
99 | other_mgr->send(o,m,bl,w); |
---|
100 | } |
---|
101 | }; |
---|
102 | |
---|
103 | #endif |
---|
104 | |
---|