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. |
---|
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 | |
---|
12 | class LoggerBase; |
---|
13 | |
---|
14 | class LoggerManager |
---|
15 | { |
---|
16 | friend class LoggerBase; |
---|
17 | vector<LoggerBase*> loggers; |
---|
18 | void send(int position, const char *obj, const char *method, int level, const char *msg); |
---|
19 | public: |
---|
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); |
---|
26 | void remove(int i); |
---|
27 | void remove(LoggerBase *logger); |
---|
28 | void removeAll(); |
---|
29 | void send(const char *obj, const char *method, int level, const char *msg) |
---|
30 | { |
---|
31 | send(loggers.size() - 1, obj, method, level, msg); |
---|
32 | } |
---|
33 | ~LoggerManager() { removeAll(); } |
---|
34 | }; |
---|
35 | |
---|
36 | extern THREAD_LOCAL_DECL(LoggerManager, message_handler_manager_instance); |
---|
37 | |
---|
38 | //////////////////////////////////////// |
---|
39 | |
---|
40 | class LoggerBase |
---|
41 | { |
---|
42 | friend class LoggerManager; |
---|
43 | protected: |
---|
44 | LoggerManager* manager; |
---|
45 | int options; |
---|
46 | |
---|
47 | public: |
---|
48 | |
---|
49 | enum LoggerOptions |
---|
50 | { |
---|
51 | DontBlock = 1, CannotBeBlocked = 2, Enable = 4, Paused = 8 |
---|
52 | }; |
---|
53 | |
---|
54 | void logPrintf(const char *o, const char *m, int w, const char *bl, ...); |
---|
55 | void send(const char *obj, const char *method, int level, const char *msg); |
---|
56 | |
---|
57 | bool isEnabled() { return manager ? true : false; } |
---|
58 | void enable(); |
---|
59 | void disable(); |
---|
60 | bool isPaused() { return (options & Paused) != 0; } |
---|
61 | void pause(); |
---|
62 | void resume(); |
---|
63 | |
---|
64 | LoggerBase(int opts = 0) :manager(NULL), options(opts) |
---|
65 | { |
---|
66 | if (options&Enable) enable(); |
---|
67 | } |
---|
68 | virtual ~LoggerBase() |
---|
69 | { |
---|
70 | disable(); |
---|
71 | } |
---|
72 | |
---|
73 | virtual void handle(const char *obj, const char *method, int level, const char *msg) {} |
---|
74 | }; |
---|
75 | |
---|
76 | /////////////////////////////////////////// |
---|
77 | |
---|
78 | class LoggerToMemory : public LoggerBase |
---|
79 | { |
---|
80 | protected: |
---|
81 | int maxlevel, minleveltostore, errcount, warncount, infocount, storedcount; |
---|
82 | string msgs; |
---|
83 | |
---|
84 | public: |
---|
85 | |
---|
86 | void reset() { maxlevel = LOG_INFO - 1; errcount = warncount = infocount = storedcount = 0; msgs = ""; } |
---|
87 | |
---|
88 | enum Options2 |
---|
89 | { |
---|
90 | StoreFirstMessage = 16, StoreAllMessages = 32 |
---|
91 | }; |
---|
92 | |
---|
93 | int getErrorCount() { return errcount; } |
---|
94 | int getWarningCount() { return warncount; } |
---|
95 | int getInfoCount() { return infocount; } |
---|
96 | int getStoredCount() { return storedcount; } |
---|
97 | int getErrorLevel() { return maxlevel; } |
---|
98 | const string getMessages() { return msgs; } |
---|
99 | |
---|
100 | LoggerToMemory(int opts = 0, int minimal_level_to_store = LOG_ERROR) :LoggerBase(opts), minleveltostore(minimal_level_to_store) |
---|
101 | { |
---|
102 | reset(); |
---|
103 | } |
---|
104 | |
---|
105 | void handle(const char *obj, const char *method, int level, const char *msg); |
---|
106 | }; |
---|
107 | |
---|
108 | class RedirectingLogger : public LoggerBase |
---|
109 | { |
---|
110 | LoggerManager *other_manager; |
---|
111 | public: |
---|
112 | RedirectingLogger(LoggerManager *other_mgr, int opts = 0) |
---|
113 | :LoggerBase(opts), other_manager(other_mgr) {} |
---|
114 | |
---|
115 | void handle(const char *obj, const char *method, int level, const char *msg) |
---|
116 | { |
---|
117 | other_manager->send(obj, method, level, msg); |
---|
118 | } |
---|
119 | }; |
---|
120 | |
---|
121 | #endif |
---|