1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
3 | // See LICENSE.txt for details. |
---|
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 | { |
---|
28 | send(handlers.size() - 1, o, m, bl, w); |
---|
29 | } |
---|
30 | ~ErrorManager() { removeAll(); } |
---|
31 | }; |
---|
32 | |
---|
33 | extern THREAD_LOCAL_DECL(ErrorManager, errmgr_instance); |
---|
34 | |
---|
35 | //////////////////////////////////////// |
---|
36 | |
---|
37 | class ErrorHandlerBase |
---|
38 | { |
---|
39 | friend class ErrorManager; |
---|
40 | protected: |
---|
41 | ErrorManager* mgr; |
---|
42 | int options; |
---|
43 | |
---|
44 | public: |
---|
45 | |
---|
46 | enum HandlerOptions |
---|
47 | { |
---|
48 | DontBlock = 1, CannotBeBlocked = 2, DontEnable = 4, Paused = 8 |
---|
49 | }; |
---|
50 | |
---|
51 | void FMprintf(const char *o, const char *m, int w, const char *bl, ...); |
---|
52 | void send(const char *o, const char *m, const char *bl, int w); |
---|
53 | |
---|
54 | bool isEnabled() { return mgr ? 1 : 0; } |
---|
55 | void enable(); |
---|
56 | void disable(); |
---|
57 | bool isPaused() { return (options & Paused) != 0; } |
---|
58 | void pause(); |
---|
59 | void resume(); |
---|
60 | |
---|
61 | ErrorHandlerBase(int opts = 0) :mgr(0), options(opts) |
---|
62 | { |
---|
63 | if (!(options&DontEnable)) enable(); |
---|
64 | } |
---|
65 | virtual ~ErrorHandlerBase() |
---|
66 | { |
---|
67 | disable(); |
---|
68 | } |
---|
69 | |
---|
70 | virtual void handle(const char *o, const char *m, const char *bl, int w) {} |
---|
71 | }; |
---|
72 | |
---|
73 | /////////////////////////////////////////// |
---|
74 | |
---|
75 | class ErrorHandler : public ErrorHandlerBase |
---|
76 | { |
---|
77 | protected: |
---|
78 | int maxlevel, errcount, warncount, storlevel, storcount, infocount; |
---|
79 | SString msgs; |
---|
80 | |
---|
81 | public: |
---|
82 | |
---|
83 | void reset() { maxlevel = FMLV_INFO - 1; errcount = warncount = storcount = infocount = 0; msgs = 0; } |
---|
84 | |
---|
85 | enum Options2 |
---|
86 | { |
---|
87 | StoreFirstMessage = 16, StoreAllMessages = 32 |
---|
88 | }; |
---|
89 | |
---|
90 | int getErrorCount() { return errcount; } |
---|
91 | int getWarningCount() { return warncount; } |
---|
92 | int getInfoCount() { return infocount; } |
---|
93 | int getStoredCount() { return storcount; } |
---|
94 | int getErrorLevel() { return maxlevel; } |
---|
95 | const SString& getMessages() { return msgs; } |
---|
96 | |
---|
97 | ErrorHandler(int opts = 0, int store = FMLV_ERROR) :ErrorHandlerBase(opts), storlevel(store) |
---|
98 | { |
---|
99 | reset(); |
---|
100 | } |
---|
101 | |
---|
102 | void handle(const char *o, const char *m, const char *bl, int w); |
---|
103 | }; |
---|
104 | |
---|
105 | class ErrorRedirector : public ErrorHandlerBase |
---|
106 | { |
---|
107 | ErrorManager *other_mgr; |
---|
108 | public: |
---|
109 | ErrorRedirector(ErrorManager *om) |
---|
110 | :ErrorHandlerBase(), other_mgr(om) {} |
---|
111 | |
---|
112 | void handle(const char *o, const char *m, const char *bl, int w) |
---|
113 | { |
---|
114 | other_mgr->send(o, m, bl, w); |
---|
115 | } |
---|
116 | }; |
---|
117 | |
---|
118 | #endif |
---|