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

Last change on this file since 1284 was 1215, checked in by Maciej Komosinski, 20 months ago

Less ambiguous names of counting functions in loggers: warning -> error_warning, info -> error_warning_info

  • Property svn:eol-style set to native
File size: 3.9 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; }
[1215]97        int getErrorWarningCount() const   { return warncount; } //previously: getWarningCount()
98        int getErrorWarningInfoCount() const   { return infocount; } //previously: getInfoCount()
99        int getWarningCount() const { return warncount-errcount; }
100        int getInfoCount() const    { return infocount-warncount; }
[452]101        int getStoredCount() const  { return storedcount; }
102        int getErrorLevel() const   { return maxlevel; }
103        string getMessages() const  { return msgs; }
104        string getCountSummary() const; ///< return the standard "... error(s), ... warning(s), ... message(s)" text (or empty string if count==0)
[1100]105        void setStoreOptions(int opts) {options=(options & (~(StoreFirstMessage|StoreAllMessages))) | (opts&(StoreFirstMessage|StoreAllMessages));}
[1082]106       
[375]107        LoggerToMemory(int opts = 0, int minimal_level_to_store = LOG_ERROR) :LoggerBase(opts), minleveltostore(minimal_level_to_store)
[336]108        {
109                reset();
110        }
[109]111
[378]112        void handle(const char *obj, const char *method, int level, const char *msg);
[109]113};
114
[375]115class RedirectingLogger : public LoggerBase
[109]116{
[375]117        LoggerManager *other_manager;
[109]118public:
[378]119        RedirectingLogger(LoggerManager *other_mgr, int opts = 0)
[375]120                :LoggerBase(opts), other_manager(other_mgr) {}
[109]121
[378]122        void handle(const char *obj, const char *method, int level, const char *msg)
[336]123        {
[378]124                other_manager->send(obj, method, level, msg);
[336]125        }
[109]126};
127
128#endif
Note: See TracBrowser for help on using the repository browser.