[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 | |
---|
[375] | 8 | void _logMessageSingleLine(const char *o, const char *m, int w, const char *txt) |
---|
[109] | 9 | { |
---|
[375] | 10 | tlsGetRef(message_handler_manager_instance).send(o, m, w, txt); |
---|
[109] | 11 | } |
---|
| 12 | |
---|
[375] | 13 | THREAD_LOCAL_DEF(LoggerManager, message_handler_manager_instance); |
---|
[109] | 14 | |
---|
[375] | 15 | void LoggerManager::send(int level, const char *o, const char *m, int w, const char *bl) |
---|
[109] | 16 | { |
---|
[336] | 17 | if (level >= handlers.size()) level = handlers.size() - 1; |
---|
| 18 | bool blocked = 0; |
---|
| 19 | for (int i = level; i >= 0; i--) |
---|
[109] | 20 | { |
---|
[375] | 21 | LoggerBase *r = handlers(i); |
---|
| 22 | if ((!(r->options & LoggerBase::Paused)) && |
---|
| 23 | ((!blocked) || (r->options & LoggerBase::CannotBeBlocked))) |
---|
[336] | 24 | { |
---|
[375] | 25 | r->handle(o, m, w, bl); |
---|
| 26 | if (!(r->options & LoggerBase::DontBlock)) blocked = 1; |
---|
[336] | 27 | } |
---|
[109] | 28 | } |
---|
| 29 | } |
---|
| 30 | |
---|
[375] | 31 | int LoggerManager::add(LoggerBase *h) |
---|
[109] | 32 | { |
---|
[373] | 33 | h->manager = this; |
---|
[336] | 34 | handlers += h; |
---|
| 35 | return handlers.size() - 1; |
---|
[109] | 36 | } |
---|
| 37 | |
---|
[375] | 38 | void LoggerManager::remove(int i) |
---|
[109] | 39 | { |
---|
[375] | 40 | LoggerBase *h = handlers(i); |
---|
[373] | 41 | h->manager = NULL; |
---|
[336] | 42 | handlers.remove(i); |
---|
[109] | 43 | } |
---|
| 44 | |
---|
[375] | 45 | void LoggerManager::remove(LoggerBase *h) |
---|
[109] | 46 | { |
---|
[336] | 47 | int i; |
---|
| 48 | if ((i = handlers.find(h)) < 0) return; |
---|
| 49 | remove(i); |
---|
[109] | 50 | } |
---|
| 51 | |
---|
[375] | 52 | void LoggerManager::removeAll() |
---|
[109] | 53 | { |
---|
[336] | 54 | while (handlers.size() > 0) |
---|
| 55 | remove(handlers.size() - 1); |
---|
[109] | 56 | } |
---|
| 57 | |
---|
| 58 | ////////////////////////////////// |
---|
| 59 | |
---|
[375] | 60 | void LoggerBase::send(const char *o, const char *m, int w, const char *bl) |
---|
[109] | 61 | { |
---|
[336] | 62 | if (!isEnabled()) return; |
---|
[373] | 63 | int level = manager->find(this); |
---|
[375] | 64 | if (level >= 0) manager->send(level - 1, o, m, w, bl); |
---|
[109] | 65 | } |
---|
| 66 | |
---|
[375] | 67 | void LoggerBase::logPrintf(const char *o, const char *m, int w, const char *bl, ...) |
---|
[109] | 68 | { |
---|
[336] | 69 | if (!isEnabled()) return; |
---|
| 70 | string buf; |
---|
| 71 | va_list argptr; |
---|
| 72 | va_start(argptr, bl); |
---|
| 73 | buf = ssprintf_va(bl, argptr); |
---|
| 74 | va_end(argptr); |
---|
[375] | 75 | send(o, m, w, 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 | |
---|
[375] | 105 | void LoggerToMemory::handle(const char *o, const char *m, int w, const char *bl) |
---|
[109] | 106 | { |
---|
[336] | 107 | if (w > maxlevel) maxlevel = w; |
---|
[375] | 108 | if (w >= LOG_INFO) infocount++; |
---|
| 109 | if (w >= LOG_WARN) warncount++; |
---|
| 110 | if (w >= LOG_ERROR) errcount++; |
---|
[109] | 111 | |
---|
[373] | 112 | if (w >= minleveltostore) |
---|
[109] | 113 | { |
---|
[373] | 114 | storedcount++; |
---|
[336] | 115 | if (options & (StoreFirstMessage | StoreAllMessages)) |
---|
[109] | 116 | { |
---|
[336] | 117 | if (!((options&StoreFirstMessage) && (msgs.len() > 0))) |
---|
[109] | 118 | { |
---|
[336] | 119 | if (msgs.len() > 0) msgs += '\n'; |
---|
[375] | 120 | msgs += SString::sprintf(LOG_FORMAT,LOG_LEVEL[w+1],o,m,bl); |
---|
[109] | 121 | } |
---|
| 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|