00001 /* 00002 Copyright 2007 Erez Bibi (erezbibi@users.sourceforge.net) 00003 This file is part of Beesnest. 00004 00005 Beesnest 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 Beesnest 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 Beesnest; if not, write to the Free Software 00017 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00018 */ 00019 00020 00021 /** 00022 * DcLogger.cpp: implementation of the CdcLogger class. 00023 * for documentation see the header file. 00024 */ 00025 00026 #include "DcLogger.h" 00027 00028 void CdcLogger::OpenLogFile () 00029 { 00030 tLogOpen = CdcHttpTime::GetCurrentTime (); 00031 CdcString name; 00032 switch (nPeriod) 00033 { 00034 case LOG_PERIOD_DAY: 00035 name = tLogOpen.Format (LOG_NAME_DAY); 00036 break; 00037 case LOG_PERIOD_WEEK: 00038 name = tLogOpen.Format (LOG_NAME_WEEK); 00039 break; 00040 case LOG_PERIOD_MONTH: 00041 name = tLogOpen.Format (LOG_NAME_MONTH); 00042 break; 00043 } 00044 if (name.IsEmpty ()) return; 00045 name = CdcFileUtil::GetWorkPath (name); 00046 oLogFile.open (name.GetBuffer (), ios::app); 00047 } 00048 00049 void CdcLogger::CloseLogFile () 00050 { 00051 if (oLogFile.is_open ()) 00052 { 00053 oLogFile.flush (); 00054 oLogFile.close (); 00055 } 00056 } 00057 00058 bool CdcLogger::CheckReplaceLogFile () 00059 { 00060 CdcHttpTime now; 00061 switch (nPeriod) 00062 { 00063 case LOG_PERIOD_DAY: 00064 return (tLogOpen.GetDays () != now.GetDays ()); 00065 case LOG_PERIOD_WEEK: 00066 return (tLogOpen.GetWeeks () != now.GetWeeks ()); 00067 case LOG_PERIOD_MONTH: 00068 return (tLogOpen.GetMonths () != now.GetMonths ()); 00069 } 00070 return false; 00071 } 00072 00073 void CdcLogger::PrintString (CdcString str, int level, bool console) 00074 { 00075 if (level <= nPrintLevel) 00076 { 00077 goMutex.Lock (); 00078 if (level != PS_LOG && console) 00079 cout<<str.GetBuffer ()<<endl; /* Print to console. */ 00080 else if (CheckReplaceLogFile ()) 00081 { /* Need to replace the log file. */ 00082 CloseLogFile (); 00083 OpenLogFile (); 00084 } 00085 if (oLogFile.is_open ()) 00086 oLogFile<<str<<endl; /* Print to file. */ 00087 goMutex.Unlock (); 00088 } 00089 } 00090 00091 void CdcLogger::Init () 00092 { 00093 CdcString period = CdcParameters::GetParam 00094 ("Server->Log-File->Log-Period"); 00095 if (period == "day") nPeriod = LOG_PERIOD_DAY; 00096 else if (period == "week") nPeriod = LOG_PERIOD_WEEK; 00097 else if (period == "month") nPeriod = LOG_PERIOD_MONTH; 00098 00099 nPrintLevel = CdcParameters::ToInt (CdcParameters::GetParam 00100 ("Server->Log-File->Log-Level")); 00101 if (nPrintLevel > 0) 00102 OpenLogFile (); 00103 } 00104