PCMLogger.cpp
Go to the documentation of this file.00001 
00002 
00003 
00004 
00005 
00006 
00007 
00008 
00009 
00010 
00011 
00012 
00013 
00014 
00015 
00016 
00017 
00018 
00019 
00020 
00021 
00022 
00023 
00024 
00025 
00026 
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   
00039   template <> Logger * Singleton < Logger >::mInstance = 0;
00040 
00041   
00042   
00043   
00044 
00045   const u8 Logger::LOGTOCONSOLE = 1;
00046   const u8 Logger::LOGTOFILE = 2;
00047 
00048   
00049   
00050   
00051 
00052    Logger::Logger(const std::string & logFileName)
00053         
00054         
00055         
00056         
00057         
00058         
00059         
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          
00080          FileSystemManager::closeFile(mFp);
00081         }
00082   }
00083 
00084  
00085 
00086   
00087   
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   
00105   
00106 
00107   void Logger::initialize()
00108   {
00109         if(mLogMode & LOGTOFILE)
00110         {
00111 
00112          
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         
00132         
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 }