[286] | 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. |
---|
[109] | 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 | { |
---|
[336] | 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(); } |
---|
[109] | 31 | }; |
---|
| 32 | |
---|
[336] | 33 | extern THREAD_LOCAL_DECL(ErrorManager, errmgr_instance); |
---|
[109] | 34 | |
---|
| 35 | //////////////////////////////////////// |
---|
| 36 | |
---|
| 37 | class ErrorHandlerBase |
---|
| 38 | { |
---|
[336] | 39 | friend class ErrorManager; |
---|
| 40 | protected: |
---|
| 41 | ErrorManager* mgr; |
---|
| 42 | int options; |
---|
[109] | 43 | |
---|
[336] | 44 | public: |
---|
[109] | 45 | |
---|
[336] | 46 | enum HandlerOptions |
---|
| 47 | { |
---|
| 48 | DontBlock = 1, CannotBeBlocked = 2, DontEnable = 4, Paused = 8 |
---|
| 49 | }; |
---|
[109] | 50 | |
---|
[336] | 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); |
---|
[109] | 53 | |
---|
[336] | 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(); |
---|
[109] | 60 | |
---|
[336] | 61 | ErrorHandlerBase(int opts = 0) :mgr(0), options(opts) |
---|
| 62 | { |
---|
| 63 | if (!(options&DontEnable)) enable(); |
---|
| 64 | } |
---|
| 65 | virtual ~ErrorHandlerBase() |
---|
| 66 | { |
---|
| 67 | disable(); |
---|
| 68 | } |
---|
[109] | 69 | |
---|
[336] | 70 | virtual void handle(const char *o, const char *m, const char *bl, int w) {} |
---|
[109] | 71 | }; |
---|
| 72 | |
---|
| 73 | /////////////////////////////////////////// |
---|
| 74 | |
---|
[336] | 75 | class ErrorHandler : public ErrorHandlerBase |
---|
[109] | 76 | { |
---|
[336] | 77 | protected: |
---|
| 78 | int maxlevel, errcount, warncount, storlevel, storcount, infocount; |
---|
| 79 | SString msgs; |
---|
[109] | 80 | |
---|
[336] | 81 | public: |
---|
[109] | 82 | |
---|
[336] | 83 | void reset() { maxlevel = FMLV_INFO - 1; errcount = warncount = storcount = infocount = 0; msgs = 0; } |
---|
[109] | 84 | |
---|
[336] | 85 | enum Options2 |
---|
| 86 | { |
---|
| 87 | StoreFirstMessage = 16, StoreAllMessages = 32 |
---|
| 88 | }; |
---|
[109] | 89 | |
---|
[336] | 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; } |
---|
[109] | 96 | |
---|
[336] | 97 | ErrorHandler(int opts = 0, int store = FMLV_ERROR) :ErrorHandlerBase(opts), storlevel(store) |
---|
| 98 | { |
---|
| 99 | reset(); |
---|
| 100 | } |
---|
[109] | 101 | |
---|
[336] | 102 | void handle(const char *o, const char *m, const char *bl, int w); |
---|
[109] | 103 | }; |
---|
| 104 | |
---|
[336] | 105 | class ErrorRedirector : public ErrorHandlerBase |
---|
[109] | 106 | { |
---|
| 107 | ErrorManager *other_mgr; |
---|
| 108 | public: |
---|
| 109 | ErrorRedirector(ErrorManager *om) |
---|
[336] | 110 | :ErrorHandlerBase(), other_mgr(om) {} |
---|
[109] | 111 | |
---|
[336] | 112 | void handle(const char *o, const char *m, const char *bl, int w) |
---|
| 113 | { |
---|
| 114 | other_mgr->send(o, m, bl, w); |
---|
| 115 | } |
---|
[109] | 116 | }; |
---|
| 117 | |
---|
| 118 | #endif |
---|