Changeset 149


Ignore:
Timestamp:
05/15/09 12:30:27 (3 years ago)
Author:
ddangelo
Message:

Logging Macro, which can controlled via LOG_LEVEL options in localdefs added

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/avango-build/src/avango/build/__init__.py

    r136 r149  
    148148        scons.BoolOption('DEBUG', 'Debug build option', False), 
    149149        scons.BoolOption('TRACE_LOGGING', 'Enable/Disable trace log level', False), 
     150        scons.EnumOption('LOG_LEVEL', 'Support for higher log levels will be compiled out', "DEBUG", 
     151                         ("FATAL", "ERROR", "WARN", "INFO", "DEBUG", "TRACE")), 
    150152        scons.PathOption('BINARY_PATH', 
    151153                   'Additional binary search paths', 
  • trunk/avango-core/SConscript

    r1 r149  
    3333    'PREFIX': avango.build.get_prefix().abspath, 
    3434    'AVANGO_DEBUG': int(local_env['DEBUG']), 
     35    'AVANGO_LOG_LEVEL': local_env['LOG_LEVEL'], 
    3536    'AVANGO_DISTRIBUTION_SUPPORT': int(local_env['DISTRIBUTION_SUPPORT']), 
    3637    'PKG_CONFIG_REQUIRES': '', 
  • trunk/avango-core/include/avango/Config.h.in

    r1 r149  
    3838#endif 
    3939 
     40#define AVANGO_LOG_LEVEL av::logging::%(AVANGO_LOG_LEVEL)s 
     41 
    4042#if %(AVANGO_DISTRIBUTION_SUPPORT)s 
    4143#define AVANGO_DISTRIBUTION_SUPPORT 1 
  • trunk/avango-core/include/avango/logging/Logger.h

    r1 r149  
    4545 
    4646#include <avango/Assert.h> 
     47#include <avango/Config.h> 
    4748#include <avango/logging/Level.h> 
    4849#include <avango/logging/LoggerManager.h> 
     
    243244       * Returns the name of this logger. 
    244245       */ 
    245       AV_DLL const std::string& getName(); 
     246      AV_DLL const std::string& getName() const; 
    246247 
    247248      /** 
    248249       * Returns true if the logger has a parent logger. 
    249250       */ 
    250       AV_DLL bool hasParent(); 
     251      AV_DLL bool hasParent() const; 
    251252 
    252253      /** 
     
    257258 
    258259      /** 
     260       * Returns the parent logger of this logger. 
     261       * \throw std::logic_error If the logger has no parents (which means it is the root logger). 
     262       */ 
     263      AV_DLL const Logger& getParent() const; 
     264 
     265      /** 
    259266       * Get level of this logger. Only messages >= this level are logged 
    260267       */ 
    261       AV_DLL Level getLevel(); 
     268      AV_DLL Level getLevel() const; 
    262269 
    263270      /** 
     
    294301       */ 
    295302      AV_DLL void removeAllAppenders(); 
     303 
     304      /** 
     305       * Check if this logger (or one of its parents) has at least one appender 
     306       */ 
     307      AV_DLL bool isActive(Level level) const; 
    296308 
    297309      /** 
     
    405417#endif 
    406418 
     419#define AVANGO_LOG(logger, level, message)\ 
     420  if ((level <= AVANGO_LOG_LEVEL) && (logger.isActive(level)))\ 
     421    logger.log(level, message); 
     422 
    407423#endif // #if !defined(AVANGO_LOGGING_LOGGER_H) 
  • trunk/avango-core/src/avango/logging/Logger.cpp

    r1 r149  
    160160 
    161161const std::string& 
    162 av::logging::Logger::getName() 
     162av::logging::Logger::getName() const 
    163163{ 
    164164  return mName; 
     
    166166 
    167167bool 
    168 av::logging::Logger::hasParent() 
     168av::logging::Logger::hasParent() const 
    169169{ 
    170170  return mParent; 
    171171} 
    172172 
    173 av::logging::Logger& 
    174 av::logging::Logger::getParent() 
     173const av::logging::Logger& 
     174av::logging::Logger::getParent() const 
    175175{ 
    176176  if (mParent) 
     
    186186} 
    187187 
     188av::logging::Logger& 
     189av::logging::Logger::getParent() 
     190{ 
     191  if (mParent) 
     192  { 
     193    return *mParent; 
     194  } 
     195  else 
     196  { 
     197    AV_ASSERT(this == &getRootLogger()); 
     198    throw std::logic_error("av::logging::Logger::getParent: Logger has no parent."); 
     199    return *this; 
     200  } 
     201} 
     202 
    188203av::logging::Level 
    189 av::logging::Logger::getLevel() 
     204av::logging::Logger::getLevel() const 
    190205{ 
    191206  return mLevel; 
     
    229244  boost::mutex::scoped_lock lock(mAppenderMutex); 
    230245  mAppenders.clear(); 
     246} 
     247 
     248bool 
     249av::logging::Logger::isActive(Level level) const 
     250{ 
     251  return ( (!mAppenders.empty() && level <= mLevel) || getParent().isActive(level)); 
    231252} 
    232253 
  • trunk/avango-core/src/avango/logging/tests/TestLogging.cpp

    r1 r149  
    192192  } 
    193193 
     194  TEST(isActive) 
     195  { 
     196    av::Logger& child = av::Logger::getLogger("useLoggerHierarchy::Node"); 
     197    av::Logger& parent = av::Logger::getLogger("useLoggerHierarchy"); 
     198 
     199    boost::shared_ptr<av::logging::Appender> file_app(new av::logging::FileAppender(filename)); 
     200    parent.addAppender(file_app); 
     201 
     202    parent.setLevel(av::logging::ERROR); 
     203    child.setLevel(av::logging::FATAL); 
     204 
     205    CHECK(child.isActive(av::logging::ERROR)); 
     206  } 
     207 
     208  TEST(LOGMacroSuccessfulLogging) 
     209  { 
     210    av::Logger& logger = av::Logger::getLogger("useLoggerHierarchy"); 
     211    std::ostringstream os; 
     212    boost::shared_ptr<av::logging::Appender> stream_app(new av::logging::StreamAppender(os)); 
     213    logger.addAppender(stream_app); 
     214    logger.setLevel(av::logging::ERROR); 
     215 
     216    AVANGO_LOG(logger, av::logging::ERROR, "Test"); 
     217 
     218    CHECK(!os.str().empty()); 
     219  } 
     220 
     221  TEST(LOGMacroUnsuccessfulLogging) 
     222  { 
     223    av::Logger& logger = av::Logger::getLogger("useLoggerHierarchy"); 
     224    std::ostringstream os; 
     225    boost::shared_ptr<av::logging::Appender> stream_app(new av::logging::StreamAppender(os)); 
     226    logger.addAppender(stream_app); 
     227    logger.setLevel(av::logging::ERROR); 
     228 
     229    AVANGO_LOG(logger, av::logging::WARN, "Test"); 
     230 
     231    CHECK(os.str().empty()); 
     232  } 
     233 
    194234} // namespace 
    195235 
     
    198238int main() 
    199239{ 
    200   //av::getRootLogger().addConsoleAppender(); 
    201240  return UnitTest::RunAllTests(); 
    202241} 
Note: See TracChangeset for help on using the changeset viewer.