// 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. #include "loggers.h" #include void _logMessageSingleLine(const char *o, const char *m, int w, const char *txt) { tlsGetRef(message_handler_manager_instance).send(o, m, w, txt); } THREAD_LOCAL_DEF(LoggerManager, message_handler_manager_instance); void LoggerManager::send(int level, const char *o, const char *m, int w, const char *bl) { if (level >= handlers.size()) level = handlers.size() - 1; bool blocked = 0; for (int i = level; i >= 0; i--) { LoggerBase *r = handlers(i); if ((!(r->options & LoggerBase::Paused)) && ((!blocked) || (r->options & LoggerBase::CannotBeBlocked))) { r->handle(o, m, w, bl); if (!(r->options & LoggerBase::DontBlock)) blocked = 1; } } } int LoggerManager::add(LoggerBase *h) { h->manager = this; handlers += h; return handlers.size() - 1; } void LoggerManager::remove(int i) { LoggerBase *h = handlers(i); h->manager = NULL; handlers.remove(i); } void LoggerManager::remove(LoggerBase *h) { int i; if ((i = handlers.find(h)) < 0) return; remove(i); } void LoggerManager::removeAll() { while (handlers.size() > 0) remove(handlers.size() - 1); } ////////////////////////////////// void LoggerBase::send(const char *o, const char *m, int w, const char *bl) { if (!isEnabled()) return; int level = manager->find(this); if (level >= 0) manager->send(level - 1, o, m, w, bl); } void LoggerBase::logPrintf(const char *o, const char *m, int w, const char *bl, ...) { if (!isEnabled()) return; string buf; va_list argptr; va_start(argptr, bl); buf = ssprintf_va(bl, argptr); va_end(argptr); send(o, m, w, buf.c_str()); } void LoggerBase::enable() { if (isEnabled()) return; tlsGetRef(message_handler_manager_instance).add(this); } void LoggerBase::disable() { if (!isEnabled()) return; tlsGetRef(message_handler_manager_instance).remove(this); } void LoggerBase::pause() { if (isPaused()) return; options |= Paused; } void LoggerBase::resume() { if (!isPaused()) return; options &= ~Paused; } ///////////////////////////////// void LoggerToMemory::handle(const char *o, const char *m, int w, const char *bl) { if (w > maxlevel) maxlevel = w; if (w >= LOG_INFO) infocount++; if (w >= LOG_WARN) warncount++; if (w >= LOG_ERROR) errcount++; if (w >= minleveltostore) { storedcount++; if (options & (StoreFirstMessage | StoreAllMessages)) { if (!((options&StoreFirstMessage) && (msgs.len() > 0))) { if (msgs.len() > 0) msgs += '\n'; msgs += SString::sprintf(LOG_FORMAT,LOG_LEVEL[w+1],o,m,bl); } } } }