From: Christian P. <cp...@us...> - 2005-07-01 12:07:11
|
Update of /cvsroot/pclasses/pclasses2/src/App In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24145/src/App Modified Files: LogChannel.cpp LogManager.cpp 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.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/App/LogManager.cpp,v retrieving revision 1.4 retrieving revision 1.5 diff -u -d -r1.4 -r1.5 --- LogManager.cpp 6 May 2005 15:24:35 -0000 1.4 +++ LogManager.cpp 1 Jul 2005 12:07:00 -0000 1.5 @@ -33,15 +33,7 @@ LogManager::~LogManager() throw() { - // delete our LogTargets ... - TargetMap::iterator ti = _targets.begin(); - while(ti != _targets.end()) - { - delete ti->second; - TargetMap::iterator del = ti; - ++ti; - _targets.erase(del); - } + System::CriticalSection::ScopedLock lck(_channelsMtx); // delete our LogChannels ... ChannelMap::iterator ci = _channels.begin(); @@ -51,112 +43,144 @@ ChannelMap::iterator del = ci; ++ci; _channels.erase(del); - } + } + + System::CriticalSection::ScopedLock lck2(_targetsMtx); + + // delete our LogTargets ... + TargetMap::iterator ti = _targets.begin(); + while(ti != _targets.end()) + { + delete ti->second; + TargetMap::iterator del = ti; + ++ti; + _targets.erase(del); + } } void LogManager::reload() throw() { - for(ChannelMap::const_iterator i = _channels.begin(); - i != _channels.end(); ++i) + System::CriticalSection::ScopedLock lck(_targetsMtx); + + for(TargetMap::const_iterator i = _targets.begin(); + i != _targets.end(); ++i) { - LogChannel* channel = i->second; - channel->reload(); + LogTarget* target = i->second; + target->reload(); } } LogChannel* LogManager::addChannel(const std::string& name) { - LogChannel* chan = channel(name); - if(!chan) + System::CriticalSection::ScopedLock lck(_channelsMtx); + + LogChannel* chan = 0; + ChannelMap::const_iterator i = _channels.find(name); + if(i != _channels.end()) + { + chan = i->second; + } + else { chan = new LogChannel(name); - _channels[name] = chan; + _channels.insert(std::make_pair(name, chan)); } return chan; } -bool LogManager::removeChannel(const std::string& name) +bool LogManager::removeChannel(const std::string& name) throw() { + System::CriticalSection::ScopedLock lck(_channelsMtx); + ChannelMap::iterator i = _channels.find(name); if(i != _channels.end()) { - LogChannel* chan = i->second; - - // delete all our LogTargets for this channel ... - TargetMap::iterator ti = _targets.begin(); - while(ti != _targets.end()) - { - if(ti->first.first == chan) - { - delete ti->second; - TargetMap::iterator del = ti; - ++ti; - _targets.erase(del); - } - else - { - ++ti; - } - } - + delete i->second; _channels.erase(i); - delete chan; - return true; } return false; } -LogTarget* LogManager::addTarget(LogChannel* channel, const std::string& name, const std::string& type) +LogChannel* LogManager::channel(const std::string& name) const throw() { - LogTarget* target = LogTargetFactory::instance().create(type); - if(target) - { - if(!channel->addTarget(name, target)) - { - delete target; - target = 0; - } + System::CriticalSection::ScopedLock lck(_channelsMtx); - _targets[make_pair(channel, name)] = target; - } + LogChannel* chan = 0; + ChannelMap::const_iterator i = _channels.find(name); + if(i != _channels.end()) + chan = i->second; - return target; + return chan; } -bool LogManager::removeTarget(LogChannel* channel, const std::string& name) +LogTarget* LogManager::addTarget(const std::string& name, const std::string& type) { - TargetMap::iterator i = _targets.find(make_pair(channel, name)); + System::CriticalSection::ScopedLock lck(_targetsMtx); + + LogTarget* target = 0; + TargetMap::const_iterator i = _targets.find(name); + + // target already exists ... if(i != _targets.end()) { - LogTarget* target = channel->removeTarget(name); + target = i->second; + } + // create the new target ... + else + { + target = LogTargetFactory::instance().create(type); + if(target) + _targets.insert(std::make_pair(name, target)); + } + + return target; +} + +bool LogManager::removeTarget(const std::string& name) +{ + System::CriticalSection::ScopedLock lck(_targetsMtx); + + TargetMap::iterator ti = _targets.find(name); + if(ti != _targets.end()) + { + LogTarget* target = ti->second; + + // remove target from channels ... + System::CriticalSection::ScopedLock lck2(_channelsMtx); + + ChannelMap::iterator ci = _channels.begin(); + while(ci != _channels.end()) + { + ci->second->removeTarget(target); + ++ci; + } + delete target; - _targets.erase(i); + _targets.erase(ti); return true; } return false; } -LogChannel* LogManager::channel(const std::string& name) const +LogTarget* LogManager::target(const std::string& name) const throw() { - LogChannel* chan = 0; - ChannelMap::const_iterator i = _channels.find(name); - if(i != _channels.end()) - chan = i->second; + System::CriticalSection::ScopedLock lck(_targetsMtx); - return chan; + LogTarget* target = 0; + TargetMap::const_iterator i = _targets.find(name); + if(i != _targets.end()) + target = i->second; + + return target; } LogChannel& LogManager::operator()(const std::string& chanName) { - LogChannel* chan = channel(chanName); - if(!chan) - chan = addChannel(chanName); - - return *chan; + return *addChannel(chanName); } struct LogManagerContext { }; Index: LogChannel.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/App/LogChannel.cpp,v retrieving revision 1.3 retrieving revision 1.4 diff -u -d -r1.3 -r1.4 --- LogChannel.cpp 28 Apr 2005 10:34:06 -0000 1.3 +++ LogChannel.cpp 1 Jul 2005 12:06:59 -0000 1.4 @@ -143,36 +143,29 @@ return _name; } -bool LogChannel::addTarget(const std::string& name, LogTarget* target) -{ - TargetMap::const_iterator i = _targets.find(name); - if(i != _targets.end()) - return false; - - _targets[name] = target; - return true; -} - -LogTarget* LogChannel::removeTarget(const std::string& name) throw() +bool LogChannel::addTarget(LogTarget* target) { - LogTarget* target = 0; + System::CriticalSection::ScopedLock lck(_mutex); - TargetMap::iterator i = _targets.find(name); - if(i != _targets.end()) + for(TargetList::iterator i = _targets.begin(); + i != _targets.end(); ++i) { - target = i->second; - _targets.erase(i); + if(*i == target) + return false; } - return target; + _targets.push_back(target); + return true; } bool LogChannel::removeTarget(LogTarget* target) throw() { - for(TargetMap::iterator i = _targets.begin(); + System::CriticalSection::ScopedLock lck(_mutex); + + for(TargetList::iterator i = _targets.begin(); i != _targets.end(); ++i) { - if(i->second == target) + if(*i == target) { _targets.erase(i); return true; @@ -182,31 +175,17 @@ return false; } -void LogChannel::reload() const throw() -{ - for(TargetMap::const_iterator i = _targets.begin(); - i != _targets.end(); ++i) - { - LogTarget* target = i->second; - try - { - target->reload(); - } - catch(IO::IOError& err) - { - } - } -} - void LogChannel::output(const LogMessage& msg) const throw() { if(msg.level() >= _logLevel) { + System::CriticalSection::ScopedLock lck(_mutex); + // output to all registered LogTargets ... - for(TargetMap::const_iterator i = _targets.begin(); + for(TargetList::const_iterator i = _targets.begin(); i != _targets.end(); ++i) { - LogTarget* target = i->second; + LogTarget* target = *i; try { if(target->valid() && msg.level() >= target->logLevel()) @@ -223,11 +202,13 @@ LogMessage::Level LogChannel::logLevel() const throw() { + System::CriticalSection::ScopedLock lck(_mutex); return _logLevel; } void LogChannel::setLogLevel(LogMessage::Level l) throw() { + System::CriticalSection::ScopedLock lck(_mutex); _logLevel = l; } |