Logger.cpp

Go to the documentation of this file.
00001 #pragma ident "$Id: Logger.cpp 2952 2011-10-28 14:53:20Z yanweignss $"
00002 
00008 //============================================================================
00009 //
00010 //  This file is part of GPSTk, the GPS Toolkit.
00011 //
00012 //  The GPSTk is free software; you can redistribute it and/or modify
00013 //  it under the terms of the GNU Lesser General Public License as published
00014 //  by the Free Software Foundation; either version 2.1 of the License, or
00015 //  any later version.
00016 //
00017 //  The GPSTk is distributed in the hope that it will be useful,
00018 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
00019 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00020 //  GNU Lesser General Public License for more details.
00021 //
00022 //  You should have received a copy of the GNU Lesser General Public
00023 //  License along with GPSTk; if not, write to the Free Software Foundation,
00024 //  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 //
00026 //  Wei Yan - Chinese Academy of Sciences . 2009~2015
00027 //
00028 //============================================================================
00029 
00030 #include "Logger.hpp"
00031 #include "StringUtils.hpp"
00032 #include "MemoryUtils.hpp"
00033 #include "String.hpp"
00034 
00035 namespace gpstk
00036 {
00037    using namespace std;
00038 
00039    std::map<std::string, Logger*> Logger::loggerMap;
00040    AutoReleasePool<LogChannel> Logger::channelPool;
00041    ConsoleLogChannel Logger::defaultChannel;
00042 
00043    Logger::Logger(const Logger& right)
00044    {
00045       this->name = right.name;
00046       this->level = right.level;
00047       this->channel = right.channel;
00048    }
00049 
00050    Logger& Logger::operator = (const Logger& right)
00051    {
00052       this->name = right.name;
00053       this->level = right.level;
00054       this->channel = right.channel;
00055 
00056       return *this;
00057    }
00058 
00059    void Logger::log(const LogMessage& msg)
00060    {
00061       if( (msg.level <= level) && (channel) )
00062       {
00063          channel->log(msg);
00064       }
00065    }
00066 
00067    void Logger::log(const std::string& text, LogLevel level)
00068    {
00069       LogMessage msg(name, text, level);
00070       log(msg);
00071    }
00072 
00073    void Logger::log(const std::string& text, LogLevel level, ExceptionLocation location)
00074    {
00075       DayTime now; now.setLocalTime();
00076       LogMessage msg(name, text, level, now,
00077                      location.getFileName(),
00078                      location.getFunctionName(),
00079                      location.getLineNumber());
00080       log(msg);
00081    }
00082 
00083    void Logger::setLevel(const std::string& level)
00084    {
00085       string temp = toLower(level);
00086 
00087       if (temp == "none")
00088          setLevel(0);
00089       else if (temp == "fatal")
00090          setLevel(LEVEL_FATAL);
00091       else if (temp == "critical")
00092          setLevel(LEVEL_CRITICAL);
00093       else if (temp == "error")
00094          setLevel(LEVEL_ERROR);
00095       else if (temp == "warning")
00096          setLevel(LEVEL_WARNING);
00097       else if (temp == "notice")
00098          setLevel(LEVEL_NOTICE);
00099       else if (temp == "information")
00100          setLevel(LEVEL_INFORMATION);
00101       else if (temp == "debug")
00102          setLevel(LEVEL_DEBUG);
00103       else if (temp == "trace")
00104          setLevel(LEVEL_TRACE);
00105       else
00106          throw Exception("Not a valid log level"+level);
00107    }
00108    
00109    //
00110    // static method
00111    //
00112 
00113    Logger& Logger::nullLogger(const std::string& logname,
00114                               LogLevel loglevel, 
00115                               const std::string& pattern)
00116    {
00117       LogChannel* pChannel = new LogChannel(pattern);
00118       channelPool.add(pChannel);
00119 
00120       Logger* pLogger = find(logname);
00121       if(pLogger)
00122       {
00123          pLogger->setChannel(pChannel);
00124       }
00125       else
00126       {
00127          return create(logname,pChannel,loglevel);
00128       }
00129 
00130    }
00131 
00132 
00133    Logger& Logger::consoleLogger(const std::string& logname,
00134                                  LogLevel loglevel, 
00135                                  const std::string& pattern)
00136    {
00137       LogChannel* pChannel = new ConsoleLogChannel(pattern);
00138       channelPool.add(pChannel);
00139 
00140       Logger* pLogger = find(logname);
00141       if(pLogger)
00142       {
00143          pLogger->setChannel(pChannel);
00144       }
00145       else
00146       {
00147          return create(logname,pChannel,loglevel);
00148       }
00149    }
00150 
00151    Logger& Logger::fileLogger(const std::string& logname,
00152                               const std::string& filename,
00153                               LogLevel loglevel, 
00154                               const std::string& pattern)
00155    {
00156       LogChannel* pChannel = new FileLogChannel(filename,pattern);
00157       channelPool.add(pChannel);
00158 
00159       Logger* pLogger = find(logname);
00160       if(pLogger)
00161       {
00162          pLogger->setChannel(pChannel);
00163       }
00164       else
00165       {
00166          return create(logname,pChannel,loglevel);
00167       }
00168    }
00169 
00170    Logger& Logger::create( const std::string& logname,
00171                            LogChannel* logchannel,
00172                            LogLevel loglevel )
00173    {
00174       Logger* pLogger = find(logname);
00175       if (!pLogger)
00176       {
00177          pLogger = new Logger(logname, loglevel, logchannel);
00178          add(pLogger);
00179       }
00180 
00181       return (*pLogger);
00182    }
00183 
00184    Logger* Logger::find(const std::string& name)
00185    {
00186       
00187       LoggerMap::iterator it = loggerMap.find(name);
00188       if (it != loggerMap.end()) return it->second;
00189       else return 0;
00190    }
00191 
00192    void Logger::add(Logger* pLogger)
00193    {
00194       loggerMap.insert(
00195          map<string, Logger*>::value_type(pLogger->getName(), pLogger) );
00196    }
00197 
00198    void Logger::destroy(const std::string& name)
00199    {
00200       LoggerMap::iterator it = loggerMap.find(name);
00201       if (it != loggerMap.end())
00202       {
00203          delete it->second;
00204          it->second = 0;
00205          loggerMap.erase(it);
00206       }
00207    }
00208 
00209    void Logger::shutdown()
00210    {
00211       LoggerMap::iterator it;
00212       for (it = loggerMap.begin(); it != loggerMap.end(); ++it)
00213       {
00214          delete it->second;
00215          it->second = 0;
00216       }
00217       loggerMap.clear();
00218    }
00219 
00220    Logger& Logger::get(const std::string& name)
00221    {
00222       Logger* pLogger = find(name);
00223       if (!pLogger)
00224       {
00225          if (name == "")
00226          {
00227             pLogger = new Logger(name, LEVEL_INFORMATION, &defaultChannel);
00228          }
00229          
00230          add(pLogger);
00231       }
00232       return (*pLogger);
00233    }
00234 
00235 
00237 
00238    
00239    class AutoLoggerShutdown
00240    {
00241    public:
00242       AutoLoggerShutdown()  
00243       {
00244       }
00245       
00246       ~AutoLoggerShutdown() 
00247       { 
00248          Logger::shutdown(); 
00249          Logger::channelPool.release();
00250       }
00251 
00252    protected:
00253       
00254    };
00255 
00256    
00257    static AutoLoggerShutdown als;
00258 
00259 }  // End of namespace 'gpstk'
00260 
00261 

Generated on Tue May 22 03:30:59 2012 for GPS ToolKit Software Library by  doxygen 1.3.9.1