// This file is a part of Framsticks SDK. http://www.framsticks.com/ // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. // See LICENSE.txt for details. #ifndef _FRAMS_LOGGERS_H_ #define _FRAMS_LOGGERS_H_ #include #include #include #include class LoggerBase; class LoggerManager { friend class LoggerBase; SListTempl handlers; void send(int level, const char *o, const char *m, int w, const char *bl); public: int find(LoggerBase *r) { return handlers.find(r); } int add(LoggerBase *r); void remove(int i); void remove(LoggerBase *r); void removeAll(); void send(const char *o, const char *m, int w, const char *bl) { send(handlers.size() - 1, o, m, w, bl); } ~LoggerManager() { removeAll(); } }; extern THREAD_LOCAL_DECL(LoggerManager, message_handler_manager_instance); //////////////////////////////////////// class LoggerBase { friend class LoggerManager; protected: LoggerManager* manager; int options; public: enum LoggerOptions { DontBlock = 1, CannotBeBlocked = 2, Enable = 4, Paused = 8 }; void logPrintf(const char *o, const char *m, int w, const char *bl, ...); void send(const char *o, const char *m, int w, const char *bl); bool isEnabled() { return manager ? true : false; } void enable(); void disable(); bool isPaused() { return (options & Paused) != 0; } void pause(); void resume(); LoggerBase(int opts = 0) :manager(NULL), options(opts) { if (options&Enable) enable(); } virtual ~LoggerBase() { disable(); } virtual void handle(const char *o, const char *m, int w, const char *bl) {} }; /////////////////////////////////////////// class LoggerToMemory : public LoggerBase { protected: int maxlevel, minleveltostore, errcount, warncount, storedcount, infocount; SString msgs; public: void reset() { maxlevel = LOG_INFO - 1; errcount = warncount = storedcount = infocount = 0; msgs = 0; } enum Options2 { StoreFirstMessage = 16, StoreAllMessages = 32 }; int getErrorCount() { return errcount; } int getWarningCount() { return warncount; } int getInfoCount() { return infocount; } int getStoredCount() { return storedcount; } int getErrorLevel() { return maxlevel; } const SString& getMessages() { return msgs; } LoggerToMemory(int opts = 0, int minimal_level_to_store = LOG_ERROR) :LoggerBase(opts), minleveltostore(minimal_level_to_store) { reset(); } void handle(const char *o, const char *m, int w, const char *bl); }; class RedirectingLogger : public LoggerBase { LoggerManager *other_manager; public: RedirectingLogger(LoggerManager *other_mgr,int opts=0) :LoggerBase(opts), other_manager(other_mgr) {} void handle(const char *o, const char *m, int w, const char *bl) { other_manager->send(o, m, w, bl); } }; #endif