Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Aug 11, 2011, 11:20:39 PM (13 years ago)
Author:
landauf
Message:

A context is now defined by a struct instead of only a mask.

Introduced sub-contexts. Sub-contexts of the same main-context share the same mask, but have a different ID.
Main-contexts are filtered using a bitmask which happens for every line of output and is very fast.
Sub-contexts are filtered using a set which is slow but happens only if a specific sub-context is enabled in the config file which is usually not the case.

The concept of filtering normal output + additional contexts was moved from BaseWriter directly to OutputListener and OutputManager which makes the whole system faster.
BaseWriter now calls registerContext() for each configured output context, which basically allows the usage of more than 64 contexts as long as these contexts are not used before loading the config file. Though by design it's not recommended.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/output/src/libraries/util/output/OutputManager.cc

    r8808 r8833  
    3232#include "ConsoleWriter.h"
    3333#include "LogWriter.h"
     34#include "util/Output.h"
    3435#include "util/StringUtils.h"
    3536
     
    3940    {
    4041        this->combinedLevelMask_ = level::none;
    41         this->combinedContextMask_ = 0;
     42        this->combinedAdditionalContextsLevelMask_ = level::none;
     43        this->combinedAdditionalContextsMask_ = context::none;
     44
     45        this->subcontextCounter_ = 0;
    4246    }
    4347
     
    6367    }
    6468
    65     void OutputManager::pushMessage(OutputLevel level, OutputContext context, const std::string& message)
     69    void OutputManager::pushMessage(OutputLevel level, const OutputContextContainer& context, const std::string& message)
    6670    {
    6771        std::vector<std::string> lines;
     
    9498    {
    9599        this->updateCombinedLevelMask();
    96         this->updateCombinedContextMask();
     100        this->updateCombinedAdditionalContextsLevelMask();
     101        this->updateCombinedAdditionalContextsMask();
    97102    }
    98103
     
    105110    }
    106111
    107     void OutputManager::updateCombinedContextMask()
     112    void OutputManager::updateCombinedAdditionalContextsLevelMask()
    108113    {
    109         this->combinedContextMask_ = 0;
     114        int mask = 0;
    110115        for (size_t i = 0; i < this->listeners_.size(); ++i)
    111             this->combinedContextMask_ |= this->listeners_[i]->getContextMask();
     116            mask |= this->listeners_[i]->getAdditionalContextsLevelMask();
     117        this->combinedAdditionalContextsLevelMask_ = static_cast<OutputLevel>(mask);
    112118    }
    113119
    114     OutputContext OutputManager::registerContext(const std::string& name)
     120    void OutputManager::updateCombinedAdditionalContextsMask()
    115121    {
    116         boost::bimap<OutputContext, std::string>::right_map::iterator it = this->contexts_.right.find(name);
    117         if (it == this->contexts_.right.end())
     122        this->combinedAdditionalContextsMask_ = 0;
     123        for (size_t i = 0; i < this->listeners_.size(); ++i)
     124            this->combinedAdditionalContextsMask_ |= this->listeners_[i]->getAdditionalContextsMask();
     125    }
     126
     127    const OutputContextContainer& OutputManager::registerContext(const std::string& name, const std::string& subname)
     128    {
     129        std::string full_name = name;
     130        if (subname != "")
     131            full_name += "::" + subname;
     132
     133        std::map<std::string, OutputContextContainer>::iterator it_container = this->contextContainers_.find(full_name);
     134        if (it_container != this->contextContainers_.end())
     135            return it_container->second;
     136
     137        OutputContextContainer container;
     138        container.name = full_name;
     139
     140        std::map<std::string, OutputContextMask>::iterator it_mask = this->contextMasks_.find(name);
     141        if (it_mask != this->contextMasks_.end())
    118142        {
    119             OutputContext context = 0x1 << this->contexts_.size();
    120             this->contexts_.insert(boost::bimap<OutputContext, std::string>::value_type(context, name));
    121             return context;
     143            container.mask = it_mask->second;
    122144        }
    123145        else
    124146        {
    125             return it->second;
     147            container.mask = static_cast<OutputContextMask>(0x1) << this->contextMasks_.size();
     148            this->contextMasks_[name] = container.mask;
     149
     150            if (container.mask == 0)
     151                orxout(internal_warning) << "More than " << sizeof(OutputContextMask) * 8 << " output contexts defined. Context '" << name << "' might not get filtered correctly" << endl;
    126152        }
     153
     154        if (subname == "")
     155            container.sub_id = context::no_subcontext;
     156        else
     157            container.sub_id = ++this->subcontextCounter_; // start with 1
     158
     159        return (this->contextContainers_[full_name] = container);
    127160    }
    128161
    129     OutputContext registerContext(const std::string& name)
     162    const OutputContextContainer& registerContext(const std::string& name, const std::string& subname)
    130163    {
    131         return OutputManager::getInstance().registerContext(name);
     164        return OutputManager::getInstance().registerContext(name, subname);
    132165    }
    133166
     
    154187    }
    155188
    156     const std::string& OutputManager::getContextName(OutputContext context) const
     189    std::string OutputManager::getDefaultPrefix(OutputLevel level, const OutputContextContainer& context) const
    157190    {
    158         if (context != context::undefined())
    159         {
    160             boost::bimap<OutputContext, std::string>::left_map::const_iterator it = this->contexts_.left.find(context);
    161             if (it != this->contexts_.left.end())
    162                 return it->second;
    163         }
    164         return BLANKSTRING;
    165     }
     191        static OutputContextMask undefined_mask = context::undefined().mask;
    166192
    167     OutputContext OutputManager::getContextValue(const std::string& name) const
    168     {
    169         boost::bimap<OutputContext, std::string>::right_map::const_iterator it = this->contexts_.right.find(name);
    170         if (it != this->contexts_.right.end())
    171             return it->second;
    172         else
    173             return context::none;
    174     }
     193        std::string prefix = this->getLevelName(level) + ": ";
     194        if (context.mask != undefined_mask)
     195            prefix += "[" + context.name + "] ";
    175196
    176     std::string OutputManager::getComposedContextName(OutputContext context) const
    177     {
    178         std::string name;
    179         size_t counter = 0;
    180         for (OutputContext context_test = 0x1; context_test != 0x0; context_test = context_test << 1)
    181         {
    182             if (context & context_test)
    183             {
    184                 boost::bimap<OutputContext, std::string>::left_map::const_iterator it = this->contexts_.left.find(context_test);
    185                 if (it != this->contexts_.left.end())
    186                 {
    187                     if (counter)
    188                         name += ", ";
    189 
    190                     name += it->second;
    191                     ++counter;
    192                 }
    193             }
    194         }
    195         return name;
    196     }
    197 
    198     std::string OutputManager::getDefaultPrefix(OutputLevel level, OutputContext context) const
    199     {
    200         std::string prefix = this->getLevelName(level) + ": ";
    201         if (context != context::undefined())
    202         {
    203             std::string context_name = this->getContextName(context);
    204             if (context_name == "")
    205                 context_name = this->getComposedContextName(context);
    206             prefix += "[" + context_name + "] ";
    207         }
    208197        return prefix;
    209198    }
Note: See TracChangeset for help on using the changeset viewer.