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

Last change on this file since 1130 was 1130, checked in by Maciej Komosinski, 3 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
RevLine 
[286]1// This file is a part of Framsticks SDK.  http://www.framsticks.com/
[1130]2// Copyright (C) 1999-2021  Maciej Komosinski and Szymon Ulatowski.
[286]3// See LICENSE.txt for details.
[109]4
[378]5#ifndef _COMMON_LOGGERS_H_
6#define _COMMON_LOGGERS_H_
[109]7
[375]8#include <common/log.h>
[109]9#include <common/threads.h>
[378]10#include <common/nonstd_stl.h>
[1130]11#include <algorithm>
[109]12
[375]13class LoggerBase;
[109]14
[375]15class LoggerManager
[109]16{
[375]17        friend class LoggerBase;
[378]18        vector<LoggerBase*> loggers;
19        void send(int position, const char *obj, const char *method, int level, const char *msg);
[336]20public:
[378]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);
[336]27        void remove(int i);
[378]28        void remove(LoggerBase *logger);
[336]29        void removeAll();
[378]30        void send(const char *obj, const char *method, int level, const char *msg)
[336]31        {
[1075]32                send(int(loggers.size())-1, obj, method, level, msg);
[336]33        }
[375]34        ~LoggerManager() { removeAll(); }
[109]35};
36
[375]37extern THREAD_LOCAL_DECL(LoggerManager, message_handler_manager_instance);
[109]38
39////////////////////////////////////////
40
[375]41class LoggerBase
[109]42{
[375]43        friend class LoggerManager;
[336]44protected:
[375]45        LoggerManager* manager;
[336]46        int options;
[109]47
[336]48public:
[109]49
[375]50        enum LoggerOptions
[336]51        {
[372]52                DontBlock = 1, CannotBeBlocked = 2, Enable = 4, Paused = 8
[336]53        };
[109]54
[375]55        void logPrintf(const char *o, const char *m, int w, const char *bl, ...);
[378]56        void send(const char *obj, const char *method, int level, const char *msg);
[109]57
[1082]58        int getOptions() { return options; }
[373]59        bool isEnabled() { return manager ? true : false; }
[336]60        void enable();
61        void disable();
62        bool isPaused() { return (options & Paused) != 0; }
63        void pause();
64        void resume();
[109]65
[375]66        LoggerBase(int opts = 0) :manager(NULL), options(opts)
[336]67        {
[372]68                if (options&Enable) enable();
[336]69        }
[375]70        virtual ~LoggerBase()
[336]71        {
72                disable();
73        }
[109]74
[511]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
[109]77};
78
79///////////////////////////////////////////
80
[375]81class LoggerToMemory : public LoggerBase
[109]82{
[336]83protected:
[378]84        int maxlevel, minleveltostore, errcount, warncount, infocount, storedcount;
85        string msgs;
[109]86
[336]87public:
[109]88
[876]89        void reset() { maxlevel = LOG_MIN - 1; errcount = warncount = infocount = storedcount = 0; msgs = ""; }
[109]90
[336]91        enum Options2
92        {
93                StoreFirstMessage = 16, StoreAllMessages = 32
94        };
[109]95
[452]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)
[1100]103        void setStoreOptions(int opts) {options=(options & (~(StoreFirstMessage|StoreAllMessages))) | (opts&(StoreFirstMessage|StoreAllMessages));}
[1082]104       
[375]105        LoggerToMemory(int opts = 0, int minimal_level_to_store = LOG_ERROR) :LoggerBase(opts), minleveltostore(minimal_level_to_store)
[336]106        {
107                reset();
108        }
[109]109
[378]110        void handle(const char *obj, const char *method, int level, const char *msg);
[109]111};
112
[375]113class RedirectingLogger : public LoggerBase
[109]114{
[375]115        LoggerManager *other_manager;
[109]116public:
[378]117        RedirectingLogger(LoggerManager *other_mgr, int opts = 0)
[375]118                :LoggerBase(opts), other_manager(other_mgr) {}
[109]119
[378]120        void handle(const char *obj, const char *method, int level, const char *msg)
[336]121        {
[378]122                other_manager->send(obj, method, level, msg);
[336]123        }
[109]124};
125
126#endif
Note: See TracBrowser for help on using the repository browser.