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