1 /* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */ 2 3 #ifndef LOG_SINK_HANDLER_H 4 #define LOG_SINK_HANDLER_H 5 6 #include <string> 7 #include <set> 8 9 10 /** 11 * C++/OO API for log sinks. 12 * @see MulticastLogSink 13 */ 14 class ILogSink { 15 public: 16 virtual void RecordLogMessage(const std::string& section, int level, 17 const std::string& text) = 0; 18 protected: ~ILogSink()19 ~ILogSink() {} 20 }; 21 22 /** 23 * Sends log messages to ILogSink implementations. 24 */ 25 class LogSinkHandler { 26 public: LogSinkHandler()27 LogSinkHandler(): sinking(true) { 28 } 29 GetInstance()30 static LogSinkHandler& GetInstance() { 31 static LogSinkHandler lsh; 32 return lsh; 33 } 34 35 void AddSink(ILogSink* logSink); 36 void RemoveSink(ILogSink* logSink); 37 38 /** 39 * @brief Enables/Disables sinking to registered sinks 40 * 41 * The list of registered sinks does not get altered when calling this 42 * function, just its use is enabled or disabled. 43 * Used, for example, in case the info-console and other in game 44 * subscribers should not be notified anymore (eg. SDL shutdown). 45 */ SetSinking(bool enabled)46 void SetSinking(bool enabled) { sinking = enabled; } 47 /** 48 * Indicates whether sinking to registered sinks is enabled 49 * 50 * The initial value is true. 51 */ IsSinking()52 bool IsSinking() const { return sinking; } 53 54 void RecordLogMessage(const std::string& section, int level, 55 const std::string& text) const; 56 57 private: 58 std::set<ILogSink*> sinks; 59 /** 60 * Whether log records are passed on to registered sinks, or dismissed. 61 */ 62 bool sinking; 63 }; 64 65 #define logSinkHandler (LogSinkHandler::GetInstance()) 66 67 #endif // LOG_SINK_HANDLER_H 68 69