From 32a6a7c5cb757c03166d28c82c0bbfb8afaace19 Mon Sep 17 00:00:00 2001 From: Nick Draper <nick.draper@stfc.ac.uk> Date: Fri, 25 Oct 2013 16:35:04 +0100 Subject: [PATCH] re #8232 Able to reduce log levels --- .../Framework/API/inc/MantidAPI/Algorithm.h | 6 + .../API/inc/MantidAPI/AlgorithmProxy.h | 6 + .../Framework/API/inc/MantidAPI/IAlgorithm.h | 6 +- Code/Mantid/Framework/API/src/Algorithm.cpp | 1 + .../Framework/API/src/AlgorithmProxy.cpp | 2 +- .../Kernel/inc/MantidKernel/Logger.h | 27 ++-- .../Framework/Kernel/src/ConfigService.cpp | 8 +- Code/Mantid/Framework/Kernel/src/Logger.cpp | 141 +++++++++++------- .../Framework/Kernel/test/ConfigServiceTest.h | 19 +++ .../MantidPlot/src/ApplicationWindow.cpp | 1 + 10 files changed, 148 insertions(+), 69 deletions(-) diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h b/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h index c09915d8c38..7b8bf3ed47b 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/Algorithm.h @@ -223,6 +223,12 @@ public: void setLogging(const bool value){g_log.setEnabled(value);} ///returns the status of logging, True = enabled bool isLogging() const {return g_log.getEnabled();} + + ///sets the logging priority offset + void setLoggingOffset(const int value) {g_log.setLevelOffset(value);} + ///returns the logging priority offset + int getLoggingOffset() const {return g_log.getLevelOffset();} + /// Returns a reference to the logger. Kernel::Logger& getLogger() const { return g_log; } diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h b/Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h index 528133cd96b..5838c7b4e4f 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/AlgorithmProxy.h @@ -124,6 +124,11 @@ namespace Mantid void setLogging(const bool value) { m_isLoggingEnabled=value; } /// Is the algorithm have logging enabled bool isLogging() const { return m_isLoggingEnabled; } + + ///returns the logging priority offset + void setLoggingOffset(const int value) { m_loggingOffset=value; } + ///returns the logging priority offset + int getLoggingOffset() const { return m_loggingOffset; } ///setting the child start progress void setChildStartProgress(const double startProgress)const; @@ -166,6 +171,7 @@ namespace Mantid mutable boost::shared_ptr<Algorithm> m_alg; ///< Shared pointer to a real algorithm. Created on demand bool m_isExecuted; ///< Executed flag bool m_isLoggingEnabled;///< is the logging of the underlying algorithm enabled + int m_loggingOffset; ///< the logging priority offset bool m_rethrow; ///< Whether or not to rethrow exceptions. bool m_isChild; ///< Is this a child algo diff --git a/Code/Mantid/Framework/API/inc/MantidAPI/IAlgorithm.h b/Code/Mantid/Framework/API/inc/MantidAPI/IAlgorithm.h index 3959aacc1e3..4900b2ffc13 100644 --- a/Code/Mantid/Framework/API/inc/MantidAPI/IAlgorithm.h +++ b/Code/Mantid/Framework/API/inc/MantidAPI/IAlgorithm.h @@ -159,7 +159,11 @@ public: ///Logging can be disabled by passing a value of false virtual void setLogging(const bool value) = 0; ///returns the status of logging, True = enabled - virtual bool isLogging() const = 0; + virtual bool isLogging() const = 0; + ///gets the logging priority offset + virtual void setLoggingOffset(const int value) = 0; + ///returns the logging priority offset + virtual int getLoggingOffset() const = 0; ///setting the child start progress virtual void setChildStartProgress(const double startProgress)const = 0; /// setting the child end progress diff --git a/Code/Mantid/Framework/API/src/Algorithm.cpp b/Code/Mantid/Framework/API/src/Algorithm.cpp index eb2721317f8..b6925e9041d 100644 --- a/Code/Mantid/Framework/API/src/Algorithm.cpp +++ b/Code/Mantid/Framework/API/src/Algorithm.cpp @@ -951,6 +951,7 @@ namespace Mantid copyPropertiesFrom(proxy); m_algorithmID = proxy.getAlgorithmID(); setLogging(proxy.isLogging()); + setLoggingOffset(proxy.getLoggingOffset()); setChild(proxy.isChild()); } diff --git a/Code/Mantid/Framework/API/src/AlgorithmProxy.cpp b/Code/Mantid/Framework/API/src/AlgorithmProxy.cpp index 0a1d216aa56..78210bc4ee3 100644 --- a/Code/Mantid/Framework/API/src/AlgorithmProxy.cpp +++ b/Code/Mantid/Framework/API/src/AlgorithmProxy.cpp @@ -25,7 +25,7 @@ namespace Mantid PropertyManagerOwner(),_executeAsync(this,&AlgorithmProxy::executeAsyncImpl), m_name(alg->name()),m_category(alg->category()), m_categorySeparator(alg->categorySeparator()), m_alias(alg->alias()), m_version(alg->version()), m_alg(alg), - m_isExecuted(),m_isLoggingEnabled(true), m_rethrow(false), + m_isExecuted(),m_isLoggingEnabled(true), m_loggingOffset(0), m_rethrow(false), m_isChild(false) { if (!alg) diff --git a/Code/Mantid/Framework/Kernel/inc/MantidKernel/Logger.h b/Code/Mantid/Framework/Kernel/inc/MantidKernel/Logger.h index 82cd80acaf8..93865ae10fc 100644 --- a/Code/Mantid/Framework/Kernel/inc/MantidKernel/Logger.h +++ b/Code/Mantid/Framework/Kernel/inc/MantidKernel/Logger.h @@ -71,17 +71,6 @@ class ThreadSafeLogStream; // Our logger's priority types are the same as POCO's Message's types. typedef Poco::Message::Priority Priority; -// /// An enumeration of the priority levels of a log message. -// enum Priority -// { -// PRIO_FATAL = 1, ///< A fatal error. The application will most likely terminate. This is the highest priority. -// PRIO_ERROR = 3, ///< An error. An operation did not complete successfully, but the application as a whole is not affected. -// PRIO_WARNING = 4, ///< A warning. An operation completed with an unexpected result. -// PRIO_NOTICE = 5, ///< An informational message, usually denoting the successful completion of an Algorithm, These are the headlines of what we should be reporting to the user. -// PRIO_INFORMATION = 6, ///< An informational message, usually denoting the successful completion of an operation. -// PRIO_DEBUG = 7 ///< A debugging message.This is the lowest priority. -// }; - /// Sets the Loggername to a new value. void setName(std::string newName); /// Logs at Fatal level @@ -115,7 +104,13 @@ class ThreadSafeLogStream; /// Sets the Logger's log level. void setLevel(int level); - + + /// Sets the Logger's log offset level. + void setLevelOffset(int level); + + /// Gets the Logger's log offset level. + int getLevelOffset(); + /// Returns the Logger's log level. int getLevel() const; @@ -162,12 +157,20 @@ class ThreadSafeLogStream; /// Return a log stream set with the given priority void log(const std::string message, Logger::Priority priority); + /// gets the correct log stream for a priority + std::ostream& getLogStream(Logger::Priority priority); + + /// Return a log stream set with the given priority + Priority applyLevelOffset(Priority proposedLevel); + /// Internal handle to third party logging objects Poco::Logger* m_log; /// A Log stream to allow streaming operations. This pointer is owned by this class, initialized in the constructor and deleted in the destructor ThreadSafeLogStream* m_logStream; /// Name of this logging object std::string m_name; + /// The offset of the logger + int m_levelOffset; /// The state of this logger, disabled loggers send no messages bool m_enabled; /// Typdef for a container of logger pointers diff --git a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp index 9ffdc87bef9..9d8571e8815 100644 --- a/Code/Mantid/Framework/Kernel/src/ConfigService.cpp +++ b/Code/Mantid/Framework/Kernel/src/ConfigService.cpp @@ -1780,7 +1780,7 @@ bool ConfigServiceImpl::quickParaViewCheck() const if (givenVersionNumber == targetVersionNumber) { isAvailable = true; - this->g_log.notice("ParaView is available"); + this->g_log.information("ParaView is available"); // Now set the plugin path. this->setParaViewPluginPath(); } @@ -1789,7 +1789,7 @@ bool ConfigServiceImpl::quickParaViewCheck() const std::stringstream messageStream; messageStream << "The compatible version of ParaView is " << targetVersionNumber << " but the installed version is " << givenVersionNumber; this->g_log.debug(messageStream.str()); - this->g_log.notice("ParaView is not available"); + this->g_log.information("ParaView is not available"); } } else @@ -1797,13 +1797,13 @@ bool ConfigServiceImpl::quickParaViewCheck() const std::stringstream messageStream; messageStream << "ParaView version query failed with code: " << rc; this->g_log.debug(messageStream.str()); - this->g_log.notice("ParaView is not available"); + this->g_log.information("ParaView is not available"); } } catch(Poco::SystemException &e) { this->g_log.debug(e.what()); - this->g_log.notice("ParaView is not available"); + this->g_log.information("ParaView is not available"); } return isAvailable; } diff --git a/Code/Mantid/Framework/Kernel/src/Logger.cpp b/Code/Mantid/Framework/Kernel/src/Logger.cpp index 1d5f065d497..8dee83fbe51 100644 --- a/Code/Mantid/Framework/Kernel/src/Logger.cpp +++ b/Code/Mantid/Framework/Kernel/src/Logger.cpp @@ -30,9 +30,8 @@ namespace Kernel /** Constructor * @param name :: The class name invoking this logger */ - Logger::Logger(const std::string& name) : m_enabled(true) + Logger::Logger(const std::string& name) : m_enabled(true), m_name(name), m_levelOffset(0) { - m_name = name; m_log=&Poco::Logger::get(m_name); m_logStream = new Mantid::Kernel::ThreadSafeLogStream(*m_log); } @@ -226,14 +225,7 @@ namespace Kernel */ std::ostream& Logger::fatal() { - if (m_enabled) - { - return m_logStream->fatal(); - } - else - { - return *m_nullStream; - } + return getLogStream(Priority::PRIO_FATAL); } /** This class implements an ostream interface to the Logger for error messages. @@ -245,14 +237,7 @@ namespace Kernel */ std::ostream& Logger::error() { - if (m_enabled) - { - return m_logStream->error(); - } - else - { - return *m_nullStream; - } + return getLogStream(Priority::PRIO_ERROR); } /** This class implements an ostream interface to the Logger for warning messages. @@ -264,14 +249,7 @@ namespace Kernel */ std::ostream& Logger::warning() { - if (m_enabled) - { - return m_logStream->warning(); - } - else - { - return *m_nullStream; - } + return getLogStream(Priority::PRIO_WARNING); } /** This class implements an ostream interface to the Logger for notice messages. @@ -283,14 +261,7 @@ namespace Kernel */ std::ostream& Logger::notice() { - if (m_enabled) - { - return m_logStream->notice(); - } - else - { - return *m_nullStream; - } + return getLogStream(Priority::PRIO_NOTICE); } /** This class implements an ostream interface to the Logger for information messages. @@ -302,14 +273,7 @@ namespace Kernel */ std::ostream& Logger::information() { - if (m_enabled) - { - return m_logStream->information(); - } - else - { - return *m_nullStream; - } + return getLogStream(Priority::PRIO_INFORMATION); } /** This class implements an ostream interface to the Logger for debug messages. @@ -321,14 +285,7 @@ namespace Kernel */ std::ostream& Logger::debug() { - if (m_enabled) - { - return m_logStream->debug(); - } - else - { - return *m_nullStream; - } + return getLogStream(Priority::PRIO_DEBUG); } /** releases resources and deletes this object. @@ -463,10 +420,12 @@ namespace Kernel try { - switch( priority ) + switch( applyLevelOffset(priority) ) { case Poco::Message::PRIO_FATAL: m_log->fatal(message); break; + case Poco::Message::PRIO_CRITICAL: m_log->critical(message); + break; case Poco::Message::PRIO_ERROR: m_log->error(message); break; case Poco::Message::PRIO_WARNING: m_log->warning(message); @@ -477,6 +436,8 @@ namespace Kernel break; case Poco::Message::PRIO_DEBUG: m_log->debug(message); break; + case Poco::Message::PRIO_TRACE: m_log->trace(message); + break; default: break; } @@ -487,7 +448,85 @@ namespace Kernel std::cerr << "Error in logging framework: " << e.what(); } } + + /** + * Log a given message at a given priority + * @param message :: The message to log + * @param priority :: The priority level + */ + std::ostream& Logger::getLogStream(Logger::Priority priority) + { + if( !m_enabled ) return *m_nullStream; + + switch( applyLevelOffset(priority) ) + { + case Poco::Message::PRIO_FATAL: return m_logStream->fatal(); + break; + case Poco::Message::PRIO_CRITICAL: return m_logStream->critical(); + break; + case Poco::Message::PRIO_ERROR: return m_logStream->error(); + break; + case Poco::Message::PRIO_WARNING: return m_logStream->warning(); + break; + case Poco::Message::PRIO_NOTICE: return m_logStream->notice(); + break; + case Poco::Message::PRIO_INFORMATION:return m_logStream->information(); + break; + case Poco::Message::PRIO_DEBUG: return m_logStream->debug(); + break; + default: + return *m_nullStream; + } + + } + + /** + * Adjust a log priority level based off the m_levelOffset + * @param proposedLevel :: The proposed level + * @returns The offseted level + */ + Logger::Priority Logger::applyLevelOffset(Logger::Priority proposedLevel) + { + int retVal = proposedLevel; + //fast exit is offset is 0 + if (m_levelOffset==0) + { + return proposedLevel; + } + else + { + retVal += m_levelOffset; + if (retVal < static_cast<int>(Priority::PRIO_FATAL)) + { + retVal = Priority::PRIO_FATAL; + } + else if (retVal > static_cast<int>(Priority::PRIO_TRACE)) + { + retVal = Priority::PRIO_TRACE; + } + } + //Logger::Priority p(retVal); + return static_cast<Logger::Priority>(retVal); + } + + /** + * Sets the Logger's log offset level. + * @param level :: The level offset to use + */ + void Logger::setLevelOffset(int level) + { + m_levelOffset = level; + } + + /** + * Gets the Logger's log offset level. + * @returns The offset level + */ /// Gets the Logger's log offset level. + int Logger::getLevelOffset() + { + return m_levelOffset; + } } // namespace Kernel } // Namespace Mantid diff --git a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h index de4a2d413f0..d226f1cb0a4 100644 --- a/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h +++ b/Code/Mantid/Framework/Kernel/test/ConfigServiceTest.h @@ -100,6 +100,25 @@ public: } + void testLogLevelOffset() + { + //attempt some logging + Logger& log1 = Logger::get("logTestOffset"); + log1.setLevelOffset(0); + TS_ASSERT_THROWS_NOTHING(log1.fatal("a fatal string with offset 0")); + log1.setLevelOffset(-1); + TS_ASSERT_THROWS_NOTHING(log1.fatal("a fatal string with offset -1 should still be fatal")); + TS_ASSERT_THROWS_NOTHING(log1.information("a information string with offset -1 should be notice")); + log1.setLevelOffset(1); + TS_ASSERT_THROWS_NOTHING(log1.fatal("a fatal string with offset 1 should be critical")); + TS_ASSERT_THROWS_NOTHING(log1.notice("a notice string with offset 1 should be information")); + TS_ASSERT_THROWS_NOTHING(log1.debug("a debug string with offset 1 should be debug")); + log1.setLevelOffset(999); + TS_ASSERT_THROWS_NOTHING(log1.fatal("a fatal string with offset 999 should be trace")); + TS_ASSERT_THROWS_NOTHING(log1.notice("a notice string with offset 999 should be trace")); + TS_ASSERT_THROWS_NOTHING(log1.debug("a debug string with offset 999 should be trace")); + } + void testDefaultFacility() { TS_ASSERT_THROWS_NOTHING(ConfigService::Instance().getFacility() ); diff --git a/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp b/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp index aba3bead8b7..1cd0d71dc78 100644 --- a/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp +++ b/Code/Mantid/MantidPlot/src/ApplicationWindow.cpp @@ -17995,6 +17995,7 @@ void ApplicationWindow::about2Start(){ // there is no reason to trigger UpdataScriptRepository if it has never been installed Mantid::API::IAlgorithm_sptr update_script_repo = mantidUI->createAlgorithm("UpdateScriptRepository"); update_script_repo->initialize(); + update_script_repo->setLoggingOffset(1); mantidUI->executeAlgorithmAsync(update_script_repo); } } -- GitLab