- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

# LogFilter.h

Go to the documentation of this file.

    00001
    00015 #pragma once
    00016
    00017 #include "LogLevel.h"
    00018 #include "LogInfo.h"
    00019 #include "RegistryKey.h"
    00020 #include "TStd.h"
    00021
    00022 #include <boost/algorithm/string/erase.hpp>
    00023 #include <boost/foreach.hpp>
    00024
    00025 #include <map>
    00026
    00027 #define LOGGING_KEYNAME_DEFAULT_LOG_LEVEL _T("DefaultLogLevel")
    00028 #define LOGGING_KEYNAME_ORIGIN_RULES _T("OriginRules")
    00029
    00030 namespace Nvidia {
    00031 namespace Logging {
    00032
    00039 class LogFilter
    00040 {
    00041 private:
    00052     std::map<std::tstring, LogLevel> OriginRules;
    00053     typedef std::map<std::tstring, LogLevel>::const_iterator OriginRulesIterator;
    00054
    00056     LogLevel DefaultLogLevel;
    00057
    00058 public:
    00066     LogFilter(const RegistryKey& key);
    00067
    00073     LogFilter(LogLevel defaultLogLevel);
    00074
    00076     bool IsFiltered(const LogInfo& logInfo);
    00077
    00092     LogLevel GetMaximumLogLevel(const std::tstring& scopeLoggerName);
    00093
    00094 private:
    00096     void StripLastPart(std::tstring& originPrefix);
    00097 };
    00098
    00099 inline LogFilter::LogFilter(LogLevel defaultLogLevel) : DefaultLogLevel(defaultLogLevel) {}
    00100
    00101 inline LogFilter::LogFilter(const RegistryKey& key)
    00102 {
    00103     DefaultLogLevel = (LogLevel) key.GetDWORDValue(LOGGING_KEYNAME_DEFAULT_LOG_LEVEL);
    00104     const RegistryKey& originRulesKey = key.GetSubKey(LOGGING_KEYNAME_ORIGIN_RULES);
    00105
    00106     BOOST_FOREACH(std::tstring origin, originRulesKey.GetValueNames())
    00107     {
    00108         OriginRules[origin] = (LogLevel) originRulesKey.GetDWORDValue(origin);
    00109     }
    00110 }
    00111
    00112 inline bool LogFilter::IsFiltered(const LogInfo& logInfo)
    00113 {
    00114     return logInfo.GetLevel() > GetMaximumLogLevel(logInfo.GetScopeLoggerName());
    00115 }
    00116
    00117 inline void LogFilter::StripLastPart(std::tstring& originPrefix)
    00118 {
    00119     size_t lastDot = originPrefix.find_last_of(_T('.'));
    00120
    00121     if ( lastDot == std::tstring::npos )
    00122     {
    00123         originPrefix = _T("");
    00124     }
    00125     else
    00126     {
    00127         // Boost documentation is self-contradictory with regards to what the second parameter means:
    00128         // http://www.boost.org/doc/libs/1_38_0/doc/html/boost/algorithm/erase_tail.html
    00129         // passing length of the *tail*, not the head, seems to produce desired result.
    00130         // I've submitted a bug report to boost tracker regarding this issue: https://svn.boost.org/trac/boost/ticket/3314
    00131         boost::algorithm::erase_tail(originPrefix, (int)(originPrefix.length() - lastDot));
    00132         // boost::algorithm::erase_tail(originPrefix, lastDot);
    00133     }
    00134 }
    00135
    00136 inline LogLevel LogFilter::GetMaximumLogLevel(const std::tstring& origin)
    00137 {
    00138     std::tstring originPrefix = origin;
    00139     OriginRulesIterator iterator;
    00140
    00141     while (originPrefix.size() > 0)
    00142     {
    00143         if ( (iterator = OriginRules.find(originPrefix)) != OriginRules.end() )
    00144         {
    00145             OriginRules[origin] = iterator->second;
    00146             return iterator->second;
    00147         }
    00148         StripLastPart(originPrefix);
    00149     }
    00150
    00151     return DefaultLogLevel;
    00152 }
    00153
    00154 }
    00155 }