From: Christian P. <cp...@us...> - 2005-07-01 12:07:11
|
Update of /cvsroot/pclasses/pclasses2/include/pclasses/App In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24145/include/pclasses/App Modified Files: LogChannel.h LogManager.h LogTarget.h Log Message: - Fixed some design flaws (LogTargets could not be added to multiple LogChannels) - Added documentation to LogManager, LogTarget, LogChannel - Added locking to LogManager, LogChannel Index: LogManager.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/App/LogManager.h,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- LogManager.h 24 Apr 2005 11:36:31 -0000 1.2 +++ LogManager.h 1 Jul 2005 12:06:59 -0000 1.3 @@ -22,6 +22,7 @@ #define P_LogManager_h #include <pclasses/Export.h> +#include <pclasses/System/CriticalSection.h> #include <pclasses/App/LogTarget.h> #include <pclasses/App/LogChannel.h> @@ -32,66 +33,123 @@ namespace App { -//! Log manager +//! Logging manager +/*! + The LogManager class provides a container for easy management of + LogChannel and LogTarget objects. Together with these classes the + LogManager forms the logging framework of P::Classes. + + All methods in this class a thread-safe. +*/ class PAPP_EXPORT LogManager { public: //! Map of LogChannels typedef std::map<std::string,LogChannel*> ChannelMap; //! Map of owned LogTargets - typedef std::map< - std::pair<LogChannel*, std::string>, - LogTarget* - > TargetMap; + typedef std::map<std::string,LogTarget*> TargetMap; + //! Constructor LogManager(); + + //! Destructor + /*! + The destructor deletes all channels and targets + wich have been previously added. + */ ~LogManager() throw(); //! Close and re-open all LogTargets void reload() throw(); - //! Add logging channel + //! Add a logging channel /*! - Returns NULL if the named LogChannel already exists. - The LogChannel is owned by the LogManager. + Adds a LogChannel to the LogManager which is identified + by the given name. If the method is called twice for the + same name, it simply returns a pointer to the previously + added LogChannel. + The LogChannel is owned by the LogManager and must not be + deleted by the client code. + \param name the name for the added LogChannel + \return returns a pointer to the LogChannel object */ LogChannel* addChannel(const std::string& name); //! Remove logging channel - bool removeChannel(const std::string& name); + /*! + Removes a previously added LogChannel object identified by the + given name. Returns true if the channel was removed, false if + the channel is unknown. + \param name the name of the LogChannel to remove + \return returns true if the channel was removed, false otherwise + */ + bool removeChannel(const std::string& name) throw(); //! Returns a pointer to the named LogChannel /*! - Returns NULL if the named LogChannel does not exist. + Returns a pointer to the LogChannel object identified by the + given name. Returns NULL if the named LogChannel does not exist. + \param name the name of the LogChannel which should be returned + \return a pointer to the LogChannel object identified by the given name */ - LogChannel* channel(const std::string& name) const; + LogChannel* channel(const std::string& name) const throw(); - //! Add a logging target + //! Add logging target /*! - Returns NULL if the target already exists, or the LogTarget - type is not registered. - The LogTarget is owned by the LogManager. + Adds a LogTarget to the LogManager which is identified by the given + name. If the method is called twice for the same name, it simply + returns a pointer to the previously added LogTarget. + The LogTarget is created by a call to Factory<LogTarget>::create(type) + If the LogTarget could not be created by the Factory, a NULL + pointer is returned. + The LogTarget is owned by the LogManager and must not be + deleted by the client code. + \param name the name for the added LogTarget + \param type the type of the LogTarget to create + \return returns a pointer to the LogTarget object */ - LogTarget* addTarget(LogChannel* chan, const std::string& name, - const std::string& type); + LogTarget* addTarget(const std::string& name, const std::string& type); - //! Remove a logging target - bool removeTarget(LogChannel* chan, const std::string& name); + //! Remove logging target + /*! + Removes a previously added LogTarget object identified by the + given name. Returns true if the target was removed, false if + the channel is unknown. + \param name the name of the LogTarget to remove + \return returns true if the target was removed, false otherwise + */ + bool removeTarget(const std::string& name); + + //! Returns a pointer to the named LogTarget + /*! + Returns a pointer to the LogTarget object identified by the + given name. Returns NULL if the named LogTarget does not exist. + \param name the name of the LogTarget which should be returned + \return a pointer to the LogTarget object identified by the given name + */ + LogTarget* target(const std::string& name) const throw(); //! Returns a refernce to the named LogChannel /*! - If the channel does not exist it is created. + This method does basically the same as channel(chanName); + Except it adds the LogChannel if it does not exist. + \param chanName name of the LogChannel to retrieve + \return a reference to the named LogChannel */ - LogChannel& operator()(const std::string& channel); + LogChannel& operator()(const std::string& chanName); //! Returns the instance of the LogManager static LogManager& instance(); private: - ChannelMap _channels; - TargetMap _targets; + mutable System::CriticalSection + _channelsMtx, _targetsMtx; + + ChannelMap _channels; + TargetMap _targets; }; +#define P_LOG(channel) P::App::LogManager::instance()(channel) } // !namespace App Index: LogTarget.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/App/LogTarget.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- LogTarget.h 6 May 2005 15:24:34 -0000 1.3 +++ LogTarget.h 1 Jul 2005 12:06:59 -0000 1.4 @@ -31,7 +31,16 @@ namespace App { -//! Log message target +//! Logging target +/*! + The LogTarget interface is used by the LogChannel class to output + it's log messages. Log messages are usally written to a LogChannel + which then output's the message to it's known LogTarget's. + Note that a LogTarget object may be added to multiple LogChannel's + which in turn may output messages simultanously from multiple + threads, therefore a class inherited from LogTarget must implement + locking by itself. +*/ class PAPP_EXPORT LogTarget { public: LogTarget(); Index: LogChannel.h =================================================================== RCS file: /cvsroot/pclasses/pclasses2/include/pclasses/App/LogChannel.h,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- LogChannel.h 28 Apr 2005 10:34:06 -0000 1.3 +++ LogChannel.h 1 Jul 2005 12:06:59 -0000 1.4 @@ -23,10 +23,11 @@ #include <pclasses/Export.h> #include <pclasses/IO/IOError.h> +#include <pclasses/System/CriticalSection.h> #include <pclasses/App/LogMessage.h> #include <string> -#include <map> +#include <list> #include <iostream> namespace P { @@ -35,10 +36,18 @@ class LogTarget; -//! Log message channel +//! Logging channel +/*! + The LogChannel class is used to output LogMessage's to its + registered LogTarget's. + Messages are only written to its targets if the level of the + message is greater or equal to the log-level of the LogChannel. + + All methods in this class are thread-safe. +*/ class PAPP_EXPORT LogChannel: public std::ostream { public: - typedef std::map<std::string, LogTarget*> TargetMap; + typedef std::list<LogTarget*> TargetList; LogChannel(const std::string& name); ~LogChannel() throw(); @@ -46,18 +55,11 @@ const std::string& name() const; //! Add a logging target - bool addTarget(const std::string& name, - LogTarget* target); - - //! Remove named logging target - LogTarget* removeTarget(const std::string& name) throw(); + bool addTarget(LogTarget* target); //! Remove logging target by pointer bool removeTarget(LogTarget* target) throw(); - //! Close and re-open all targets - void reload() const throw(); - //! Output log message to all targets void output(const LogMessage& msg) const throw(); @@ -71,9 +73,10 @@ LogChannel& operator()(LogMessage::Level) throw(); private: - std::string _name; - TargetMap _targets; - LogMessage::Level _logLevel; + mutable System::CriticalSection _mutex; + std::string _name; + TargetList _targets; + LogMessage::Level _logLevel; }; |