[286] | 1 | // This file is a part of Framsticks SDK. http://www.framsticks.com/ |
---|
| 2 | // Copyright (C) 1999-2015 Maciej Komosinski and Szymon Ulatowski. |
---|
| 3 | // See LICENSE.txt for details. |
---|
[109] | 4 | |
---|
[375] | 5 | #include "loggers.h" |
---|
[180] | 6 | #include <common/stl-util.h> |
---|
[109] | 7 | |
---|
[378] | 8 | void _logMessageSingleLine(const char *obj, const char *method, int level, const char *msg) |
---|
[109] | 9 | { |
---|
[378] | 10 | tlsGetRef(message_handler_manager_instance).send(obj, method, level, msg); |
---|
[109] | 11 | } |
---|
| 12 | |
---|
[375] | 13 | THREAD_LOCAL_DEF(LoggerManager, message_handler_manager_instance); |
---|
[109] | 14 | |
---|
[378] | 15 | void LoggerManager::send(int position, const char *obj, const char *method, int level, const char *msg) |
---|
[109] | 16 | { |
---|
[378] | 17 | if (position >= (int)loggers.size()) position = loggers.size() - 1; |
---|
| 18 | bool blocked = false; |
---|
| 19 | for (int i = position; i >= 0; i--) |
---|
[109] | 20 | { |
---|
[378] | 21 | LoggerBase *logger = loggers[i]; |
---|
| 22 | if ((!(logger->options & LoggerBase::Paused)) && |
---|
| 23 | ((!blocked) || (logger->options & LoggerBase::CannotBeBlocked))) |
---|
[336] | 24 | { |
---|
[378] | 25 | logger->handle(obj, method, level, msg); |
---|
| 26 | if (!(logger->options & LoggerBase::DontBlock)) blocked = true; |
---|
[336] | 27 | } |
---|
[109] | 28 | } |
---|
| 29 | } |
---|
| 30 | |
---|
[378] | 31 | int LoggerManager::add(LoggerBase *logger) |
---|
[109] | 32 | { |
---|
[378] | 33 | logger->manager = this; |
---|
| 34 | loggers.push_back(logger); |
---|
| 35 | return loggers.size() - 1; |
---|
[109] | 36 | } |
---|
| 37 | |
---|
[375] | 38 | void LoggerManager::remove(int i) |
---|
[109] | 39 | { |
---|
[378] | 40 | LoggerBase *h = loggers[i]; |
---|
[373] | 41 | h->manager = NULL; |
---|
[378] | 42 | loggers.erase(loggers.begin() + i); |
---|
[109] | 43 | } |
---|
| 44 | |
---|
[378] | 45 | void LoggerManager::remove(LoggerBase *logger) |
---|
[109] | 46 | { |
---|
[378] | 47 | int index = find(logger); |
---|
| 48 | if (index >= 0) |
---|
| 49 | remove(index); |
---|
[109] | 50 | } |
---|
| 51 | |
---|
[375] | 52 | void LoggerManager::removeAll() |
---|
[109] | 53 | { |
---|
[378] | 54 | while (loggers.size() > 0) |
---|
| 55 | remove(loggers.size() - 1); |
---|
[109] | 56 | } |
---|
| 57 | |
---|
| 58 | ////////////////////////////////// |
---|
| 59 | |
---|
[378] | 60 | void LoggerBase::send(const char *obj, const char *method, int level, const char *msg) |
---|
[109] | 61 | { |
---|
[336] | 62 | if (!isEnabled()) return; |
---|
[378] | 63 | int position = manager->find(this); |
---|
| 64 | if (position >= 0) manager->send(position - 1, obj, method, level, msg); |
---|
[109] | 65 | } |
---|
| 66 | |
---|
[378] | 67 | void LoggerBase::logPrintf(const char *obj, const char *method, int level, const char *msg, ...) |
---|
[109] | 68 | { |
---|
[336] | 69 | if (!isEnabled()) return; |
---|
| 70 | string buf; |
---|
| 71 | va_list argptr; |
---|
[378] | 72 | va_start(argptr, msg); |
---|
| 73 | buf = ssprintf_va(msg, argptr); |
---|
[336] | 74 | va_end(argptr); |
---|
[378] | 75 | send(obj, method, level, buf.c_str()); |
---|
[109] | 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
[375] | 79 | void LoggerBase::enable() |
---|
[109] | 80 | { |
---|
[336] | 81 | if (isEnabled()) return; |
---|
[372] | 82 | tlsGetRef(message_handler_manager_instance).add(this); |
---|
[109] | 83 | } |
---|
| 84 | |
---|
[375] | 85 | void LoggerBase::disable() |
---|
[109] | 86 | { |
---|
[336] | 87 | if (!isEnabled()) return; |
---|
[372] | 88 | tlsGetRef(message_handler_manager_instance).remove(this); |
---|
[109] | 89 | } |
---|
| 90 | |
---|
[375] | 91 | void LoggerBase::pause() |
---|
[336] | 92 | { |
---|
| 93 | if (isPaused()) return; |
---|
| 94 | options |= Paused; |
---|
| 95 | } |
---|
| 96 | |
---|
[375] | 97 | void LoggerBase::resume() |
---|
[336] | 98 | { |
---|
| 99 | if (!isPaused()) return; |
---|
| 100 | options &= ~Paused; |
---|
| 101 | } |
---|
| 102 | |
---|
[109] | 103 | ///////////////////////////////// |
---|
| 104 | |
---|
[378] | 105 | void LoggerToMemory::handle(const char *obj, const char *method, int level, const char *msg) |
---|
[109] | 106 | { |
---|
[378] | 107 | if (level > maxlevel) maxlevel = level; |
---|
| 108 | if (level >= LOG_INFO) infocount++; |
---|
| 109 | if (level >= LOG_WARN) warncount++; |
---|
| 110 | if (level >= LOG_ERROR) errcount++; |
---|
[109] | 111 | |
---|
[378] | 112 | if (level >= minleveltostore) |
---|
[109] | 113 | { |
---|
[373] | 114 | storedcount++; |
---|
[336] | 115 | if (options & (StoreFirstMessage | StoreAllMessages)) |
---|
[109] | 116 | { |
---|
[378] | 117 | if (!((options&StoreFirstMessage) && (msgs.length() > 0))) |
---|
[109] | 118 | { |
---|
[378] | 119 | if (msgs.length() > 0) msgs += '\n'; |
---|
| 120 | msgs += ssprintf(LOG_FORMAT, LOG_LEVEL[level + 1], obj, method, msg); |
---|
[109] | 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
[452] | 125 | |
---|
| 126 | string LoggerToMemory::getCountSummary() const |
---|
| 127 | { |
---|
| 128 | if (getInfoCount()) |
---|
| 129 | { |
---|
| 130 | string msg; |
---|
| 131 | if (getErrorCount()) |
---|
| 132 | msg=ssprintf("%d error(s)",getErrorCount()); |
---|
| 133 | int w; |
---|
| 134 | if (w=getWarningCount()-getErrorCount()) |
---|
| 135 | msg+=ssprintf("%s%d warning(s)",(getErrorCount()?", ":""),w); |
---|
| 136 | if (w=getInfoCount()-getWarningCount()) |
---|
| 137 | msg+=ssprintf("%s%d message(s)",(getWarningCount()?", ":""),w); |
---|
| 138 | return msg; |
---|
| 139 | } |
---|
| 140 | return string(""); |
---|
| 141 | } |
---|
| 142 | |
---|