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