[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 | |
---|
[375] | 5 | #ifndef _FRAMS_LOGGERS_H_ |
---|
| 6 | #define _FRAMS_LOGGERS_H_ |
---|
[109] | 7 | |
---|
| 8 | #include <frams/util/list.h> |
---|
| 9 | #include <frams/util/sstring.h> |
---|
[375] | 10 | #include <common/log.h> |
---|
[109] | 11 | #include <common/threads.h> |
---|
| 12 | |
---|
[375] | 13 | class LoggerBase; |
---|
[109] | 14 | |
---|
[375] | 15 | class LoggerManager |
---|
[109] | 16 | { |
---|
[375] | 17 | friend class LoggerBase; |
---|
| 18 | SListTempl<LoggerBase*> handlers; |
---|
| 19 | void send(int level, const char *o, const char *m, int w, const char *bl); |
---|
[336] | 20 | public: |
---|
[375] | 21 | int find(LoggerBase *r) { return handlers.find(r); } |
---|
| 22 | int add(LoggerBase *r); |
---|
[336] | 23 | void remove(int i); |
---|
[375] | 24 | void remove(LoggerBase *r); |
---|
[336] | 25 | void removeAll(); |
---|
[375] | 26 | void send(const char *o, const char *m, int w, const char *bl) |
---|
[336] | 27 | { |
---|
[375] | 28 | send(handlers.size() - 1, o, m, w, bl); |
---|
[336] | 29 | } |
---|
[375] | 30 | ~LoggerManager() { removeAll(); } |
---|
[109] | 31 | }; |
---|
| 32 | |
---|
[375] | 33 | extern THREAD_LOCAL_DECL(LoggerManager, message_handler_manager_instance); |
---|
[109] | 34 | |
---|
| 35 | //////////////////////////////////////// |
---|
| 36 | |
---|
[375] | 37 | class LoggerBase |
---|
[109] | 38 | { |
---|
[375] | 39 | friend class LoggerManager; |
---|
[336] | 40 | protected: |
---|
[375] | 41 | LoggerManager* manager; |
---|
[336] | 42 | int options; |
---|
[109] | 43 | |
---|
[336] | 44 | public: |
---|
[109] | 45 | |
---|
[375] | 46 | enum LoggerOptions |
---|
[336] | 47 | { |
---|
[372] | 48 | DontBlock = 1, CannotBeBlocked = 2, Enable = 4, Paused = 8 |
---|
[336] | 49 | }; |
---|
[109] | 50 | |
---|
[375] | 51 | void logPrintf(const char *o, const char *m, int w, const char *bl, ...); |
---|
| 52 | void send(const char *o, const char *m, int w, const char *bl); |
---|
[109] | 53 | |
---|
[373] | 54 | bool isEnabled() { return manager ? true : false; } |
---|
[336] | 55 | void enable(); |
---|
| 56 | void disable(); |
---|
| 57 | bool isPaused() { return (options & Paused) != 0; } |
---|
| 58 | void pause(); |
---|
| 59 | void resume(); |
---|
[109] | 60 | |
---|
[375] | 61 | LoggerBase(int opts = 0) :manager(NULL), options(opts) |
---|
[336] | 62 | { |
---|
[372] | 63 | if (options&Enable) enable(); |
---|
[336] | 64 | } |
---|
[375] | 65 | virtual ~LoggerBase() |
---|
[336] | 66 | { |
---|
| 67 | disable(); |
---|
| 68 | } |
---|
[109] | 69 | |
---|
[375] | 70 | virtual void handle(const char *o, const char *m, int w, const char *bl) {} |
---|
[109] | 71 | }; |
---|
| 72 | |
---|
| 73 | /////////////////////////////////////////// |
---|
| 74 | |
---|
[375] | 75 | class LoggerToMemory : public LoggerBase |
---|
[109] | 76 | { |
---|
[336] | 77 | protected: |
---|
[373] | 78 | int maxlevel, minleveltostore, errcount, warncount, storedcount, infocount; |
---|
[336] | 79 | SString msgs; |
---|
[109] | 80 | |
---|
[336] | 81 | public: |
---|
[109] | 82 | |
---|
[375] | 83 | void reset() { maxlevel = LOG_INFO - 1; errcount = warncount = storedcount = 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; } |
---|
[373] | 93 | int getStoredCount() { return storedcount; } |
---|
[336] | 94 | int getErrorLevel() { return maxlevel; } |
---|
| 95 | const SString& getMessages() { return msgs; } |
---|
[109] | 96 | |
---|
[375] | 97 | LoggerToMemory(int opts = 0, int minimal_level_to_store = LOG_ERROR) :LoggerBase(opts), minleveltostore(minimal_level_to_store) |
---|
[336] | 98 | { |
---|
| 99 | reset(); |
---|
| 100 | } |
---|
[109] | 101 | |
---|
[375] | 102 | void handle(const char *o, const char *m, int w, const char *bl); |
---|
[109] | 103 | }; |
---|
| 104 | |
---|
[375] | 105 | class RedirectingLogger : public LoggerBase |
---|
[109] | 106 | { |
---|
[375] | 107 | LoggerManager *other_manager; |
---|
[109] | 108 | public: |
---|
[375] | 109 | RedirectingLogger(LoggerManager *other_mgr,int opts=0) |
---|
| 110 | :LoggerBase(opts), other_manager(other_mgr) {} |
---|
[109] | 111 | |
---|
[375] | 112 | void handle(const char *o, const char *m, int w, const char *bl) |
---|
[336] | 113 | { |
---|
[375] | 114 | other_manager->send(o, m, w, bl); |
---|
[336] | 115 | } |
---|
[109] | 116 | }; |
---|
| 117 | |
---|
| 118 | #endif |
---|