[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 | |
---|
[372] | 5 | #ifndef _MHANDLERS_H_ |
---|
| 6 | #define _MHANDLERS_H_ |
---|
[109] | 7 | |
---|
| 8 | #include <frams/util/list.h> |
---|
| 9 | #include <frams/util/sstring.h> |
---|
[372] | 10 | #include <common/hmessage.h> |
---|
[109] | 11 | #include <common/threads.h> |
---|
| 12 | |
---|
[372] | 13 | class MessageHandlerBase; |
---|
[109] | 14 | |
---|
[372] | 15 | class MessageHandlerManager |
---|
[109] | 16 | { |
---|
[372] | 17 | friend class MessageHandlerBase; |
---|
| 18 | SListTempl<MessageHandlerBase*> handlers; |
---|
[336] | 19 | void send(int level, const char *o, const char *m, const char *bl, int w); |
---|
| 20 | public: |
---|
[372] | 21 | int find(MessageHandlerBase *r) { return handlers.find(r); } |
---|
| 22 | int add(MessageHandlerBase *r); |
---|
[336] | 23 | void remove(int i); |
---|
[372] | 24 | void remove(MessageHandlerBase *r); |
---|
[336] | 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 | } |
---|
[372] | 30 | ~MessageHandlerManager() { removeAll(); } |
---|
[109] | 31 | }; |
---|
| 32 | |
---|
[372] | 33 | extern THREAD_LOCAL_DECL(MessageHandlerManager, message_handler_manager_instance); |
---|
[109] | 34 | |
---|
| 35 | //////////////////////////////////////// |
---|
| 36 | |
---|
[372] | 37 | class MessageHandlerBase |
---|
[109] | 38 | { |
---|
[372] | 39 | friend class MessageHandlerManager; |
---|
[336] | 40 | protected: |
---|
[372] | 41 | MessageHandlerManager* mgr; |
---|
[336] | 42 | int options; |
---|
[109] | 43 | |
---|
[336] | 44 | public: |
---|
[109] | 45 | |
---|
[336] | 46 | enum HandlerOptions |
---|
| 47 | { |
---|
[372] | 48 | DontBlock = 1, CannotBeBlocked = 2, Enable = 4, Paused = 8 |
---|
[336] | 49 | }; |
---|
[109] | 50 | |
---|
[372] | 51 | void Hprintf(const char *o, const char *m, int w, const char *bl, ...); |
---|
[336] | 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 | |
---|
[372] | 61 | MessageHandlerBase(int opts = 0) :mgr(0), options(opts) |
---|
[336] | 62 | { |
---|
[372] | 63 | if (options&Enable) enable(); |
---|
[336] | 64 | } |
---|
[372] | 65 | virtual ~MessageHandlerBase() |
---|
[336] | 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 | |
---|
[372] | 75 | class MessageHandlerToMemory : public MessageHandlerBase |
---|
[109] | 76 | { |
---|
[336] | 77 | protected: |
---|
| 78 | int maxlevel, errcount, warncount, storlevel, storcount, infocount; |
---|
| 79 | SString msgs; |
---|
[109] | 80 | |
---|
[336] | 81 | public: |
---|
[109] | 82 | |
---|
[372] | 83 | void reset() { maxlevel = HMLV_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 | |
---|
[372] | 97 | MessageHandlerToMemory(int opts = 0, int minimal_level_to_store = HMLV_ERROR) :MessageHandlerBase(opts), storlevel(minimal_level_to_store) |
---|
[336] | 98 | { |
---|
| 99 | reset(); |
---|
| 100 | } |
---|
[109] | 101 | |
---|
[336] | 102 | void handle(const char *o, const char *m, const char *bl, int w); |
---|
[109] | 103 | }; |
---|
| 104 | |
---|
[372] | 105 | class RedirectingMessageHandler : public MessageHandlerBase |
---|
[109] | 106 | { |
---|
[372] | 107 | MessageHandlerManager *other_mgr; |
---|
[109] | 108 | public: |
---|
[372] | 109 | RedirectingMessageHandler(MessageHandlerManager *om,int opts=0) |
---|
| 110 | :MessageHandlerBase(opts), 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 |
---|