diff --git a/Code/Mantid/Kernel/inc/ConfigSvc.h b/Code/Mantid/Kernel/inc/ConfigSvc.h new file mode 100644 index 0000000000000000000000000000000000000000..d289efdac5f7b485014a53a443e0028195e87fdd --- /dev/null +++ b/Code/Mantid/Kernel/inc/ConfigSvc.h @@ -0,0 +1,192 @@ +#ifndef MANTID_CONFIGSVC_H_ +#define MANTID_CONFIGSVC_H_ +//---------------------------------------------------------------------- +// Includes +//---------------------------------------------------------------------- +#include <string> + + +//forward declaration +namespace Poco +{ + namespace Util + { + class PropertyFileConfiguration; + class SystemConfiguration; + } +} + +namespace Mantid +{ +/** @class ConfigSvc ConfigSvc.h Kernel/ConfigSvc.h + + The ConfigSvc class provides a simple facade to access the Configuration functionality of the Mantid Framework. + The class gathers information from config files and the system varaibles. + This information is available to all the objects within the framework as well as being used to configure the logging framework. + This class currently uses the Logging functionality provided through the POCO (portable components library). + + @author Nicholas Draper, Tessella Support Services plc + @date 15/10/2007 + + Copyright ? 2007 ???RAL??? + + This file is part of Mantid. + + Mantid is free software; you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation; either version 3 of the License, or + (at your option) any later version. + + Mantid is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see <http://www.gnu.org/licenses/>. + + File change history is stored at: <https://svn.mantidproject.org/mantid/trunk/Code/Mantid> +*/ + class ConfigSvc + { + /// Inner templated class to wrap the poco library objects that have protected desctructors and expose them as public + template<typename T > + class WrappedObject : public T + { + public: + typedef T element_type; + WrappedObject() + { + m_pPtr = static_cast<T*>(this); + } + + template<typename Field> + WrappedObject(const Field& F) : T(F) + { + m_pPtr = static_cast<T*>(this); + } + + WrappedObject(const WrappedObject<T>& A) : T(A) + { + m_pPtr = static_cast<T*>(this); + } + + virtual ~WrappedObject() + {} + + const T& operator*() const { return *m_pPtr; } + T& operator*() { return m_pPtr; } + + const T* operator->() const{ return m_pPtr; } + T* operator->() { return m_pPtr; } + + private: + T* m_pPtr; + }; + + + public: + /// A static method which retrieves the single instance of the ConfigSvc + /// + /// @returns A pointer to the instance + static ConfigSvc* Instance() + { + if (!m_instance) m_instance = new ConfigSvc; + return m_instance; + } + + /// Loads the config file provided, any previous configuration is discarded. + /// If the file contains logging setup instructions then these will be used to setup the logging framework. + /// + /// @param filename The filename and optionally path of the file to load + void loadConfig(const std::string& filename); + + /// Searches for the string within the currently loaded configuaration values and returns to value as a string. + /// + /// @param keyName The case sensitive name of the property that you need the value of. + /// @returns the string value of the property + std::string getString(const std::string& keyName); + + /// Searches for the string within the currently loaded configuaration values and returns to value as an integer. + /// + /// @param keyName The case sensitive name of the property that you need the value of. + /// @returns the integer value of the property + int getInt(const std::string& keyName); + + /// Searches for the string within the currently loaded configuaration values and returns to value as a double. + /// + /// @param keyName The case sensitive name of the property that you need the value of. + /// @returns the double value of the property + double getDouble(const std::string& keyName); + + /// Searches for the string within the environment variables and returns to value as a string. + /// + /// @param keyName The name of the environment variable that you need the value of. + /// @returns the string value of the property + std::string getEnvironment(const std::string& keyName); + + /// Gets the name of the operation system + /// + /// @returns the string value of the operation system version + std::string getOSName(); + + /// Gets the name of the computer running Mantid + /// + /// @returns the string value of the name of the computer + std::string getComputerName(); + + /// Gets the name of the operation system Architecture + /// + /// @returns the string value of the operation system Architecture + std::string getOSArchitecture(); + +/* Removed as the use of these throughs a debug assertion about an invlid heap pointer + File dbgheap.c + Expression _CrtIsValidHeapPointer(pUserData) + + /// Gets the name of the operation system version + /// + /// @returns the string value of the operation system version + std::string getOSVersion(); + + /// Gets the path of the current directory + /// + /// @returns the string value of the path of the current directory + std::string getCurrentDir(); + + /// Gets the path of the home directory + /// + /// @returns the string value of the path of the home directory + std::string getHomeDir(); + + /// Gets the path of the temp directory + /// + /// @returns the string value of the path of the temp directory + std::string getTempDir(); +*/ + + /// Destructor + /// Prevents client from calling 'delete' on the pointer handed + /// out by Instance + virtual ~ConfigSvc(); + private: + /// Private Constructor for singleton class + ConfigSvc(); + + /// Private copy constructor + /// Prevents singleton being copied + ConfigSvc(const ConfigSvc&) {} + + /// the POCO file config object + WrappedObject<Poco::Util::PropertyFileConfiguration>* m_pConf; + /// the POCO system Config Object + WrappedObject<Poco::Util::SystemConfiguration>* m_pSysConfig; + + /// Pointer to the factory instance + static ConfigSvc* m_instance; + + }; + +} + +#endif /*MANTID_CONFIGSVC_H_*/ diff --git a/Code/Mantid/Kernel/inc/Logger.h b/Code/Mantid/Kernel/inc/Logger.h index feafe98af55a758754754abb5a88f226b102d275..b00c9d85f0547957e515a0966aa38835c9556ce6 100755 --- a/Code/Mantid/Kernel/inc/Logger.h +++ b/Code/Mantid/Kernel/inc/Logger.h @@ -112,7 +112,7 @@ namespace Mantid //void dump(const std::string& msg, const void* buffer, std::size_t length, Message::Priority prio = Message::PRIO_DEBUG); /// Returns true if at least the given log level is set. - /// @param level The logging level 0=trace, 1=debug, 2=information, 3=notice, 4=warning, 5=error, 6=critical, 7=fatal + /// @param level The logging level 8=trace, 7=debug, 6=information, 5=notice, 4=warning, 3=error, 2=critical, 1=fatal bool is(int level) const; /// Returns a reference to the Logger with the given name. @@ -125,12 +125,13 @@ namespace Mantid /// Loggers. static void shutdown(); - protected: - ///Protecxted Conctractor called by static get method - Logger(const std::string& name); - ///protected destructor + /// destructor ~Logger(); + protected: + ///Protected Constructor called by static get method + Logger(const std::string& name); + private: ///no arg constructor Logger(); @@ -142,8 +143,6 @@ namespace Mantid std::string _name; }; - - } #endif /*MANTID_LOGGINGSERVICE_H_*/ diff --git a/Code/Mantid/Kernel/src/ConfigSvc.cpp b/Code/Mantid/Kernel/src/ConfigSvc.cpp new file mode 100644 index 0000000000000000000000000000000000000000..9804bad47f3627bba7140830f350e838888b04ab --- /dev/null +++ b/Code/Mantid/Kernel/src/ConfigSvc.cpp @@ -0,0 +1,97 @@ +#include "../inc/ConfigSvc.h" +#include "Poco/Util/LoggingConfigurator.h" +#include "Poco/Util/SystemConfiguration.h" +#include "Poco/Util/PropertyFileConfiguration.h" + +namespace Mantid +{ + // Initialise the instance pointer to zero + ConfigSvc* ConfigSvc::m_instance=0; + + //private constructor + ConfigSvc::ConfigSvc() + { + //getting at system details + m_pSysConfig = new WrappedObject<Poco::Util::SystemConfiguration>; + + m_pConf = 0; + } + + //destructor + ConfigSvc::~ConfigSvc() + { + delete m_pSysConfig; + delete m_pConf; + } + + + void ConfigSvc::loadConfig(const std::string& filename) + { + delete m_pConf; + m_pConf = new WrappedObject<Poco::Util::PropertyFileConfiguration>(filename); + + //configure the logging framework + Poco::Util::LoggingConfigurator configurator; + configurator.configure(m_pConf); + } + + std::string ConfigSvc::getString(const std::string& keyName) + { + return m_pConf->getString(keyName); + } + + int ConfigSvc::getInt(const std::string& keyName) + { + return 1; + } + + double ConfigSvc::getDouble(const std::string& keyName) + { + return 1.0; + } + + std::string ConfigSvc::getEnvironment(const std::string& keyName) + { + return m_pSysConfig->getString("system.env." + keyName); + } + + std::string ConfigSvc::getOSName() + { + return m_pSysConfig->getString("system.osName"); + } + + std::string ConfigSvc::getOSArchitecture() + { + return m_pSysConfig->getString("system.osArchitecture"); + } + + std::string ConfigSvc::getComputerName() + { + return m_pSysConfig->getString("system.nodeName"); + } + +/* Removed as the use of these throughs a debug assertion about an invlid heap pointer + File dbgheap.c + Expression _CrtIsValidHeapPointer(pUserData) + + std::string ConfigSvc::getOSVersion() + { + return m_pSysConfig->getString("system.osVersion"); + } + + std::string ConfigSvc::getCurrentDir() + { + return m_pSysConfig->getString("system.currentDir"); + } + + std::string ConfigSvc::getHomeDir() + { + return m_pSysConfig->getString("system.homeDir"); + } + + std::string ConfigSvc::getTempDir() + { + return m_pSysConfig->getString("system.tempDir"); + } +*/ +} \ No newline at end of file diff --git a/Code/Mantid/Kernel/src/Logger.cpp b/Code/Mantid/Kernel/src/Logger.cpp index 05012a9bd06181f5c6ab4f9a72a64eeb58d7a741..7c3b175e2f2a040877a683337332a2b88e5ec740 100755 --- a/Code/Mantid/Kernel/src/Logger.cpp +++ b/Code/Mantid/Kernel/src/Logger.cpp @@ -3,8 +3,8 @@ namespace Mantid { - Logger::Logger(const std::string& name): _log(Poco::Logger::get(_name)) - { + Logger::Logger(const std::string& name): _log(Poco::Logger::get(name)) + { _name = name; } diff --git a/Code/Mantid/Kernel/test/ConfigSvcTest.h b/Code/Mantid/Kernel/test/ConfigSvcTest.h new file mode 100644 index 0000000000000000000000000000000000000000..afca7b9ad368fa63df41ddf3be1528486b143639 --- /dev/null +++ b/Code/Mantid/Kernel/test/ConfigSvcTest.h @@ -0,0 +1,79 @@ +#ifndef MANTID_CONFIGSVCTEST_H_ +#define MANTID_CONFIGSVCTEST_H_ + +#include <cxxtest/TestSuite.h> + +#include "../inc/ConfigSvc.h" +#include "../inc/Logger.h" +#include <string> + +using namespace Mantid; + +class ConfigSvcTest : public CxxTest::TestSuite +{ +public: + + ConfigSvcTest() + { + configSvc = Mantid::ConfigSvc::Instance(); + configSvc->loadConfig("MantidTest.properties"); + } + + void testLogging() + { + //attempt some logging + Logger& log1 = Logger::get("logTest"); + + TS_ASSERT_THROWS_NOTHING(log1.trace("a trace string")); + TS_ASSERT_THROWS_NOTHING(log1.debug("a debug string")); + TS_ASSERT_THROWS_NOTHING(log1.information("an information string")); + TS_ASSERT_THROWS_NOTHING(log1.notice("a notice string")); + TS_ASSERT_THROWS_NOTHING(log1.warning("a warning string")); + TS_ASSERT_THROWS_NOTHING(log1.error("an error string")); + TS_ASSERT_THROWS_NOTHING(log1.critical("a critical string")); + TS_ASSERT_THROWS_NOTHING(log1.fatal("a fatal string")); + + //checking the level - this should be set to debug in the config file + //therefore this should only return false for trace + TS_ASSERT(log1.is(8) == false); //trace + TS_ASSERT(log1.is(7)); //debug + TS_ASSERT(log1.is(6)); //information + TS_ASSERT(log1.is(5)); //notice + TS_ASSERT(log1.is(4)); //warning + TS_ASSERT(log1.is(3)); //error + TS_ASSERT(log1.is(2)); //critical + TS_ASSERT(log1.is(1)); //fatal + + } + + void TestSystemValues() + { + //we cannot test the return values here as they will differ based on the environment. + //therfore we will just check they return a non empty string. + std::string osName = configSvc->getOSName(); + TS_ASSERT_LESS_THAN(0, osName.length()); //check that the string is not empty + std::string osArch = configSvc->getOSArchitecture(); + TS_ASSERT_LESS_THAN(0, osArch.length()); //check that the string is not empty + std::string osCompName = configSvc->getComputerName(); + TS_ASSERT_LESS_THAN(0, osCompName.length()); //check that the string is not empty + } + + void TestCustomProperty() + { + //Mantid.legs is defined in the properties script as 6 + std::string legCountString = configSvc->getString("mantid.legs"); + TS_ASSERT_EQUALS(legCountString, "6"); + } + + void TestMissingProperty() + { + //Mantid.noses is not defined in the properties script + TS_ASSERT_THROWS_ANYTHING( std::string legCountString = configSvc->getString("mantid.noses")); + } + + +private: + ConfigSvc *configSvc; +}; + +#endif /*MANTID_CONFIGSVCTEST_H_*/ diff --git a/Code/Mantid/Kernel/test/CxxTest_2_Build.vcproj b/Code/Mantid/Kernel/test/CxxTest_2_Build.vcproj new file mode 100644 index 0000000000000000000000000000000000000000..dce85a81a760c10f40ff9481de8898b7dbec8f29 --- /dev/null +++ b/Code/Mantid/Kernel/test/CxxTest_2_Build.vcproj @@ -0,0 +1,228 @@ +<?xml version="1.0" encoding="Windows-1252"?> +<VisualStudioProject + ProjectType="Visual C++" + Version="8.00" + Name="CxxTest_2_Build" + ProjectGUID="{F6369538-0E60-4728-B192-32CB7266D1EF}" + RootNamespace="CxxTest_2_Build" + > + <Platforms> + <Platform + Name="Win32" + /> + </Platforms> + <ToolFiles> + </ToolFiles> + <Configurations> + <Configuration + Name="Debug|Win32" + OutputDirectory="." + IntermediateDirectory="." + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC60.vsprops" + UseOfMFC="0" + ATLMinimizesCRunTimeLibraryUsage="false" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TypeLibraryName=".\Debug/CxxTest_2_Build.tlb" + HeaderFileName="" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="0" + AdditionalIncludeDirectories="..\..\..\Third_Party\include;..\.." + MinimalRebuild="true" + BasicRuntimeChecks="3" + RuntimeLibrary="1" + PrecompiledHeaderFile=".\CxxTest_2_Build.pch" + AssemblerListingLocation=".\" + ObjectFile=".\" + ProgramDataBaseFileName=".\" + WarningLevel="3" + SuppressStartupBanner="true" + DebugInformationFormat="4" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + PreprocessorDefinitions="_DEBUG" + Culture="1037" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + AdditionalDependencies="kernel.lib PocoFoundationd.lib PocoUtild.lib" + OutputFile="runner.exe" + LinkIncremental="2" + SuppressStartupBanner="true" + AdditionalLibraryDirectories="../../Debug; ../../../Third_Party/lib/win32" + GenerateDebugInformation="true" + ProgramDatabaseFile=".\runner.pdb" + SubSystem="1" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + SuppressStartupBanner="true" + OutputFile=".\Debug/CxxTest_2_Build.bsc" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + <Configuration + Name="Release|Win32" + OutputDirectory=".\Release" + IntermediateDirectory=".\Release" + ConfigurationType="1" + InheritedPropertySheets="$(VCInstallDir)VCProjectDefaults\UpgradeFromVC60.vsprops" + UseOfMFC="0" + ATLMinimizesCRunTimeLibraryUsage="false" + CharacterSet="2" + > + <Tool + Name="VCPreBuildEventTool" + /> + <Tool + Name="VCCustomBuildTool" + /> + <Tool + Name="VCXMLDataGeneratorTool" + /> + <Tool + Name="VCWebServiceProxyGeneratorTool" + /> + <Tool + Name="VCMIDLTool" + TypeLibraryName=".\Release/CxxTest_2_Build.tlb" + HeaderFileName="" + /> + <Tool + Name="VCCLCompilerTool" + Optimization="2" + InlineFunctionExpansion="1" + AdditionalIncludeDirectories="..\.." + PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE" + StringPooling="true" + RuntimeLibrary="0" + EnableFunctionLevelLinking="true" + PrecompiledHeaderFile=".\Release/CxxTest_2_Build.pch" + AssemblerListingLocation=".\Release/" + ObjectFile=".\Release/" + ProgramDataBaseFileName=".\Release/" + WarningLevel="3" + SuppressStartupBanner="true" + /> + <Tool + Name="VCManagedResourceCompilerTool" + /> + <Tool + Name="VCResourceCompilerTool" + PreprocessorDefinitions="NDEBUG" + Culture="1037" + /> + <Tool + Name="VCPreLinkEventTool" + /> + <Tool + Name="VCLinkerTool" + OutputFile="Release/runner.exe" + LinkIncremental="1" + SuppressStartupBanner="true" + ProgramDatabaseFile=".\Release/runner.pdb" + SubSystem="1" + TargetMachine="1" + /> + <Tool + Name="VCALinkTool" + /> + <Tool + Name="VCManifestTool" + /> + <Tool + Name="VCXDCMakeTool" + /> + <Tool + Name="VCBscMakeTool" + SuppressStartupBanner="true" + OutputFile=".\Release/CxxTest_2_Build.bsc" + /> + <Tool + Name="VCFxCopTool" + /> + <Tool + Name="VCAppVerifierTool" + /> + <Tool + Name="VCWebDeploymentTool" + /> + <Tool + Name="VCPostBuildEventTool" + /> + </Configuration> + </Configurations> + <References> + </References> + <Files> + <File + RelativePath="runner.cpp" + > + <FileConfiguration + Name="Debug|Win32" + > + <Tool + Name="VCCLCompilerTool" + AdditionalIncludeDirectories="" + PreprocessorDefinitions="" + /> + </FileConfiguration> + <FileConfiguration + Name="Release|Win32" + > + <Tool + Name="VCCLCompilerTool" + AdditionalIncludeDirectories="" + PreprocessorDefinitions="" + /> + </FileConfiguration> + </File> + </Files> + <Globals> + </Globals> +</VisualStudioProject> diff --git a/Code/Mantid/Kernel/test/MantidTest.properties b/Code/Mantid/Kernel/test/MantidTest.properties new file mode 100644 index 0000000000000000000000000000000000000000..bb2988e41552e7e6e390bc578bb0f603b4a49cc1 --- /dev/null +++ b/Code/Mantid/Kernel/test/MantidTest.properties @@ -0,0 +1,10 @@ +#framework configuration +mantid.legs = 6 + +#logging configuartion +logging.loggers.root.level = debug +logging.loggers.root.channel = fileChannel +logging.channels.fileChannel.class = FileChannel +logging.channels.fileChannel.path = sample.log +logging.channels.fileChannel.formatter.class = PatternFormatter +logging.channels.fileChannel.formatter.pattern = %s: {%p} %t diff --git a/Code/Mantid/Kernel/test/runTests.bat b/Code/Mantid/Kernel/test/runTests.bat new file mode 100644 index 0000000000000000000000000000000000000000..ada5dc265db2cfb8f63ec88b39e30893d1eda32c --- /dev/null +++ b/Code/Mantid/Kernel/test/runTests.bat @@ -0,0 +1,25 @@ +@echo off +REM IF "%VCINSTALLDIR%"=="" SET PATH=%path%;C:\Mantid\Code\Third_Party\lib\win32 +IF "%VCINSTALLDIR%"=="" CALL "C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat" +REM Simple script to build and run the tests. +REM Have kept separate from the makefile since that's automatically generated +REM by Eclipse. +REM +REM Author: Nick Draper, 19/10/07 +REM +echo "Generating the source from the test header files..." +python ..\..\..\Third_Party\src\cxxtest\cxxtestgen.py --error-printer -o runner.cpp *.h + +echo "Compiling the test executable..." +devenv CxxTest_2_Build.vcproj /BUILD "Debug|Win32" + +copy ..\..\..\Third_Party\lib\win32\*.dll . + +REM echo "Running the tests..." +runner.exe +REM Remove the generated files to ensure that they're not inadvertently run +REM when something in the chain has failed. +devenv CxxTest_2_Build.vcproj /CLEAN +del runner.cpp +del BuildLog.htm +del *.dll \ No newline at end of file