wutil_log.cpp

00001 // wutil_log.cpp
00002 //
00003 // CLog class implementation
00004 //
00005 //
00006 // Copyright (c) 2002-2004 Rob Wiskow
00007 // rob-dev@boxedchaos.com
00008 //
00009 // This library is free software; you can redistribute it and/or
00010 // modify it under the terms of the GNU Lesser General Public
00011 // License as published by the Free Software Foundation; either
00012 // version 2.1 of the License, or (at your option) any later version.
00013 //
00014 // This library is distributed in the hope that it will be useful,
00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00017 // Lesser General Public License for more details.
00018 //
00019 // You should have received a copy of the GNU Lesser General Public
00020 // License along with this library; if not, write to the Free Software
00021 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
00022 //
00023 
00024 
00025 #include "wgui_include_config.h"
00026 #include "wutil_log.h"
00027 #include <fstream>
00028 
00029 
00030 namespace wUtil
00031 {
00032 
00033 CLog::t_LogList CLog::GetLogEntries(unsigned int iSeverity) const
00034 {
00035      t_LogList OutLog;
00036      for (t_LogList::const_iterator iter = m_LogList.begin(); iter != m_LogList.end(); ++iter)
00037      {
00038           if (iSeverity == 0 || iter->m_iSeverity == iSeverity)
00039           {
00040                OutLog.push_back(*iter);
00041           }
00042      }
00043      return OutLog;
00044 }
00045 
00046 
00047 CLog::t_LogList CLog::GetLogEntries(unsigned int iHighSeverity, unsigned int iLowSeverity) const
00048 {
00049      t_LogList OutLog;
00050      for (t_LogList::const_iterator iter = m_LogList.begin(); iter != m_LogList.end(); ++iter)
00051      {
00052           if (iter->m_iSeverity >= iHighSeverity || iter->m_iSeverity <= iLowSeverity)
00053           {
00054                OutLog.push_back(*iter);
00055           }
00056      }
00057      return OutLog;
00058 }
00059 
00060 
00061 void CLog::WriteToFile(const std::wstring& sFilename, bool bAppend, const std::wstring& sLogHeader) const
00062 {
00063      std::wofstream File;
00064      if (bAppend)
00065      {
00066           File.open(stdex::ToMbString(sFilename).c_str(), std::ios::app);
00067      }
00068      else
00069      {
00070           File.open(stdex::ToMbString(sFilename).c_str(), std::ios::out | std::ios::trunc);
00071      }
00072      if (File.is_open())
00073      {
00074           File << sLogHeader << std::endl;
00075           for (t_LogList::const_iterator iter = m_LogList.begin(); iter != m_LogList.end(); ++iter)
00076           {
00077                wchar_t sTime[100]; // hopefully the timestamp is no more than 100 characters, but it would be good to change this to a system define
00078                mbstowcs(sTime, asctime(localtime(&iter->m_TimeStamp)), 100);
00079                std::wstring sTimeStamp(sTime);
00080                File << sTimeStamp.substr(0, sTimeStamp.size() - 1) << L" [ " << iter->m_iSeverity << L" ] : " << iter->m_sMessage << std::endl;
00081           }
00082           File.close();
00083      }
00084 }
00085 
00086 }
00087 
00088 

Generated on Wed May 16 23:11:26 2007 for wGui by  doxygen 1.5.1