PCMLogger.cpp

Go to the documentation of this file.
00001 
00002 //
00003 // This source file is a part of ParCompMark
00004 // Parallel Compositing Benchmark Framework
00005 //
00006 // for latest info see http://parcompmark.sourceforge.net
00007 
00008 //
00009 // Copyright (C) 2006 IT2 ParCompMark Dev. Team
00010 // 
00011 // This program is free software; you can redistribute it and/or
00012 // modify it under the terms of the GNU General Public License
00013 // as published by the Free Software Foundation; either version 2
00014 // of the License, or (at your option) any later version.
00015 // 
00016 // This program is distributed in the hope that it will be useful,
00017 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00019 // GNU General Public License for more details.
00020 // 
00021 // You should have received a copy of the GNU General Public License
00022 // along with this program; if not, write to the Free Software
00023 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00024 
00025 //
00026 // Inner includes
00027 //
00028 
00029 #include "../include/PCMLogger.h"
00030 
00031 #include "../include/PCMFileSystemManager.h"
00032 
00033 #include "../include/PCMNetwork.h"
00034 
00035 namespace ParCompMark
00036 {
00037 
00038   // Inherited static attribute initialization
00039   template <> Logger * Singleton < Logger >::mInstance = 0;
00040 
00041   //
00042   // Class constants
00043   //
00044 
00045   const u8 Logger::LOGTOCONSOLE = 1;
00046   const u8 Logger::LOGTOFILE = 2;
00047 
00048   //
00049   // Constructors & destructor
00050   //
00051 
00052    Logger::Logger(const std::string & logFileName)
00053         // You have to initialize the following attributes:
00054         // - mLogMode
00055         // - mConsoleLogLevel
00056         // - mFileLogLevel
00057         // - mFp
00058         // - mInitialized
00059         // - mLogFileName
00060   {
00061         mLogMode = LOGTOCONSOLE | LOGTOFILE;
00062         mConsoleLogLevel = WARNING;
00063         mFileLogLevel = DEBUG;
00064         mFp = (FILE *) 0;
00065         mInitialized = false;
00066         Network::getIP();
00067         mLogFileName = Network::getIfConfs()->get("eth0")->IP + '-' + logFileName;
00068   }
00069 
00070  /*----------------------------------------------------------------------*/
00071 
00072   Logger::~Logger()
00073   {
00074         if(mInitialized)
00075         {
00076          log(NOTICE, "Logger has been shut down.");
00077          mInitialized = false;
00078 
00079          // Close file
00080          FileSystemManager::closeFile(mFp);
00081         }
00082   }
00083 
00084  /*----------------------------------------------------------------------*/
00085 
00086   //
00087   // Class methods
00088   //
00089 
00090   std::string Logger::translateException(const Exception & exception)
00091   {
00092         std::ostringstream line;
00093         line << Exception::translateType(exception.getType()) << ' ' << exception.
00094          getDescription() << " in file `" << exception.getFileName() << '\'' << " in method `" << exception.
00095          getFunctionName() << '\'' << " at line " << exception.
00096          getLineNumber() << " on host `" << Network::getIfConfs()->get("eth0")->IP << "\'";
00097 
00098         return line.str();
00099   }
00100 
00101  /*----------------------------------------------------------------------*/
00102 
00103   //
00104   // Methods
00105   //
00106 
00107   void Logger::initialize()
00108   {
00109         if(mLogMode & LOGTOFILE)
00110         {
00111 
00112          // Create file manager if not exist
00113          if(!FileSystemManager::getInstance())
00114          {
00115                 FileSystemManager::createInstance();
00116                 FileSystemManager::getInstance()->initialize();
00117          }
00118 
00119          mFp = FileSystemManager::getInstance()->openAppFileC(mLogFileName, FileSystemManager::WRITE);
00120          Assert(mFp.getPtr(), INVALID_NAME_ERROR, "Logger::init()");
00121 
00122          mInitialized = true;
00123          log(NOTICE, "Logger has been initialized at " + Timer::getTimeDateString() + ".");
00124         }
00125   }
00126 
00127  /*----------------------------------------------------------------------*/
00128 
00129   void Logger::logMultiLine(const LogLevel & loglevel, const std::string & message)
00130   {
00131         // Tokenize
00132         // and log as single lines
00133         std::string::size_type lineStart = 0, lineEnd = 0;
00134         std::string delim = "\n";
00135 
00136         while((lineEnd = message.find(delim, lineStart)) != std::string::npos)
00137         {
00138          log(loglevel, message.substr(lineStart, lineEnd - lineStart));
00139          lineStart = lineEnd + 1;
00140         }
00141 
00142         log(loglevel, message.substr(lineStart, lineEnd - lineStart));
00143   }
00144 
00145  /*----------------------------------------------------------------------*/
00146 
00147 }