source: cpp/common/loggers/loggers.h @ 1197

Last change on this file since 1197 was 1130, checked in by Maciej Komosinski, 4 years ago

Used std::min(), std::max() explicitly to avoid compiler confusion. Used std::size() explicitly instead of the equivalent macro

  • Property svn:eol-style set to native
File size: 3.7 KB
Line 
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
13class LoggerBase;
14
15class 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);
20public:
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
37extern THREAD_LOCAL_DECL(LoggerManager, message_handler_manager_instance);
38
39////////////////////////////////////////
40
41class LoggerBase
42{
43        friend class LoggerManager;
44protected:
45        LoggerManager* manager;
46        int options;
47
48public:
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
81class LoggerToMemory : public LoggerBase
82{
83protected:
84        int maxlevel, minleveltostore, errcount, warncount, infocount, storedcount;
85        string msgs;
86
87public:
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 getWarningCount() const { return warncount; }
98        int getInfoCount() const    { return infocount; }
99        int getStoredCount() const  { return storedcount; }
100        int getErrorLevel() const   { return maxlevel; }
101        string getMessages() const  { return msgs; }
102        string getCountSummary() const; ///< return the standard "... error(s), ... warning(s), ... message(s)" text (or empty string if count==0)
103        void setStoreOptions(int opts) {options=(options & (~(StoreFirstMessage|StoreAllMessages))) | (opts&(StoreFirstMessage|StoreAllMessages));}
104       
105        LoggerToMemory(int opts = 0, int minimal_level_to_store = LOG_ERROR) :LoggerBase(opts), minleveltostore(minimal_level_to_store)
106        {
107                reset();
108        }
109
110        void handle(const char *obj, const char *method, int level, const char *msg);
111};
112
113class RedirectingLogger : public LoggerBase
114{
115        LoggerManager *other_manager;
116public:
117        RedirectingLogger(LoggerManager *other_mgr, int opts = 0)
118                :LoggerBase(opts), other_manager(other_mgr) {}
119
120        void handle(const char *obj, const char *method, int level, const char *msg)
121        {
122                other_manager->send(obj, method, level, msg);
123        }
124};
125
126#endif
Note: See TracBrowser for help on using the repository browser.