Kaya Kupferschmidt avatar Kaya Kupferschmidt committed 1802c48

Added log level inheritance to logging

Comments (0)

Files changed (7)

source/libs/magnum_logging/source/magnum/logging/AbstractLogger.cpp

  */
 LogLevel::Level AbstractLogger::getEffectiveLevel() const 
 {
-	return m_Level;
+    return m_Level != LogLevel::Inherit ? m_Level : LogLevel::None;
+}
+
+
+/*--------------------------------------------------------------------------*/
+/**
+ */
+Logger* AbstractLogger::getParent() const
+{
+    return 0;
 }
 
 

source/libs/magnum_logging/source/magnum/logging/AbstractLogger.h

 	virtual LogLevel::Level getEffectiveLevel() const;
     virtual LogCategory getCategory() const;
 
+    virtual Logger* getParent() const;
+
 private:
     LogCategory m_Category;
     LogLevel::Level m_Level;

source/libs/magnum_logging/source/magnum/logging/HierarchicalLogger.cpp

  */
 LogLevel::Level HierarchicalLogger::getEffectiveLevel() const 
 {
-    LogLevel::Level level = AbstractLogger::getEffectiveLevel();
-    if (m_Parent != Nil) {
-        LogLevel::Level parentLevel = m_Parent->getEffectiveLevel();
-        if (parentLevel < level)
-            level = parentLevel;
+    LogLevel::Level level = AbstractLogger::getLevel();
+    if (level == LogLevel::Inherit) {
+        if (m_Parent != Nil) {
+            level = m_Parent->getEffectiveLevel();
+        }
+        else {
+            level = LogLevel::None;
+        }
     }
 	return level;
 }

source/libs/magnum_logging/source/magnum/logging/HierarchicalLogger.h

     virtual void logRecord(const LogRecord& );
 
 	virtual LogLevel::Level	getEffectiveLevel() const;
-	
-    Logger* getParent() const;
+    virtual Logger* getParent() const;
 	
 private:
     Reference<Logger> m_Parent;

source/libs/magnum_logging/source/magnum/logging/LogLevel.h

 		Error		= magnum::debug::DebugLevel::Error,
 		Fatal		= magnum::debug::DebugLevel::Fatal,
 		None		= magnum::debug::DebugLevel::None,
+        Inherit     = -1
 	};
 	
 public:

source/libs/magnum_logging/source/magnum/logging/Logger.h

 	virtual LogLevel::Level	getLevel() const = 0;
 	virtual LogLevel::Level	getEffectiveLevel() const = 0;
     virtual LogCategory getCategory() const = 0;
+
+    virtual Logger* getParent() const = 0;
 };
 
 

source/unittest/logging/Test_Logging.h

     LoggerProvider* mgr = LoggerProvider::getProvider();
     Logger* root = mgr->getRootLogger();
     Logger* logger_abc = mgr->getLogger(LogCategory("abc"));
-	TEST_ASSERT(logger_abc != 0);
-    TEST_ASSERT(mgr->getLogger(LogCategory("abc")) == logger_abc);
-	TEST_ASSERT(dynamic_cast<HierarchicalLogger*>(logger_abc) != 0);
-	TEST_ASSERT(dynamic_cast<HierarchicalLogger*>(logger_abc)->getParent() == root);
+    TEST_ASSERT( logger_abc != 0);
+    TEST_ASSERT( mgr->getLogger(LogCategory("abc")) == logger_abc);
+    TEST_ASSERT( dynamic_cast<HierarchicalLogger*>(logger_abc) != 0);
+    TEST_ASSERT( dynamic_cast<HierarchicalLogger*>(logger_abc)->getParent() == root);
 	
     Logger* logger_abc_def = mgr->getLogger(LogCategory("abc.def"));
