#pragma once
#include <wtf/Algorithms.h>
#include <wtf/HashSet.h>
#include <wtf/Logger.h>
#include <wtf/Ref.h>
#include <wtf/RefPtr.h>
namespace WTF {
class AggregateLogger : public Logger {
WTF_MAKE_NONCOPYABLE(AggregateLogger);
public:
static Ref<AggregateLogger> create(const void* owner)
{
return adoptRef(*new AggregateLogger(owner));
}
void addLogger(const Logger& logger)
{
m_loggers.add(&logger);
}
void removeLogger(const Logger& logger)
{
m_loggers.remove(&logger);
}
template<typename... Arguments>
inline void logAlways(WTFLogChannel& channel, UNUSED_FUNCTION const Arguments&... arguments) const
{
#if RELEASE_LOG_DISABLED
UNUSED_PARAM(channel);
#else
log(channel, WTFLogLevel::Always, arguments...);
#endif
}
template<typename... Arguments>
inline void error(WTFLogChannel& channel, const Arguments&... arguments) const
{
log(channel, WTFLogLevel::Error, arguments...);
}
template<typename... Arguments>
inline void warning(WTFLogChannel& channel, const Arguments&... arguments) const
{
log(channel, WTFLogLevel::Warning, arguments...);
}
template<typename... Arguments>
inline void info(WTFLogChannel& channel, const Arguments&... arguments) const
{
log(channel, WTFLogLevel::Info, arguments...);
}
template<typename... Arguments>
inline void debug(WTFLogChannel& channel, const Arguments&... arguments) const
{
log(channel, WTFLogLevel::Debug, arguments...);
}
inline bool willLog(const WTFLogChannel& channel, WTFLogLevel level) const
{
return allOf(m_loggers, [channel, level] (auto& logger) { return logger->willLog(channel, level); });
}
private:
AggregateLogger(const void* owner)
: Logger(owner)
{
}
template<typename... Argument>
inline void log(WTFLogChannel& channel, WTFLogLevel level, const Argument&... arguments) const
{
if (!willLog(channel, level))
return;
Logger::log(channel, level, arguments...);
for (const auto& logger : m_loggers) {
for (Observer& observer : logger->observers())
observer.didLogMessage(channel, level, { ConsoleLogValue<Argument>::toValue(arguments)... });
}
}
HashSet<RefPtr<const Logger>> m_loggers;
};
}
using WTF::AggregateLogger;