00001 /*========================================================================= 00002 00003 Program: F.R.E.E. - flexible registration evaluation engine 00004 Version: v.1.0.0 00005 Date: $Date: 2006/09/01 12:00:00 $ 00006 Module: $RCSfile: freLogFileController.cxx,v $ 00007 Language: C++ 00008 00009 00010 00011 Copyright (c) 2007 Ralf o Floca (Department of Medical Informatics, 00012 Institute for Medical Biometry and Informatics, University of Heidelberg, 00013 Germany). All rights reserved. 00014 See FREECopyright.txt or http://www.mi.med.uni-hd.de/free/copyright.htm 00015 for details. 00016 00017 This software is distributed WITHOUT ANY WARRANTY; without even 00018 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR 00019 PURPOSE. See the above copyright notices for more information. 00020 00021 =========================================================================*/ 00022 00023 #include "freLogFileController.h" 00024 #include "freElementals.h" 00025 00026 #include <time.h> 00027 00028 namespace FREE 00029 { 00030 00031 static std::string ActTimeStampToStr() 00032 { 00033 char tbuffer [40]; 00034 00035 time_t now; 00036 time(&now); 00037 tm* nowTM = localtime(&now); 00038 00039 strftime(tbuffer,20,"%m/%d/%y %H:%M:%S",nowTM); 00040 00041 std::string sTimeStamp = tbuffer; 00042 00043 return sTimeStamp; 00044 }; 00045 00046 00047 bool 00048 LogFileController:: 00049 StartLog() 00050 { 00051 bool result = true; 00052 00053 if (m_bActive) 00054 { 00055 StopLog(); 00056 m_bActive = false; 00057 } 00058 00059 result = OpenLogFile(); 00060 if (result) 00061 { 00062 m_LogFile << m_sHeader <<std::endl; 00063 00064 m_LogFile << "Log start: "+ActTimeStampToStr() << std::endl << std::endl; 00065 m_LogFile.flush(); 00066 00067 m_bActive = true; 00068 } 00069 00070 return result; 00071 }; 00072 00073 bool 00074 LogFileController:: 00075 StartLog(const std::string sLogFileName) 00076 { 00077 SetFileName(sLogFileName); 00078 return StartLog(); 00079 }; 00080 00081 bool 00082 LogFileController:: 00083 StopLog() 00084 { 00085 bool result = true; 00086 00087 if (m_bActive) 00088 { 00089 m_LogFile << std::endl << "Log stop: "+ActTimeStampToStr() << std::endl; 00090 m_LogFile <<"***********************************"<< std::endl << std::endl; 00091 m_LogFile.flush(); 00092 00093 result = CloseLogFile(); 00094 if (result) m_bActive = false; 00095 } 00096 00097 return result; 00098 }; 00099 00100 bool 00101 LogFileController:: 00102 Active() const 00103 { 00104 return m_bActive; 00105 }; 00106 00107 bool 00108 LogFileController:: 00109 AddEntry(const std::string sEntry, const bool bAddTimeStamp) 00110 { 00111 bool result = false; 00112 if (m_bActive) 00113 { 00114 try 00115 { 00116 std::string sLine; 00117 00118 if (bAddTimeStamp) 00119 { 00120 sLine = ActTimeStampToStr(); 00121 sLine = sLine+" >> "+sEntry; 00122 } 00123 else sLine = sEntry; 00124 00125 m_LogFile << sLine << std::endl; 00126 m_LogFile.flush(); 00127 result = true; 00128 } 00129 catch (...){/* ignore all exceptions... */} 00130 } 00131 return result; 00132 }; 00133 00134 bool 00135 LogFileController:: 00136 AddEntry(const std::string sEntry) 00137 { 00138 return AddEntry(sEntry,m_bInsertTime); 00139 }; 00140 00141 bool 00142 LogFileController:: 00143 AddDirectEntry(const std::string sEntry) 00144 { 00145 if (!m_bActive) StartLog(); 00146 bool result = AddEntry(sEntry); 00147 StopLog(); 00148 return result; 00149 }; 00150 00151 void 00152 LogFileController:: 00153 SetFileName(const std::string sFileName) 00154 { 00155 if (sFileName!=m_sFileName) 00156 { 00157 StopLog(); 00158 m_sFileName = sFileName; 00159 }; 00160 }; 00161 00162 const std::string 00163 LogFileController:: 00164 GetFileName() const 00165 { 00166 return m_sFileName; 00167 }; 00168 00169 void 00170 LogFileController:: 00171 SetHeader(const std::string sHeader) 00172 { 00173 if (sHeader!=m_sHeader) m_sHeader = sHeader; 00174 }; 00175 00176 const std::string 00177 LogFileController:: 00178 GetHeader() const 00179 { 00180 return m_sHeader; 00181 }; 00182 00183 void 00184 LogFileController:: 00185 SetInsertTimeStamp(const bool bTime) 00186 { 00187 if (bTime!=m_bInsertTime) m_bInsertTime = bTime; 00188 }; 00189 00190 const 00191 bool 00192 LogFileController:: 00193 GetInsertTimeStamp() const 00194 { 00195 return m_bInsertTime; 00196 }; 00197 00198 void 00199 LogFileController:: 00200 SetAppendFile(const bool bAppend) 00201 { 00202 m_bAppendFile = bAppend; 00203 }; 00204 00205 const bool 00206 LogFileController:: 00207 GetAppendFile() const 00208 { 00209 return m_bAppendFile; 00210 }; 00211 00212 LogFileController:: 00213 LogFileController() 00214 { 00215 m_sFileName = "free.log"; 00216 m_sHeader = "Logfile"; 00217 m_bActive = false; 00218 m_bInsertTime = true; 00219 m_bAppendFile = true; 00220 }; 00221 00222 00223 LogFileController:: 00224 LogFileController(const std::string sFileName, const std::string sHeader, 00225 const bool bAppend, const bool bInsertTime) 00226 { 00227 m_sFileName = sFileName; 00228 m_sHeader = sHeader; 00229 m_bActive = false; 00230 m_bInsertTime = bInsertTime; 00231 m_bAppendFile = bAppend; 00232 }; 00233 00234 LogFileController:: 00235 ~LogFileController() 00236 { 00237 if (m_bActive) StopLog(); 00238 }; 00239 00240 bool 00241 LogFileController:: 00242 OpenLogFile() 00243 { 00244 std::ios_base::openmode iOpenFlag; 00245 00246 bool result = false; 00247 00248 if (!m_bActive) 00249 { 00250 if (m_bAppendFile) iOpenFlag = std::ios_base::out | std::ios_base::app; 00251 else iOpenFlag = std::ios_base::out | std::ios_base::trunc; 00252 m_LogFile.open(m_sFileName.c_str(), iOpenFlag ); 00253 00254 result = m_LogFile.is_open(); 00255 } 00256 return result; 00257 }; 00258 00259 bool 00260 LogFileController:: 00261 CloseLogFile() 00262 { 00263 bool result = false; 00264 00265 if (m_bActive) 00266 { 00267 m_LogFile.close(); 00268 result = true; 00269 }; 00270 00271 return result; 00272 }; 00273 00274 }//End of Namespace free
1.5.3 written by Dimitri van Heesch,
© 1997-2000