[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 | |
---|
| 5 | #include "errmanager.h" |
---|
[180] | 6 | #include <common/stl-util.h> |
---|
[109] | 7 | |
---|
[244] | 8 | void _FramMessageSingleLine(const char *o, const char *m, const char *txt, int w) |
---|
[109] | 9 | { |
---|
[336] | 10 | tlsGetRef(errmgr_instance).send(o, m, txt, w); |
---|
[109] | 11 | } |
---|
| 12 | |
---|
[336] | 13 | THREAD_LOCAL_DEF(ErrorManager, errmgr_instance); |
---|
[109] | 14 | |
---|
[336] | 15 | void ErrorManager::send(int level, const char *o, const char *m, const char *bl, int w) |
---|
[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 | { |
---|
[336] | 21 | ErrorHandlerBase *r = handlers(i); |
---|
| 22 | if ((!(r->options & ErrorHandlerBase::Paused)) && |
---|
| 23 | ((!blocked) || (r->options & ErrorHandlerBase::CannotBeBlocked))) |
---|
| 24 | { |
---|
| 25 | r->handle(o, m, bl, w); |
---|
| 26 | if (!(r->options & ErrorHandlerBase::DontBlock)) blocked = 1; |
---|
| 27 | } |
---|
[109] | 28 | } |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | int ErrorManager::add(ErrorHandlerBase *h) |
---|
| 32 | { |
---|
[336] | 33 | h->mgr = this; |
---|
| 34 | handlers += h; |
---|
| 35 | return handlers.size() - 1; |
---|
[109] | 36 | } |
---|
| 37 | |
---|
| 38 | void ErrorManager::remove(int i) |
---|
| 39 | { |
---|
[336] | 40 | ErrorHandlerBase *h = handlers(i); |
---|
| 41 | h->mgr = 0; |
---|
| 42 | handlers.remove(i); |
---|
[109] | 43 | } |
---|
| 44 | |
---|
| 45 | void ErrorManager::remove(ErrorHandlerBase *h) |
---|
| 46 | { |
---|
[336] | 47 | int i; |
---|
| 48 | if ((i = handlers.find(h)) < 0) return; |
---|
| 49 | remove(i); |
---|
[109] | 50 | } |
---|
| 51 | |
---|
| 52 | void ErrorManager::removeAll() |
---|
| 53 | { |
---|
[336] | 54 | while (handlers.size() > 0) |
---|
| 55 | remove(handlers.size() - 1); |
---|
[109] | 56 | } |
---|
| 57 | |
---|
| 58 | ////////////////////////////////// |
---|
| 59 | |
---|
[336] | 60 | void ErrorHandlerBase::send(const char *o, const char *m, const char *bl, int w) |
---|
[109] | 61 | { |
---|
[336] | 62 | if (!isEnabled()) return; |
---|
| 63 | int level = mgr->find(this); |
---|
| 64 | if (level >= 0) mgr->send(level - 1, o, m, bl, w); |
---|
[109] | 65 | } |
---|
| 66 | |
---|
[336] | 67 | void ErrorHandlerBase::FMprintf(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); |
---|
| 75 | send(o, m, buf.c_str(), w); |
---|
[109] | 76 | } |
---|
| 77 | |
---|
| 78 | |
---|
| 79 | void ErrorHandlerBase::enable() |
---|
| 80 | { |
---|
[336] | 81 | if (isEnabled()) return; |
---|
| 82 | tlsGetRef(errmgr_instance).add(this); |
---|
[109] | 83 | } |
---|
| 84 | |
---|
| 85 | void ErrorHandlerBase::disable() |
---|
| 86 | { |
---|
[336] | 87 | if (!isEnabled()) return; |
---|
| 88 | tlsGetRef(errmgr_instance).remove(this); |
---|
[109] | 89 | } |
---|
| 90 | |
---|
[336] | 91 | void ErrorHandlerBase::pause() |
---|
| 92 | { |
---|
| 93 | if (isPaused()) return; |
---|
| 94 | options |= Paused; |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | void ErrorHandlerBase::resume() |
---|
| 98 | { |
---|
| 99 | if (!isPaused()) return; |
---|
| 100 | options &= ~Paused; |
---|
| 101 | } |
---|
| 102 | |
---|
[109] | 103 | ///////////////////////////////// |
---|
| 104 | |
---|
[336] | 105 | void ErrorHandler::handle(const char *o, const char *m, const char *bl, int w) |
---|
[109] | 106 | { |
---|
[336] | 107 | if (w > maxlevel) maxlevel = w; |
---|
| 108 | if (w >= FMLV_INFO) infocount++; |
---|
| 109 | if (w >= FMLV_WARN) warncount++; |
---|
| 110 | if (w >= FMLV_ERROR) errcount++; |
---|
[109] | 111 | |
---|
[336] | 112 | if (w >= storlevel) |
---|
[109] | 113 | { |
---|
[336] | 114 | storcount++; |
---|
| 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'; |
---|
| 120 | msgs += o; msgs += "::"; msgs += m; |
---|
| 121 | msgs += " - "; msgs += bl; |
---|
[109] | 122 | } |
---|
| 123 | } |
---|
| 124 | } |
---|
| 125 | } |
---|