[64] | 1 | // This file is a part of the Framsticks GDK library.
|
---|
| 2 | // Copyright (C) 2002-2011 Szymon Ulatowski. See LICENSE.txt for details.
|
---|
| 3 | // Refer to http://www.framsticks.com/ for further information.
|
---|
| 4 |
|
---|
| 5 | #include "errmanager.h"
|
---|
| 6 |
|
---|
| 7 | ErrorManager globalErrorManager;
|
---|
| 8 |
|
---|
| 9 | void FramMessage(const char *o,const char *m,const char *bl,int w)
|
---|
| 10 | {
|
---|
| 11 | globalErrorManager.send(o,m,bl,w);
|
---|
| 12 | }
|
---|
| 13 |
|
---|
| 14 | void ErrorManager::send(int level,const char *o,const char *m,const char *bl,int w)
|
---|
| 15 | {
|
---|
| 16 | if (level>=handlers.size()) level=handlers.size()-1;
|
---|
| 17 | bool blocked=0;
|
---|
| 18 | for(int i=level;i>=0;i--)
|
---|
| 19 | {
|
---|
| 20 | ErrorHandlerBase *r=handlers(i);
|
---|
| 21 | if ((!blocked)||(r->options & ErrorHandlerBase::CannotBeBlocked))
|
---|
| 22 | r->handle(o,m,bl,w);
|
---|
| 23 | if (!(r->options & ErrorHandlerBase::DontBlock)) blocked=1;
|
---|
| 24 | }
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | int ErrorManager::add(ErrorHandlerBase *h)
|
---|
| 28 | {
|
---|
| 29 | h->mgr=this;
|
---|
| 30 | handlers+=h;
|
---|
| 31 | return handlers.size()-1;
|
---|
| 32 | }
|
---|
| 33 |
|
---|
| 34 | void ErrorManager::remove(int i)
|
---|
| 35 | {
|
---|
| 36 | ErrorHandlerBase *h=handlers(i);
|
---|
| 37 | h->mgr=0;
|
---|
| 38 | handlers.remove(i);
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | void ErrorManager::remove(ErrorHandlerBase *h)
|
---|
| 42 | {
|
---|
| 43 | int i;
|
---|
| 44 | if ((i=handlers.find(h))<0) return;
|
---|
| 45 | remove(i);
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | void ErrorManager::removeAll()
|
---|
| 49 | {
|
---|
| 50 | while(handlers.size()>0)
|
---|
| 51 | remove(handlers.size()-1);
|
---|
| 52 | }
|
---|
| 53 |
|
---|
| 54 | //////////////////////////////////
|
---|
| 55 |
|
---|
| 56 | void ErrorHandlerBase::send(const char *o,const char *m,const char *bl,int w)
|
---|
| 57 | {
|
---|
| 58 | if (!isEnabled()) return;
|
---|
| 59 | int level=mgr->find(this);
|
---|
| 60 | if (level>=0) mgr->send(level-1,o,m,bl,w);
|
---|
| 61 | }
|
---|
| 62 |
|
---|
| 63 | void ErrorHandlerBase::FMprintf(const char *o,const char *m,int w,const char *bl, ...)
|
---|
| 64 | {
|
---|
| 65 | if (!isEnabled()) return;
|
---|
| 66 | char buf[10000];
|
---|
| 67 | va_list argptr;
|
---|
| 68 | va_start(argptr,bl);
|
---|
| 69 | vsnprintf(buf,10000,bl,argptr);
|
---|
| 70 | va_end(argptr);
|
---|
| 71 | send(o,m,buf,w);
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 |
|
---|
| 75 | void ErrorHandlerBase::enable()
|
---|
| 76 | {
|
---|
| 77 | if (isEnabled()) return;
|
---|
| 78 | globalErrorManager.add(this);
|
---|
| 79 | }
|
---|
| 80 |
|
---|
| 81 | void ErrorHandlerBase::disable()
|
---|
| 82 | {
|
---|
| 83 | if (!isEnabled()) return;
|
---|
| 84 | globalErrorManager.remove(this);
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /////////////////////////////////
|
---|
| 88 |
|
---|
| 89 | void ErrorHandler::handle(const char *o,const char *m,const char *bl,int w)
|
---|
| 90 | {
|
---|
| 91 | if (w>maxlevel) maxlevel=w;
|
---|
| 92 | if (w>=FMLV_INFO) infocount++;
|
---|
| 93 | if (w>=FMLV_WARN) warncount++;
|
---|
| 94 | if (w>=FMLV_ERROR) errcount++;
|
---|
| 95 |
|
---|
| 96 | if (w>=storlevel)
|
---|
| 97 | {
|
---|
| 98 | storcount++;
|
---|
| 99 | if (options & (StoreFirstMessage|StoreAllMessages))
|
---|
| 100 | {
|
---|
| 101 | if (!((options&StoreFirstMessage)&&(msgs.len()>0)))
|
---|
| 102 | {
|
---|
| 103 | if (msgs.len()>0) msgs+='\n';
|
---|
| 104 | msgs+=o; msgs+="::"; msgs+=m;
|
---|
| 105 | msgs+=" - "; msgs+=bl;
|
---|
| 106 | }
|
---|
| 107 | }
|
---|
| 108 | }
|
---|
| 109 | }
|
---|