00001 /* 00002 00003 Copyright (C) University of Oxford, 2005-2009 00004 00005 University of Oxford means the Chancellor, Masters and Scholars of the 00006 University of Oxford, having an administrative office at Wellington 00007 Square, Oxford OX1 2JD, UK. 00008 00009 This file is part of Chaste. 00010 00011 Chaste is free software: you can redistribute it and/or modify it 00012 under the terms of the GNU Lesser General Public License as published 00013 by the Free Software Foundation, either version 2.1 of the License, or 00014 (at your option) any later version. 00015 00016 Chaste is distributed in the hope that it will be useful, but WITHOUT 00017 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 00018 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 00019 License for more details. The offer of Chaste under the terms of the 00020 License is subject to the License being interpreted in accordance with 00021 English Law and subject to any action against the University of Oxford 00022 being under the jurisdiction of the English Courts. 00023 00024 You should have received a copy of the GNU Lesser General Public License 00025 along with Chaste. If not, see <http://www.gnu.org/licenses/>. 00026 00027 */ 00028 00029 00030 #include "LogFile.hpp" 00031 #include "Exception.hpp" 00032 #include <cmath> 00033 00034 LogFile* LogFile::mpInstance = NULL; 00035 00036 LogFile::LogFile() 00037 { 00038 mFileSet = false; 00039 mLevel = 0; 00040 mInitTime = time(NULL); 00041 mPrecision = 6; 00042 } 00043 00044 00045 LogFile* LogFile::Instance() 00046 { 00047 if (mpInstance == NULL) 00048 { 00049 mpInstance = new LogFile; // default construtor which doesn't write 00050 } 00051 return mpInstance; 00052 } 00053 00054 unsigned LogFile::Level() 00055 { 00056 if (mpInstance == NULL) 00057 { 00058 return 0; 00059 } 00060 else 00061 { 00062 return mpInstance->mLevel; 00063 } 00064 } 00065 00066 00067 void LogFile::Set(unsigned level, std::string directory, std::string fileName) 00068 { 00069 if (level > mMaxLoggingLevel) 00070 { 00071 std::stringstream string_stream; 00072 string_stream << "Requested level " << level 00073 << " should have been less than or equal to " << mMaxLoggingLevel; 00074 EXCEPTION(string_stream.str()); 00075 } 00076 mLevel = level; 00077 00080 // std::string file = "/home/chaste/Desktop/" + fileName; 00081 // out_stream p_file(new std::ofstream(file.c_str())); 00082 // mpOutStream = p_file; 00083 00084 OutputFileHandler handler(directory, false); 00085 mpOutStream = handler.OpenOutputFile(fileName); 00086 mFileSet = true; 00087 00088 // write header in the log file..? 00089 } 00090 00091 unsigned LogFile::MaxLoggingLevel() 00092 { 00093 return mMaxLoggingLevel; 00094 } 00095 00096 00097 void LogFile::Close() 00098 { 00099 if (mpInstance) 00100 { 00101 mpInstance->mpOutStream->close(); 00102 delete mpInstance; 00103 mpInstance = NULL; 00104 } 00105 } 00106 00107 void LogFile::WriteHeader(std::string simulationType) 00108 { 00109 *this << "\nChaste: " << simulationType << " simulation, on " << ctime(&mInitTime) << "\n"; 00110 } 00111 00112 void LogFile::WriteElapsedTime(std::string pre) 00113 { 00114 double fsecs = difftime(time(NULL),mInitTime); 00115 long total_secs = static_cast<long>(floor(fsecs+0.5)); 00116 int total_mins = total_secs/60; 00117 00118 int secs = total_secs%60; 00119 int mins = total_mins%60; 00120 int hrs = total_mins/60; 00121 00122 *this << pre << "Elapsed time is: " << hrs << "h " << mins << "m " << secs << "s\n"; 00123 } 00124 00125 bool LogFile::IsFileSet() 00126 { 00127 return mFileSet; 00128 } 00129 00130 00131 void LogFile::SetPrecision(unsigned precision) 00132 { 00133 assert(precision>0); 00134 mPrecision = precision; 00135 }