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 00033 #include <cmath> 00034 #include <sstream> 00035 00036 LogFile* LogFile::mpInstance = NULL; 00037 00038 LogFile::LogFile() 00039 : mFileSet(false), 00040 mInitTime(time(NULL)), 00041 mLevel(0), 00042 mPrecision(6) 00043 { 00044 } 00045 00046 00047 LogFile* LogFile::Instance() 00048 { 00049 if (mpInstance == NULL) 00050 { 00051 mpInstance = new LogFile; // default construtor which doesn't write 00052 } 00053 return mpInstance; 00054 } 00055 00056 unsigned LogFile::Level() 00057 { 00058 if (mpInstance == NULL) 00059 { 00060 return 0; 00061 } 00062 else 00063 { 00064 return mpInstance->mLevel; 00065 } 00066 } 00067 00068 00069 void LogFile::Set(unsigned level, std::string directory, std::string fileName) 00070 { 00071 if (level > mMaxLoggingLevel) 00072 { 00073 std::stringstream string_stream; 00074 string_stream << "Requested level " << level 00075 << " should have been less than or equal to " << mMaxLoggingLevel; 00076 EXCEPTION(string_stream.str()); 00077 } 00078 mLevel = level; 00079 00082 // std::string file = "/home/chaste/Desktop/" + fileName; 00083 // out_stream p_file(new std::ofstream(file.c_str())); 00084 // mpOutStream = p_file; 00085 00086 OutputFileHandler handler(directory, false); 00087 mpOutStream = handler.OpenOutputFile(fileName); 00088 mFileSet = true; 00089 00090 // write header in the log file..? 00091 } 00092 00093 unsigned LogFile::MaxLoggingLevel() 00094 { 00095 return mMaxLoggingLevel; 00096 } 00097 00098 00099 void LogFile::Close() 00100 { 00101 if (mpInstance) 00102 { 00103 mpInstance->mpOutStream->close(); 00104 delete mpInstance; 00105 mpInstance = NULL; 00106 } 00107 } 00108 00109 void LogFile::WriteHeader(std::string simulationType) 00110 { 00111 *this << "\nChaste: " << simulationType << " simulation, on " << ctime(&mInitTime) << "\n"; 00112 } 00113 00114 void LogFile::WriteElapsedTime(std::string pre) 00115 { 00116 double fsecs = difftime(time(NULL),mInitTime); 00117 long total_secs = static_cast<long>(floor(fsecs+0.5)); 00118 int total_mins = total_secs/60; 00119 00120 int secs = total_secs%60; 00121 int mins = total_mins%60; 00122 int hrs = total_mins/60; 00123 00124 *this << pre << "Elapsed time is: " << hrs << "h " << mins << "m " << secs << "s\n"; 00125 } 00126 00127 bool LogFile::IsFileSet() 00128 { 00129 return mFileSet; 00130 } 00131 00132 00133 void LogFile::SetPrecision(unsigned precision) 00134 { 00135 assert(precision>0); 00136 mPrecision = precision; 00137 }