ThreadSafeLogger.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright 2007, 2008, 2009, 2010, 2011 Instituto de Sistemas e Robotica, Instituto Superior Tecnico
00003 
00004 This file is part of MeRMaID.
00005 
00006 MeRMaID is free software: you can redistribute it and/or modify
00007 it under the terms of the GNU Lesser General Public License as published by
00008 the Free Software Foundation, either version 3 of the License, or
00009 (at your option) any later version.
00010 
00011 MeRMaID is distributed in the hope that it will be useful,
00012 but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 GNU Lesser General Public License for more details.
00015 
00016 You should have received a copy of the GNU Lesser General Public License
00017 along with MeRMaID.  If not, see <http://www.gnu.org/licenses/>.
00018 */
00019 
00020 
00021 
00022 /**
00023  * @Filename ThreadSafeLogger.cpp
00024  * @Description ThreadSafeLogger implementation
00025  * @Status Finished
00026  * @Version $Id: ThreadSafeLogger.cpp 1 2011-03-04 18:13:18Z jreis $
00027  * @Maintainer Joao Reis (joaocgreis@gmail.com)
00028  */
00029 
00030 #include "config.h"
00031 
00032 #include <ctime>
00033 
00034 #include "Logger.hpp"
00035 
00036 
00037 
00038 using namespace mermaid::support::logging;
00039 
00040 
00041 
00042 shared_ptr<Logger> ThreadSafeLogger::build()
00043 {
00044   return shared_ptr<Logger> (new ThreadSafeLogger());
00045 }
00046 
00047 
00048 
00049 shared_ptr<Logger> ThreadSafeLogger::build (const std::string& prefix)
00050 {
00051   return shared_ptr<Logger> (new ThreadSafeLogger (prefix));
00052 }
00053 
00054 
00055 
00056 ThreadSafeLogger::ThreadSafeLogger() :
00057     mutex_(),
00058     indentation_ (0),
00059     prefix_(),
00060     levelOutputs_ (std::map<LogLevel, std::list<shared_ptr<LogOutput> > >())
00061 {
00062 }
00063 
00064 
00065 
00066 ThreadSafeLogger::ThreadSafeLogger (const std::string& prefix) :
00067     mutex_(),
00068     indentation_ (0),
00069     prefix_ (prefix),
00070     levelOutputs_ (std::map<LogLevel, std::list<shared_ptr<LogOutput> > >())
00071 {
00072 }
00073 
00074 void ThreadSafeLogger::enableLogLevel (const LogLevel& logLevel,
00075                                        shared_ptr<LogOutput> logOutput)
00076 {
00077   ACE_Write_Guard<ACE_RW_Mutex> guard (mutex_);
00078   levelOutputs_[logLevel].push_back (logOutput);
00079 }
00080 
00081 
00082 
00083 void ThreadSafeLogger::disableLogLevel (const LogLevel& logLevel,
00084                                         shared_ptr<LogOutput> logOutput)
00085 {
00086   ACE_Write_Guard<ACE_RW_Mutex> guard (mutex_);
00087   levelOutputs_[logLevel].remove (logOutput);
00088 }
00089 
00090 
00091 
00092 void ThreadSafeLogger::disableLogLevel (const LogLevel& logLevel)
00093 {
00094   ACE_Write_Guard<ACE_RW_Mutex> guard (mutex_);
00095   levelOutputs_[logLevel].clear();
00096 }
00097 
00098 
00099 
00100 void ThreadSafeLogger::setPrefix (const std::string& prefix)
00101 {
00102   ACE_Write_Guard<ACE_RW_Mutex> guard (mutex_);
00103   prefix_ = prefix;
00104 }
00105 
00106 
00107 
00108 void ThreadSafeLogger::log (const LogLevel& logLevel,
00109                             const std::string& message)
00110 {
00111   time_t t = time (NULL);
00112   
00113   // BEGIN and END are write locks because of the indentation update
00114   if ( (logLevel == LOG_BEGIN) || (logLevel == LOG_END)) {
00115     mutex_.acquire_write();
00116   }
00117   else {
00118     mutex_.acquire_read();
00119   }
00120   
00121   // Update and get the right indentation
00122   if ( (logLevel == LOG_END) && (indentation_ > 0)) {
00123     indentation_--;
00124   }
00125   int indentation = indentation_;
00126   if (logLevel == LOG_BEGIN) {
00127     indentation_++;
00128   }
00129   
00130   std::list<shared_ptr<LogOutput> >& logList = levelOutputs_[logLevel];
00131   for (std::list<shared_ptr<LogOutput> >::iterator i = logList.begin();
00132        i != logList.end();
00133        i++) {
00134     (*i)->outputMessage (LogMessage (logLevel,
00135                                      t,
00136                                      indentation,
00137                                      prefix_,
00138                                      message));
00139   }
00140   
00141   mutex_.release();
00142 }
Generated on Fri Mar 4 22:14:58 2011 for MeRMaID::support by  doxygen 1.6.3