Changeset 336
- Timestamp:
- 03/12/15 04:19:45 (10 years ago)
- Location:
- cpp/frams/errmgr
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/errmgr/errmanager.cpp
r286 r336 8 8 void _FramMessageSingleLine(const char *o, const char *m, const char *txt, int w) 9 9 { 10 tlsGetRef(errmgr_instance).send(o,m,txt,w);10 tlsGetRef(errmgr_instance).send(o, m, txt, w); 11 11 } 12 12 13 THREAD_LOCAL_DEF(ErrorManager, errmgr_instance);13 THREAD_LOCAL_DEF(ErrorManager, errmgr_instance); 14 14 15 void ErrorManager::send(int level, const char *o,const char *m,const char *bl,int w)15 void ErrorManager::send(int level, const char *o, const char *m, const char *bl, int w) 16 16 { 17 if (level>=handlers.size()) level=handlers.size()-1;18 bool blocked=0;19 for(int i=level;i>=0;i--)17 if (level >= handlers.size()) level = handlers.size() - 1; 18 bool blocked = 0; 19 for (int i = level; i >= 0; i--) 20 20 { 21 ErrorHandlerBase *r=handlers(i); 22 if ((!blocked)||(r->options & ErrorHandlerBase::CannotBeBlocked)) 23 r->handle(o,m,bl,w); 24 if (!(r->options & ErrorHandlerBase::DontBlock)) blocked=1; 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 } 25 28 } 26 29 } … … 28 31 int ErrorManager::add(ErrorHandlerBase *h) 29 32 { 30 h->mgr=this;31 handlers+=h;32 return handlers.size()-1;33 h->mgr = this; 34 handlers += h; 35 return handlers.size() - 1; 33 36 } 34 37 35 38 void ErrorManager::remove(int i) 36 39 { 37 ErrorHandlerBase *h=handlers(i);38 h->mgr=0;39 handlers.remove(i);40 ErrorHandlerBase *h = handlers(i); 41 h->mgr = 0; 42 handlers.remove(i); 40 43 } 41 44 42 45 void ErrorManager::remove(ErrorHandlerBase *h) 43 46 { 44 int i;45 if ((i=handlers.find(h))<0) return;46 remove(i);47 int i; 48 if ((i = handlers.find(h)) < 0) return; 49 remove(i); 47 50 } 48 51 49 52 void ErrorManager::removeAll() 50 53 { 51 while(handlers.size()>0)52 remove(handlers.size()-1);54 while (handlers.size() > 0) 55 remove(handlers.size() - 1); 53 56 } 54 57 55 58 ////////////////////////////////// 56 59 57 void ErrorHandlerBase::send(const char *o, const char *m,const char *bl,int w)60 void ErrorHandlerBase::send(const char *o, const char *m, const char *bl, int w) 58 61 { 59 if (!isEnabled()) return;60 int level=mgr->find(this);61 if (level>=0) mgr->send(level-1,o,m,bl,w);62 if (!isEnabled()) return; 63 int level = mgr->find(this); 64 if (level >= 0) mgr->send(level - 1, o, m, bl, w); 62 65 } 63 66 64 void ErrorHandlerBase::FMprintf(const char *o, const char *m,int w,const char *bl, ...)67 void ErrorHandlerBase::FMprintf(const char *o, const char *m, int w, const char *bl, ...) 65 68 { 66 if (!isEnabled()) return;67 string buf;68 va_list argptr;69 va_start(argptr,bl);70 buf=ssprintf_va(bl,argptr);71 va_end(argptr);72 send(o,m,buf.c_str(),w);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); 73 76 } 74 77 … … 76 79 void ErrorHandlerBase::enable() 77 80 { 78 if (isEnabled()) return;79 tlsGetRef(errmgr_instance).add(this);81 if (isEnabled()) return; 82 tlsGetRef(errmgr_instance).add(this); 80 83 } 81 84 82 85 void ErrorHandlerBase::disable() 83 86 { 84 if (!isEnabled()) return; 85 tlsGetRef(errmgr_instance).remove(this); 87 if (!isEnabled()) return; 88 tlsGetRef(errmgr_instance).remove(this); 89 } 90 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; 86 101 } 87 102 88 103 ///////////////////////////////// 89 104 90 void ErrorHandler::handle(const char *o, const char *m,const char *bl,int w)105 void ErrorHandler::handle(const char *o, const char *m, const char *bl, int w) 91 106 { 92 if (w>maxlevel) maxlevel=w;93 if (w>=FMLV_INFO) infocount++;94 if (w>=FMLV_WARN) warncount++;95 if (w>=FMLV_ERROR) errcount++;107 if (w > maxlevel) maxlevel = w; 108 if (w >= FMLV_INFO) infocount++; 109 if (w >= FMLV_WARN) warncount++; 110 if (w >= FMLV_ERROR) errcount++; 96 111 97 if (w>=storlevel)112 if (w >= storlevel) 98 113 { 99 storcount++;100 if (options & (StoreFirstMessage|StoreAllMessages))114 storcount++; 115 if (options & (StoreFirstMessage | StoreAllMessages)) 101 116 { 102 if (!((options&StoreFirstMessage)&&(msgs.len()>0)))117 if (!((options&StoreFirstMessage) && (msgs.len() > 0))) 103 118 { 104 if (msgs.len()>0) msgs+='\n';105 msgs+=o; msgs+="::"; msgs+=m;106 msgs+=" - "; msgs+=bl;119 if (msgs.len() > 0) msgs += '\n'; 120 msgs += o; msgs += "::"; msgs += m; 121 msgs += " - "; msgs += bl; 107 122 } 108 123 } -
cpp/frams/errmgr/errmanager.h
r286 r336 15 15 class ErrorManager 16 16 { 17 friend class ErrorHandlerBase; 18 SListTempl<ErrorHandlerBase*> handlers; 19 void send(int level,const char *o,const char *m,const char *bl,int w); 20 public: 21 int find(ErrorHandlerBase *r) {return handlers.find(r);} 22 int add(ErrorHandlerBase *r); 23 void remove(int i); 24 void remove(ErrorHandlerBase *r); 25 void removeAll(); 26 void send(const char *o,const char *m,const char *bl,int w) 27 {send(handlers.size()-1,o,m,bl,w);} 28 ~ErrorManager() {removeAll();} 17 friend class ErrorHandlerBase; 18 SListTempl<ErrorHandlerBase*> handlers; 19 void send(int level, const char *o, const char *m, const char *bl, int w); 20 public: 21 int find(ErrorHandlerBase *r) { return handlers.find(r); } 22 int add(ErrorHandlerBase *r); 23 void remove(int i); 24 void remove(ErrorHandlerBase *r); 25 void removeAll(); 26 void send(const char *o, const char *m, const char *bl, int w) 27 { 28 send(handlers.size() - 1, o, m, bl, w); 29 } 30 ~ErrorManager() { removeAll(); } 29 31 }; 30 32 31 extern THREAD_LOCAL_DECL(ErrorManager, errmgr_instance);33 extern THREAD_LOCAL_DECL(ErrorManager, errmgr_instance); 32 34 33 35 //////////////////////////////////////// … … 35 37 class ErrorHandlerBase 36 38 { 37 friend class ErrorManager;38 39 ErrorManager* mgr;40 int options;39 friend class ErrorManager; 40 protected: 41 ErrorManager* mgr; 42 int options; 41 43 42 44 public: 43 45 44 enum HandlerOptions 45 { DontBlock=1, CannotBeBlocked=2, DontEnable=4 }; 46 enum HandlerOptions 47 { 48 DontBlock = 1, CannotBeBlocked = 2, DontEnable = 4, Paused = 8 49 }; 46 50 47 void FMprintf(const char *o,const char *m,int w,const char *bl, ...);48 void send(const char *o,const char *m,const char *bl,int w);51 void FMprintf(const char *o, const char *m, int w, const char *bl, ...); 52 void send(const char *o, const char *m, const char *bl, int w); 49 53 50 bool isEnabled() {return mgr?1:0;} 51 void enable(); 52 void disable(); 54 bool isEnabled() { return mgr ? 1 : 0; } 55 void enable(); 56 void disable(); 57 bool isPaused() { return (options & Paused) != 0; } 58 void pause(); 59 void resume(); 53 60 54 ErrorHandlerBase(int opts=0):mgr(0),options(opts) 55 {if (!(options&DontEnable)) enable();} 56 virtual ~ErrorHandlerBase() 57 {disable();} 61 ErrorHandlerBase(int opts = 0) :mgr(0), options(opts) 62 { 63 if (!(options&DontEnable)) enable(); 64 } 65 virtual ~ErrorHandlerBase() 66 { 67 disable(); 68 } 58 69 59 virtual void handle(const char *o,const char *m,const char *bl,int w) {}70 virtual void handle(const char *o, const char *m, const char *bl, int w) {} 60 71 }; 61 72 62 73 /////////////////////////////////////////// 63 74 64 class ErrorHandler : public ErrorHandlerBase75 class ErrorHandler : public ErrorHandlerBase 65 76 { 66 67 int maxlevel,errcount,warncount,storlevel,storcount,infocount;68 SString msgs;77 protected: 78 int maxlevel, errcount, warncount, storlevel, storcount, infocount; 79 SString msgs; 69 80 70 81 public: 71 82 72 void reset() {maxlevel=FMLV_INFO-1; errcount=warncount=storcount=infocount=0; msgs=0;}83 void reset() { maxlevel = FMLV_INFO - 1; errcount = warncount = storcount = infocount = 0; msgs = 0; } 73 84 74 enum Options2 75 { StoreFirstMessage=8, StoreAllMessages=16 }; 85 enum Options2 86 { 87 StoreFirstMessage = 16, StoreAllMessages = 32 88 }; 76 89 77 int getErrorCount() {return errcount;}78 int getWarningCount() {return warncount;}79 int getInfoCount() {return infocount;}80 int getStoredCount() {return storcount;}81 int getErrorLevel() {return maxlevel;}82 const SString& getMessages() {return msgs;}90 int getErrorCount() { return errcount; } 91 int getWarningCount() { return warncount; } 92 int getInfoCount() { return infocount; } 93 int getStoredCount() { return storcount; } 94 int getErrorLevel() { return maxlevel; } 95 const SString& getMessages() { return msgs; } 83 96 84 ErrorHandler(int opts=0,int store=FMLV_ERROR):ErrorHandlerBase(opts),storlevel(store) 85 {reset();} 97 ErrorHandler(int opts = 0, int store = FMLV_ERROR) :ErrorHandlerBase(opts), storlevel(store) 98 { 99 reset(); 100 } 86 101 87 void handle(const char *o,const char *m,const char *bl,int w);102 void handle(const char *o, const char *m, const char *bl, int w); 88 103 }; 89 104 90 class ErrorRedirector : public ErrorHandlerBase105 class ErrorRedirector : public ErrorHandlerBase 91 106 { 92 107 ErrorManager *other_mgr; 93 108 public: 94 109 ErrorRedirector(ErrorManager *om) 95 :ErrorHandlerBase(), other_mgr(om) {}110 :ErrorHandlerBase(), other_mgr(om) {} 96 111 97 void handle(const char *o, const char *m,const char *bl,int w)98 99 other_mgr->send(o, m,bl,w);100 112 void handle(const char *o, const char *m, const char *bl, int w) 113 { 114 other_mgr->send(o, m, bl, w); 115 } 101 116 }; 102 117 103 118 #endif 104 -
cpp/frams/errmgr/stdouterr.cpp
r286 r336 14 14 if (w < -1) w = -1; else if (w>3) w = 3; 15 15 #ifdef SHP 16 AppLog("[%s] %s ::%s -%s\n",MSG_LEVEL[w+1],o,m,bl);16 AppLog("[%s] %s.%s: %s\n",MSG_LEVEL[w+1],o,m,bl); 17 17 #else 18 18 if (file) 19 file->printf("[%s] %s ::%s -%s\n", MSG_LEVEL[w + 1], o, m, bl);19 file->printf("[%s] %s.%s: %s\n", MSG_LEVEL[w + 1], o, m, bl); 20 20 else 21 printf("[%s] %s ::%s -%s\n", MSG_LEVEL[w + 1], o, m, bl);21 printf("[%s] %s.%s: %s\n", MSG_LEVEL[w + 1], o, m, bl); 22 22 #endif 23 23 } -
cpp/frams/errmgr/stdouterr.h
r286 r336 9 9 #include <frams/virtfile/virtfile.h> 10 10 11 class StdoutErrorHandler : public ErrorHandlerBase11 class StdoutErrorHandler : public ErrorHandlerBase 12 12 { 13 VirtFILE *file;14 15 StdoutErrorHandler(int opts=DontBlock,VirtFILE *_file=NULL):ErrorHandlerBase(opts),file(_file) {}16 void handle(const char *o,const char *m,const char *bl,int w);13 VirtFILE *file; 14 public: 15 StdoutErrorHandler(int opts = DontBlock, VirtFILE *_file = NULL) :ErrorHandlerBase(opts), file(_file) {} 16 void handle(const char *o, const char *m, const char *bl, int w); 17 17 }; 18 18
Note: See TracChangeset
for help on using the changeset viewer.