-	TEST_ASSERT(logger_abc_def != 0);
-    TEST_ASSERT(mgr->getLogger(LogCategory("abc.def")) == logger_abc_def);
-	TEST_ASSERT(dynamic_cast<HierarchicalLogger*>(logger_abc_def) != 0);
-	TEST_ASSERT(dynamic_cast<HierarchicalLogger*>(logger_abc_def)->getParent() == logger_abc);
+    TEST_ASSERT( logger_abc_def != 0);
+    TEST_ASSERT( mgr->getLogger(LogCategory("abc.def")) == logger_abc_def);
+    TEST_ASSERT( dynamic_cast<HierarchicalLogger*>(logger_abc_def) != 0);
+    TEST_ASSERT( dynamic_cast<HierarchicalLogger*>(logger_abc_def)->getParent() == logger_abc);
 
     Logger* logger_xyz_uvw = mgr->getLogger(LogCategory("xyz.uvw"));
-	TEST_ASSERT(logger_xyz_uvw != 0);
-    TEST_ASSERT(mgr->getLogger(LogCategory("xyz.uvw")) == logger_xyz_uvw);
+    TEST_ASSERT( logger_xyz_uvw != 0);
+    TEST_ASSERT( mgr->getLogger(LogCategory("xyz.uvw")) == logger_xyz_uvw);
     Logger* logger_xyz = mgr->getLogger(LogCategory("xyz"));
-	TEST_ASSERT(logger_xyz != 0);
-    TEST_ASSERT(mgr->getLogger(LogCategory("xyz")) == logger_xyz);
-	TEST_ASSERT(dynamic_cast<HierarchicalLogger*>(logger_xyz_uvw) != 0);
-	TEST_ASSERT(dynamic_cast<HierarchicalLogger*>(logger_xyz_uvw)->getParent() == logger_xyz);
+    TEST_ASSERT( logger_xyz != 0);
+    TEST_ASSERT( mgr->getLogger(LogCategory("xyz")) == logger_xyz);
+    TEST_ASSERT( dynamic_cast<HierarchicalLogger*>(logger_xyz_uvw) != 0);
+    TEST_ASSERT( dynamic_cast<HierarchicalLogger*>(logger_xyz_uvw)->getParent() == logger_xyz);
+
+    dynamic_cast<HierarchicalLogger*>(logger_abc)->setLevel(LogLevel::Info);
+    dynamic_cast<HierarchicalLogger*>(logger_xyz_uvw)->setLevel(LogLevel::Warning);
+    TEST_ASSERT( logger_abc->getLevel() == LogLevel::Info );
+    TEST_ASSERT( logger_abc->getEffectiveLevel() == LogLevel::Info );
+    TEST_ASSERT( logger_xyz_uvw->getLevel() == LogLevel::Warning );
+    TEST_ASSERT( logger_xyz_uvw->getEffectiveLevel() == LogLevel::Warning );
+
+    dynamic_cast<HierarchicalLogger*>(logger_abc)->setLevel(LogLevel::Info);
+    dynamic_cast<HierarchicalLogger*>(logger_abc_def)->setLevel(LogLevel::Inherit);
+    TEST_ASSERT( logger_abc->getLevel() == LogLevel::Info );
+    TEST_ASSERT( logger_abc->getEffectiveLevel() == LogLevel::Info );
+    TEST_ASSERT( logger_abc_def->getLevel() == LogLevel::Inherit );
+    TEST_ASSERT( logger_abc_def->getEffectiveLevel() == LogLevel::Info );
+
+    dynamic_cast<HierarchicalLogger*>(logger_abc)->setLevel(LogLevel::Inherit);
+    dynamic_cast<HierarchicalLogger*>(logger_abc_def)->setLevel(LogLevel::Warning);
+    TEST_ASSERT( logger_abc->getLevel() == LogLevel::Inherit );
+    TEST_ASSERT( logger_abc->getEffectiveLevel() == root->getLevel() );
+    TEST_ASSERT( logger_abc_def->getLevel() == LogLevel::Warning );
+    TEST_ASSERT( logger_abc_def->getEffectiveLevel() == LogLevel::Warning );
 }
 
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.