00001 /* 00002 00003 Copyright (C) University of Oxford, 2005-2011 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 #include "LogFile.hpp" 00030 #include "Exception.hpp" 00031 00032 #include <cmath> 00033 #include <sstream> 00034 00035 LogFile* LogFile::mpInstance = NULL; 00036 00037 LogFile::LogFile() 00038 : mFileSet(false), 00039 mInitTime(time(NULL)), 00040 mLevel(0), 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 void LogFile::Set(unsigned level, const std::string& rDirectory, const std::string& rFileName) 00067 { 00068 if (level > mMaxLoggingLevel) 00069 { 00070 EXCEPTION("Requested level " << level 00071 << " should have been less than or equal to " 00072 << mMaxLoggingLevel); 00073 } 00074 mLevel = level; 00075 00078 // std::string file = "/home/chaste/Desktop/" + fileName; 00079 // out_stream p_file(new std::ofstream(file.c_str())); 00080 // mpOutStream = p_file; 00081 00082 OutputFileHandler handler(rDirectory, false); 00083 mpOutStream = handler.OpenOutputFile(rFileName); 00084 mFileSet = true; 00085 00086 // Write header in the log file..? 00087 } 00088 00089 unsigned LogFile::MaxLoggingLevel() 00090 { 00091 return mMaxLoggingLevel; 00092 } 00093 00094 void LogFile::Close() 00095 { 00096 if (mpInstance) 00097 { 00098 mpInstance->mpOutStream->close(); 00099 delete mpInstance; 00100 mpInstance = NULL; 00101 } 00102 } 00103 00104 void LogFile::WriteHeader(std::string simulationType) 00105 { 00106 *this << "\nChaste: " << simulationType << " simulation, on " << ctime(&mInitTime) << "\n"; 00107 } 00108 00109 void LogFile::WriteElapsedTime(std::string pre) 00110 { 00111 double fsecs = difftime(time(NULL),mInitTime); 00112 long total_secs = static_cast<long>(floor(fsecs+0.5)); 00113 int total_mins = total_secs/60; 00114 00115 int secs = total_secs%60; 00116 int mins = total_mins%60; 00117 int hrs = total_mins/60; 00118 00119 *this << pre << "Elapsed time is: " << hrs << "h " << mins << "m " << secs << "s\n"; 00120 } 00121 00122 bool LogFile::IsFileSet() 00123 { 00124 return mFileSet; 00125 } 00126 00127 void LogFile::SetPrecision(unsigned precision) 00128 { 00129 assert(precision > 0); 00130 mPrecision = precision; 00131 }