source: cpp/frams/loggers/loggers.h @ 375

Last change on this file since 375 was 375, checked in by Maciej Komosinski, 9 years ago

Renamed logging functions to more intuitive and simple names

  • Property svn:eol-style set to native
File size: 2.8 KB
Line 
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
13class LoggerBase;
14
15class 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);
20public:
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
33extern THREAD_LOCAL_DECL(LoggerManager, message_handler_manager_instance);
34
35////////////////////////////////////////
36
37class LoggerBase
38{
39        friend class LoggerManager;
40protected:
41        LoggerManager* manager;
42        int options;
43
44public:
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
75class LoggerToMemory : public LoggerBase
76{
77protected:
78        int maxlevel, minleveltostore, errcount, warncount, storedcount, infocount;
79        SString msgs;
80
81public:
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
105class RedirectingLogger : public LoggerBase
106{
107        LoggerManager *other_manager;
108public:
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
Note: See TracBrowser for help on using the repository browser.