00001 /* 00002 * Appender.cpp 00003 * 00004 * Copyright 2000, LifeLine Networks BV (www.lifeline.nl). All rights reserved. 00005 * Copyright 2000, Bastiaan Bakker. All rights reserved. 00006 * 00007 * See the COPYING file for the terms of usage and distribution. 00008 */ 00009 00010 #include "PortabilityImpl.hh" 00011 #include <log4cpp/Appender.hh> 00012 00013 namespace log4cpp { 00014 Appender::AppenderMap* Appender::_allAppenders = 0; 00015 threading::Mutex Appender::_appenderMapMutex; 00016 00017 /* assume _appenderMapMutex locked */ 00018 Appender::AppenderMap& Appender::_getAllAppenders() { 00019 if (!_allAppenders) 00020 _allAppenders = new Appender::AppenderMap(); 00021 00022 return *_allAppenders; 00023 } 00024 00025 Appender* Appender::getAppender(const std::string& name) { 00026 threading::ScopedLock lock(_appenderMapMutex); 00027 AppenderMap& allAppenders = Appender::_getAllAppenders(); 00028 AppenderMap::iterator i = allAppenders.find(name); 00029 return (allAppenders.end() == i) ? NULL : ((*i).second); 00030 } 00031 00032 void Appender::_addAppender(Appender* appender) { 00033 //REQUIRE(_allAppenders.find(appender->getName()) == _getAllAppenders().end()) 00034 threading::ScopedLock lock(_appenderMapMutex); 00035 _getAllAppenders()[appender->getName()] = appender; 00036 } 00037 00038 void Appender::_removeAppender(Appender* appender) { 00039 threading::ScopedLock lock(_appenderMapMutex); 00040 _getAllAppenders().erase(appender->getName()); 00041 } 00042 00043 bool Appender::reopenAll() { 00044 threading::ScopedLock lock(_appenderMapMutex); 00045 bool result = true; 00046 AppenderMap& allAppenders = _getAllAppenders(); 00047 for(AppenderMap::iterator i = allAppenders.begin(); i != allAppenders.end(); i++) { 00048 result = result && ((*i).second)->reopen(); 00049 } 00050 00051 return result; 00052 } 00053 00054 void Appender::closeAll() { 00055 threading::ScopedLock lock(_appenderMapMutex); 00056 AppenderMap& allAppenders = _getAllAppenders(); 00057 for(AppenderMap::iterator i = allAppenders.begin(); i != allAppenders.end(); i++) { 00058 ((*i).second)->close(); 00059 } 00060 } 00061 00062 void Appender::_deleteAllAppenders() { 00063 threading::ScopedLock lock(_appenderMapMutex); 00064 AppenderMap& allAppenders = _getAllAppenders(); 00065 for(AppenderMap::iterator i = allAppenders.begin(); i != allAppenders.end(); ) { 00066 Appender *app = (*i).second; 00067 i++; // increment iterator before delete or iterator will be invalid. 00068 delete (app); 00069 } 00070 } 00071 00072 Appender::Appender(const std::string& name) : 00073 _name(name) { 00074 _addAppender(this); 00075 } 00076 00077 Appender::~Appender() { 00078 _removeAppender(this); 00079 } 00080 }