Changeset 378
- Timestamp:
- 04/27/15 04:44:23 (10 years ago)
- Location:
- cpp/frams/loggers
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
cpp/frams/loggers/loggers.cpp
r375 r378 6 6 #include <common/stl-util.h> 7 7 8 void _logMessageSingleLine(const char *o , const char *m, int w, const char *txt)8 void _logMessageSingleLine(const char *obj, const char *method, int level, const char *msg) 9 9 { 10 tlsGetRef(message_handler_manager_instance).send(o , m, w, txt);10 tlsGetRef(message_handler_manager_instance).send(obj, method, level, msg); 11 11 } 12 12 13 13 THREAD_LOCAL_DEF(LoggerManager, message_handler_manager_instance); 14 14 15 void LoggerManager::send(int level, const char *o, const char *m, int w, const char *bl)15 void LoggerManager::send(int position, const char *obj, const char *method, int level, const char *msg) 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 (position >= (int)loggers.size()) position = loggers.size() - 1; 18 bool blocked = false; 19 for (int i = position; i >= 0; i--) 20 20 { 21 LoggerBase * r = handlers(i);22 if ((!( r->options & LoggerBase::Paused)) &&23 ((!blocked) || ( r->options & LoggerBase::CannotBeBlocked)))21 LoggerBase *logger = loggers[i]; 22 if ((!(logger->options & LoggerBase::Paused)) && 23 ((!blocked) || (logger->options & LoggerBase::CannotBeBlocked))) 24 24 { 25 r->handle(o, m, w, bl);26 if (!( r->options & LoggerBase::DontBlock)) blocked = 1;25 logger->handle(obj, method, level, msg); 26 if (!(logger->options & LoggerBase::DontBlock)) blocked = true; 27 27 } 28 28 } 29 29 } 30 30 31 int LoggerManager::add(LoggerBase * h)31 int LoggerManager::add(LoggerBase *logger) 32 32 { 33 h->manager = this;34 handlers += h;35 return handlers.size() - 1;33 logger->manager = this; 34 loggers.push_back(logger); 35 return loggers.size() - 1; 36 36 } 37 37 38 38 void LoggerManager::remove(int i) 39 39 { 40 LoggerBase *h = handlers(i);40 LoggerBase *h = loggers[i]; 41 41 h->manager = NULL; 42 handlers.remove(i);42 loggers.erase(loggers.begin() + i); 43 43 } 44 44 45 void LoggerManager::remove(LoggerBase * h)45 void LoggerManager::remove(LoggerBase *logger) 46 46 { 47 int i ;48 if ( (i = handlers.find(h)) < 0) return;49 remove(i);47 int index = find(logger); 48 if (index >= 0) 49 remove(index); 50 50 } 51 51 52 52 void LoggerManager::removeAll() 53 53 { 54 while ( handlers.size() > 0)55 remove( handlers.size() - 1);54 while (loggers.size() > 0) 55 remove(loggers.size() - 1); 56 56 } 57 57 58 58 ////////////////////////////////// 59 59 60 void LoggerBase::send(const char *o , const char *m, int w, const char *bl)60 void LoggerBase::send(const char *obj, const char *method, int level, const char *msg) 61 61 { 62 62 if (!isEnabled()) return; 63 int level= manager->find(this);64 if ( level >= 0) manager->send(level - 1, o, m, w, bl);63 int position = manager->find(this); 64 if (position >= 0) manager->send(position - 1, obj, method, level, msg); 65 65 } 66 66 67 void LoggerBase::logPrintf(const char *o , const char *m, int w, const char *bl, ...)67 void LoggerBase::logPrintf(const char *obj, const char *method, int level, const char *msg, ...) 68 68 { 69 69 if (!isEnabled()) return; 70 70 string buf; 71 71 va_list argptr; 72 va_start(argptr, bl);73 buf = ssprintf_va( bl, argptr);72 va_start(argptr, msg); 73 buf = ssprintf_va(msg, argptr); 74 74 va_end(argptr); 75 send(o , m, w, buf.c_str());75 send(obj, method, level, buf.c_str()); 76 76 } 77 77 … … 103 103 ///////////////////////////////// 104 104 105 void LoggerToMemory::handle(const char *o , const char *m, int w, const char *bl)105 void LoggerToMemory::handle(const char *obj, const char *method, int level, const char *msg) 106 106 { 107 if ( w > maxlevel) maxlevel = w;108 if ( w>= LOG_INFO) infocount++;109 if ( w>= LOG_WARN) warncount++;110 if ( w>= LOG_ERROR) errcount++;107 if (level > maxlevel) maxlevel = level; 108 if (level >= LOG_INFO) infocount++; 109 if (level >= LOG_WARN) warncount++; 110 if (level >= LOG_ERROR) errcount++; 111 111 112 if ( w>= minleveltostore)112 if (level >= minleveltostore) 113 113 { 114 114 storedcount++; 115 115 if (options & (StoreFirstMessage | StoreAllMessages)) 116 116 { 117 if (!((options&StoreFirstMessage) && (msgs.len () > 0)))117 if (!((options&StoreFirstMessage) && (msgs.length() > 0))) 118 118 { 119 if (msgs.len () > 0) msgs += '\n';120 msgs += SString::sprintf(LOG_FORMAT,LOG_LEVEL[w+1],o,m,bl);119 if (msgs.length() > 0) msgs += '\n'; 120 msgs += ssprintf(LOG_FORMAT, LOG_LEVEL[level + 1], obj, method, msg); 121 121 } 122 122 } -
cpp/frams/loggers/loggers.h
r375 r378 3 3 // See LICENSE.txt for details. 4 4 5 #ifndef _ FRAMS_LOGGERS_H_6 #define _ FRAMS_LOGGERS_H_5 #ifndef _COMMON_LOGGERS_H_ 6 #define _COMMON_LOGGERS_H_ 7 7 8 #include <frams/util/list.h>9 #include <frams/util/sstring.h>10 8 #include <common/log.h> 11 9 #include <common/threads.h> 10 #include <common/nonstd_stl.h> 12 11 13 12 class LoggerBase; … … 16 15 { 17 16 friend class LoggerBase; 18 SListTempl<LoggerBase*> handlers;19 void send(int level, const char *o, const char *m, int w, const char *bl);17 vector<LoggerBase*> loggers; 18 void send(int position, const char *obj, const char *method, int level, const char *msg); 20 19 public: 21 int find(LoggerBase *r) { return handlers.find(r); } 22 int add(LoggerBase *r); 20 int find(LoggerBase *logger) 21 { 22 vector<LoggerBase*>::iterator it = std::find(loggers.begin(), loggers.end(), logger); 23 return it == loggers.end() ? -1 : std::distance(loggers.begin(), it); 24 } 25 int add(LoggerBase *logger); 23 26 void remove(int i); 24 void remove(LoggerBase * r);27 void remove(LoggerBase *logger); 25 28 void removeAll(); 26 void send(const char *o , const char *m, int w, const char *bl)29 void send(const char *obj, const char *method, int level, const char *msg) 27 30 { 28 send( handlers.size() - 1, o, m, w, bl);31 send(loggers.size() - 1, obj, method, level, msg); 29 32 } 30 33 ~LoggerManager() { removeAll(); } … … 50 53 51 54 void logPrintf(const char *o, const char *m, int w, const char *bl, ...); 52 void send(const char *o , const char *m, int w, const char *bl);55 void send(const char *obj, const char *method, int level, const char *msg); 53 56 54 57 bool isEnabled() { return manager ? true : false; } … … 68 71 } 69 72 70 virtual void handle(const char *o , const char *m, int w, const char *bl) {}73 virtual void handle(const char *obj, const char *method, int level, const char *msg) {} 71 74 }; 72 75 … … 76 79 { 77 80 protected: 78 int maxlevel, minleveltostore, errcount, warncount, storedcount, infocount;79 SString msgs;81 int maxlevel, minleveltostore, errcount, warncount, infocount, storedcount; 82 string msgs; 80 83 81 84 public: 82 85 83 void reset() { maxlevel = LOG_INFO - 1; errcount = warncount = storedcount = infocount = 0; msgs = 0; }86 void reset() { maxlevel = LOG_INFO - 1; errcount = warncount = infocount = storedcount = 0; msgs = ""; } 84 87 85 88 enum Options2 … … 93 96 int getStoredCount() { return storedcount; } 94 97 int getErrorLevel() { return maxlevel; } 95 const SString&getMessages() { return msgs; }98 const string getMessages() { return msgs; } 96 99 97 100 LoggerToMemory(int opts = 0, int minimal_level_to_store = LOG_ERROR) :LoggerBase(opts), minleveltostore(minimal_level_to_store) … … 100 103 } 101 104 102 void handle(const char *o , const char *m, int w, const char *bl);105 void handle(const char *obj, const char *method, int level, const char *msg); 103 106 }; 104 107 … … 107 110 LoggerManager *other_manager; 108 111 public: 109 RedirectingLogger(LoggerManager *other_mgr, int opts=0)112 RedirectingLogger(LoggerManager *other_mgr, int opts = 0) 110 113 :LoggerBase(opts), other_manager(other_mgr) {} 111 114 112 void handle(const char *o , const char *m, int w, const char *bl)115 void handle(const char *obj, const char *method, int level, const char *msg) 113 116 { 114 other_manager->send(o , m, w, bl);117 other_manager->send(obj, method, level, msg); 115 118 } 116 119 }; -
cpp/frams/loggers/loggertostdout.cpp
r375 r378 10 10 #endif 11 11 12 void LoggerToStdout::handle(const char *o , const char *m, int w, const char *bl)12 void LoggerToStdout::handle(const char *obj, const char *method, int level, const char *msg) 13 13 { 14 if ( w < -1) w = -1; else if (w>3) w= 3;14 if (level < -1) level = -1; else if (level>3) level = 3; 15 15 #ifdef SHP 16 AppLog(LOG_FORMAT "\n",LOG_LEVEL[ w+1],o,m,bl);16 AppLog(LOG_FORMAT "\n",LOG_LEVEL[level+1],obj,method,msg); 17 17 #else 18 18 if (file) 19 file->printf(LOG_FORMAT "\n", LOG_LEVEL[ w + 1], o, m, bl);19 file->printf(LOG_FORMAT "\n", LOG_LEVEL[level + 1], obj, method, msg); 20 20 else 21 printf(LOG_FORMAT "\n", LOG_LEVEL[ w + 1], o, m, bl);21 printf(LOG_FORMAT "\n", LOG_LEVEL[level + 1], obj, method, msg); 22 22 #endif 23 23 } -
cpp/frams/loggers/loggertostdout.h
r375 r378 14 14 public: 15 15 LoggerToStdout(int opts = 0, VirtFILE *_file = NULL) :LoggerBase(opts), file(_file) {} 16 void handle(const char *o , const char *m, int w, const char *bl);16 void handle(const char *obj, const char *method, int level, const char *msg); 17 17 }; 18 18
Note: See TracChangeset
for help on using the changeset viewer.