log.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  *   Copyright (C) 2008 by Kutumov Alexey                                  *
00003  *   ru.pixel@gmail.com                                                    *
00004  *                                                                         *
00005  *   This program is free software; you can redistribute it and/or modify  *
00006  *   it under the terms of the GNU General Public License as published by  *
00007  *   the Free Software Foundation; either version 2 of the License, or     *
00008  *   (at your option) any later version.                                   *
00009  *                                                                         *
00010  *   This program is distributed in the hope that it will be useful,       *
00011  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
00012  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
00013  *   GNU General Public License for more details.                          *
00014  *                                                                         *
00015  *   You should have received a copy of the GNU General Public License     *
00016  *   along with this program; if not, write to the                         *
00017  *   Free Software Foundation, Inc.,                                       *
00018  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
00019  ***************************************************************************/
00020 
00034 #include "gpucalc/log.h"
00035 #include "gpucalc/string_util.h"
00036 
00037 #include <iostream>
00038 #include <iomanip>
00039 
00040 namespace gpucalc
00041 {
00042  Log::Log(const std::string & LogName, unsigned int LogClassNameAlignment, unsigned int LogObjectNameAlignment,
00043   bool DebugOutput, bool SuppressFileOutput, bool SuppressObjectInfo):
00044   Object(_LogClassName, LogName), mLogLevel(LL_Normal), mDebugOutput(DebugOutput), mSuppressFile(SuppressFileOutput),
00045   mSuppressObjectInfo(SuppressObjectInfo), mTimeOutput(true), mLogClassNameAlignment(LogClassNameAlignment), mLogObjectNameAlignment(LogObjectNameAlignment)
00046  {
00047   if (!mSuppressFile)
00048   {
00049    mfpLog.open(LogName.c_str());
00050   }
00051  }
00052 
00053 
00054  Log::~Log()
00055  {
00056   if (!mSuppressFile)
00057   {
00058    mfpLog.close();
00059   }
00060  }
00061 
00062 
00063  void Log::logMessage(const std::string & message, LogMessageLevel lml, bool maskDebug)
00064  {
00065   for (mtLogListener::iterator i = mListeners.begin(); i != mListeners.end(); ++i)
00066   {
00067    (*i)->MessageLogged(message, lml, maskDebug, this->getObjectName());
00068   }
00069   if (mDebugOutput && !maskDebug)
00070   {
00071    std::cerr << message << std::endl;
00072   }
00073   if (!mSuppressFile)
00074   {
00075    struct tm *pTime;
00076    time_t ctTime;
00077    time(&ctTime);
00078    pTime = localtime(&ctTime);
00079 
00080    if (mTimeOutput)
00081    {
00082     mfpLog << std::setw(2) << std::setfill('0') << pTime->tm_hour
00083      << ":" << std::setw(2) << std::setfill('0') << pTime->tm_min
00084      << ":" << std::setw(2) << std::setfill('0') << pTime->tm_sec
00085      << ": ";
00086    }
00087    mfpLog  << message << std::endl;
00088     
00089    mfpLog.flush();
00090   }
00091  }
00092 
00093 
00094  void Log::logMessage(const Object & object, const std::string & Message,
00095   LogMessageLevel lml, bool maskDebug)
00096  {
00097   if ((mLogLevel + lml) >= Log_Threshold)
00098   {
00099    if (!mSuppressObjectInfo)
00100    {
00101     logMessage(
00102                         auxillary::StringUtil::getFixedString(object.getClassName(), mLogClassNameAlignment, "", ":") +
00103                         auxillary::StringUtil::getFixedString(object.getObjectName(), mLogObjectNameAlignment, "", ":") +
00104                         Message, lml, maskDebug
00105                           );
00106    }
00107    else
00108    {
00109     logMessage(Message, lml, maskDebug);
00110    }
00111   }
00112  }
00113 
00114  
00115  void Log::logMessage(const Object * object, const std::string & Message,
00116   LogMessageLevel lml, bool maskDebug)
00117  {
00118   assert(object && "Cannot log noninitialized object!!!");
00119   logMessage(*object, Message, lml, maskDebug);
00120  }
00121 
00122 
00123  void Log::addListener(LogListener * listener)
00124  {
00125   mListeners.push_back(listener);
00126  }
00127 
00128 
00129  void Log::removeListener(LogListener * listener)
00130  {
00131   mListeners.erase(std::find(mListeners.begin(), mListeners.end(), listener));
00132  }
00133 }

Generated on Thu Mar 5 22:36:42 2009 for gpucalc by  doxygen 1.5.6