Changeset 378 for cpp


Ignore:
Timestamp:
04/27/15 04:44:23 (10 years ago)
Author:
Maciej Komosinski
Message:

Improved argument variable names; changed implementation to use STL classes (vector, string)

Location:
cpp/frams/loggers
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • cpp/frams/loggers/loggers.cpp

    r375 r378  
    66#include <common/stl-util.h>
    77
    8 void _logMessageSingleLine(const char *o, const char *m, int w, const char *txt)
     8void _logMessageSingleLine(const char *obj, const char *method, int level, const char *msg)
    99{
    10         tlsGetRef(message_handler_manager_instance).send(o, m, w, txt);
     10        tlsGetRef(message_handler_manager_instance).send(obj, method, level, msg);
    1111}
    1212
    1313THREAD_LOCAL_DEF(LoggerManager, message_handler_manager_instance);
    1414
    15 void LoggerManager::send(int level, const char *o, const char *m, int w, const char *bl)
     15void LoggerManager::send(int position, const char *obj, const char *method, int level, const char *msg)
    1616{
    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--)
    2020        {
    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)))
    2424                {
    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;
    2727                }
    2828        }
    2929}
    3030
    31 int LoggerManager::add(LoggerBase *h)
     31int LoggerManager::add(LoggerBase *logger)
    3232{
    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;
    3636}
    3737
    3838void LoggerManager::remove(int i)
    3939{
    40         LoggerBase *h = handlers(i);
     40        LoggerBase *h = loggers[i];
    4141        h->manager = NULL;
    42         handlers.remove(i);
     42        loggers.erase(loggers.begin() + i);
    4343}
    4444
    45 void LoggerManager::remove(LoggerBase *h)
     45void LoggerManager::remove(LoggerBase *logger)
    4646{
    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);
    5050}
    5151
    5252void LoggerManager::removeAll()
    5353{
    54         while (handlers.size() > 0)
    55                 remove(handlers.size() - 1);
     54        while (loggers.size() > 0)
     55                remove(loggers.size() - 1);
    5656}
    5757
    5858//////////////////////////////////
    5959
    60 void LoggerBase::send(const char *o, const char *m, int w, const char *bl)
     60void LoggerBase::send(const char *obj, const char *method, int level, const char *msg)
    6161{
    6262        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);
    6565}
    6666
    67 void LoggerBase::logPrintf(const char *o, const char *m, int w, const char *bl, ...)
     67void LoggerBase::logPrintf(const char *obj, const char *method, int level, const char *msg, ...)
    6868{
    6969        if (!isEnabled()) return;
    7070        string buf;
    7171        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);
    7474        va_end(argptr);
    75         send(o, m, w, buf.c_str());
     75        send(obj, method, level, buf.c_str());
    7676}
    7777
     
    103103/////////////////////////////////
    104104
    105 void LoggerToMemory::handle(const char *o, const char *m, int w, const char *bl)
     105void LoggerToMemory::handle(const char *obj, const char *method, int level, const char *msg)
    106106{
    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++;
    111111
    112         if (w >= minleveltostore)
     112        if (level >= minleveltostore)
    113113        {
    114114                storedcount++;
    115115                if (options & (StoreFirstMessage | StoreAllMessages))
    116116                {
    117                         if (!((options&StoreFirstMessage) && (msgs.len() > 0)))
     117                        if (!((options&StoreFirstMessage) && (msgs.length() > 0)))
    118118                        {
    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);
    121121                        }
    122122                }
  • cpp/frams/loggers/loggers.h

    r375 r378  
    33// See LICENSE.txt for details.
    44
    5 #ifndef _FRAMS_LOGGERS_H_
    6 #define _FRAMS_LOGGERS_H_
     5#ifndef _COMMON_LOGGERS_H_
     6#define _COMMON_LOGGERS_H_
    77
    8 #include <frams/util/list.h>
    9 #include <frams/util/sstring.h>
    108#include <common/log.h>
    119#include <common/threads.h>
     10#include <common/nonstd_stl.h>
    1211
    1312class LoggerBase;
     
    1615{
    1716        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);
    2019public:
    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);
    2326        void remove(int i);
    24         void remove(LoggerBase *r);
     27        void remove(LoggerBase *logger);
    2528        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)
    2730        {
    28                 send(handlers.size() - 1, o, m, w, bl);
     31                send(loggers.size() - 1, obj, method, level, msg);
    2932        }
    3033        ~LoggerManager() { removeAll(); }
     
    5053
    5154        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);
    5356
    5457        bool isEnabled() { return manager ? true : false; }
     
    6871        }
    6972
    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) {}
    7174};
    7275
     
    7679{
    7780protected:
    78         int maxlevel, minleveltostore, errcount, warncount, storedcount, infocount;
    79         SString msgs;
     81        int maxlevel, minleveltostore, errcount, warncount, infocount, storedcount;
     82        string msgs;
    8083
    8184public:
    8285
    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 = ""; }
    8487
    8588        enum Options2
     
    9396        int getStoredCount()      { return storedcount; }
    9497        int getErrorLevel()       { return maxlevel; }
    95         const SString& getMessages() { return msgs; }
     98        const string getMessages() { return msgs; }
    9699
    97100        LoggerToMemory(int opts = 0, int minimal_level_to_store = LOG_ERROR) :LoggerBase(opts), minleveltostore(minimal_level_to_store)
     
    100103        }
    101104
    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);
    103106};
    104107
     
    107110        LoggerManager *other_manager;
    108111public:
    109         RedirectingLogger(LoggerManager *other_mgr,int opts=0)
     112        RedirectingLogger(LoggerManager *other_mgr, int opts = 0)
    110113                :LoggerBase(opts), other_manager(other_mgr) {}
    111114
    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)
    113116        {
    114                 other_manager->send(o, m, w, bl);
     117                other_manager->send(obj, method, level, msg);
    115118        }
    116119};
  • cpp/frams/loggers/loggertostdout.cpp

    r375 r378  
    1010#endif
    1111
    12 void LoggerToStdout::handle(const char *o, const char *m, int w, const char *bl)
     12void LoggerToStdout::handle(const char *obj, const char *method, int level, const char *msg)
    1313{
    14         if (w < -1) w = -1; else if (w>3) w = 3;
     14        if (level < -1) level = -1; else if (level>3) level = 3;
    1515#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);
    1717#else
    1818        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);
    2020        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);
    2222#endif
    2323}
  • cpp/frams/loggers/loggertostdout.h

    r375 r378  
    1414public:
    1515        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);
    1717};
    1818
Note: See TracChangeset for help on using the changeset viewer.