[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
[876] | 2 | // Copyright (C) 1999-2019 Maciej Komosinski and Szymon Ulatowski. |
---|
[286] | 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
[378] | 5 | #ifndef _COMMON_LOGGERS_H_ |
---|
| 6 | #define _COMMON_LOGGERS_H_ |
---|
[109] | 7 | |
---|
[375] | 8 | #include <common/log.h> |
---|
[109] | 9 | #include <common/threads.h> |
---|
[378] | 10 | #include <common/nonstd_stl.h> |
---|
[109] | 11 | |
---|
[375] | 12 | class LoggerBase; |
---|
[109] | 13 | |
---|
[375] | 14 | class LoggerManager |
---|
[109] | 15 | { |
---|
[375] | 16 | friend class LoggerBase; |
---|
[378] | 17 | vector<LoggerBase*> loggers; |
---|
| 18 | void send(int position, const char *obj, const char *method, int level, const char *msg); |
---|
[336] | 19 | public: |
---|
[378] | 20 | int find(LoggerBase *logger) |
---|
| 21 | { |
---|
| 22 | vector<LoggerBase*>::iterator it = std::find(loggers.begin(), loggers.end(), logger); |
---|
| 23 | return it == loggers.end() ? -1 : std::distance(loggers.begin(), it); |
---|
| 24 | } |
---|
| 25 | int add(LoggerBase *logger); |
---|
[336] | 26 | void remove(int i); |
---|
[378] | 27 | void remove(LoggerBase *logger); |
---|
[336] | 28 | void removeAll(); |
---|
[378] | 29 | void send(const char *obj, const char *method, int level, const char *msg) |
---|
[336] | 30 | { |
---|
[1075] | 31 | send(int(loggers.size())-1, obj, method, level, msg); |
---|
[336] | 32 | } |
---|
[375] | 33 | ~LoggerManager() { removeAll(); } |
---|
[109] | 34 | }; |
---|
| 35 | |
---|
[375] | 36 | extern THREAD_LOCAL_DECL(LoggerManager, message_handler_manager_instance); |
---|
[109] | 37 | |
---|
| 38 | //////////////////////////////////////// |
---|
| 39 | |
---|
[375] | 40 | class LoggerBase |
---|
[109] | 41 | { |
---|
[375] | 42 | friend class LoggerManager; |
---|
[336] | 43 | protected: |
---|
[375] | 44 | LoggerManager* manager; |
---|
[336] | 45 | int options; |
---|
[109] | 46 | |
---|
[336] | 47 | public: |
---|
[109] | 48 | |
---|
[375] | 49 | enum LoggerOptions |
---|
[336] | 50 | { |
---|
[372] | 51 | DontBlock = 1, CannotBeBlocked = 2, Enable = 4, Paused = 8 |
---|
[336] | 52 | }; |
---|
[109] | 53 | |
---|
[375] | 54 | void logPrintf(const char *o, const char *m, int w, const char *bl, ...); |
---|
[378] | 55 | void send(const char *obj, const char *method, int level, const char *msg); |
---|
[109] | 56 | |
---|
[1082] | 57 | int getOptions() { return options; } |
---|
[373] | 58 | bool isEnabled() { return manager ? true : false; } |
---|
[336] | 59 | void enable(); |
---|
| 60 | void disable(); |
---|
| 61 | bool isPaused() { return (options & Paused) != 0; } |
---|
| 62 | void pause(); |
---|
| 63 | void resume(); |
---|
[109] | 64 | |
---|
[375] | 65 | LoggerBase(int opts = 0) :manager(NULL), options(opts) |
---|
[336] | 66 | { |
---|
[372] | 67 | if (options&Enable) enable(); |
---|
[336] | 68 | } |
---|
[375] | 69 | virtual ~LoggerBase() |
---|
[336] | 70 | { |
---|
| 71 | disable(); |
---|
| 72 | } |
---|
[109] | 73 | |
---|
[511] | 74 | virtual void handle(const char *obj, const char *method, int level, const char *msg); ///< implemented by loggers accepting multiline messages. if not implemented, the default handle() splits multiline text into single lines and calls handleSingleLine() |
---|
| 75 | virtual void handleSingleLine(const char *obj, const char *method, int level, const char *msg) {} ///< implemented by loggers expecting single line messages |
---|
[109] | 76 | }; |
---|
| 77 | |
---|
| 78 | /////////////////////////////////////////// |
---|
| 79 | |
---|
[375] | 80 | class LoggerToMemory : public LoggerBase |
---|
[109] | 81 | { |
---|
[336] | 82 | protected: |
---|
[378] | 83 | int maxlevel, minleveltostore, errcount, warncount, infocount, storedcount; |
---|
| 84 | string msgs; |
---|
[109] | 85 | |
---|
[336] | 86 | public: |
---|
[109] | 87 | |
---|
[876] | 88 | void reset() { maxlevel = LOG_MIN - 1; errcount = warncount = infocount = storedcount = 0; msgs = ""; } |
---|
[109] | 89 | |
---|
[336] | 90 | enum Options2 |
---|
| 91 | { |
---|
| 92 | StoreFirstMessage = 16, StoreAllMessages = 32 |
---|
| 93 | }; |
---|
[109] | 94 | |
---|
[452] | 95 | int getErrorCount() const { return errcount; } |
---|
| 96 | int getWarningCount() const { return warncount; } |
---|
| 97 | int getInfoCount() const { return infocount; } |
---|
| 98 | int getStoredCount() const { return storedcount; } |
---|
| 99 | int getErrorLevel() const { return maxlevel; } |
---|
| 100 | string getMessages() const { return msgs; } |
---|
| 101 | string getCountSummary() const; ///< return the standard "... error(s), ... warning(s), ... message(s)" text (or empty string if count==0) |
---|
[1082] | 102 | void setStoreOptions(int opts) {options=options&(~(StoreFirstMessage|StoreAllMessages))|(opts&(StoreFirstMessage|StoreAllMessages));} |
---|
| 103 | |
---|
[375] | 104 | LoggerToMemory(int opts = 0, int minimal_level_to_store = LOG_ERROR) :LoggerBase(opts), minleveltostore(minimal_level_to_store) |
---|
[336] | 105 | { |
---|
| 106 | reset(); |
---|
| 107 | } |
---|
[109] | 108 | |
---|
[378] | 109 | void handle(const char *obj, const char *method, int level, const char *msg); |
---|
[109] | 110 | }; |
---|
| 111 | |
---|
[375] | 112 | class RedirectingLogger : public LoggerBase |
---|
[109] | 113 | { |
---|
[375] | 114 | LoggerManager *other_manager; |
---|
[109] | 115 | public: |
---|
[378] | 116 | RedirectingLogger(LoggerManager *other_mgr, int opts = 0) |
---|
[375] | 117 | :LoggerBase(opts), other_manager(other_mgr) {} |
---|
[109] | 118 | |
---|
[378] | 119 | void handle(const char *obj, const char *method, int level, const char *msg) |
---|
[336] | 120 | { |
---|
[378] | 121 | other_manager->send(obj, method, level, msg); |
---|
[336] | 122 | } |
---|
[109] | 123 | }; |
---|
| 124 | |
---|
| 125 | #endif |
---|