Main Page | Class Hierarchy | Alphabetical List | Class List | File List | Class Members | File Members | Related Pages

log.cpp

Go to the documentation of this file.
00001 // PTF - Psychological Test Framework 00002 // http://ptf.sourceforge.net 00003 // 00004 // This program is free software; you can redistribute it and/or modify 00005 // it under the terms of the GNU General Public License as published by 00006 // the Free Software Foundation; either version 2 of the License, or 00007 // (at your option) any later version. 00008 // 00009 // This program is distributed in the hope that it will be useful, 00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 // GNU Library General Public License for more details. 00013 // 00014 // You should have received a copy of the GNU General Public License 00015 // along with this program; if not, write to the Free Software 00016 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 00017 00018 #include <ptf/core/log.hpp> 00019 #include <fstream> 00020 #include <iostream> 00021 #include <stdexcept> 00022 00023 // bring stuff into scope 00024 using std::ofstream; 00025 using std::cout; 00026 using std::endl; 00027 00031 Log::Log() { 00032 silent = false; 00033 } 00034 00039 Log::Log(bool in) { 00040 silent = in; 00041 } 00042 00046 Log::~Log() { 00047 // nothing to do here 00048 } 00049 00054 bool Log::getSilent() { 00055 return silent; 00056 } 00057 00062 void Log::setSilent(bool in) { 00063 silent = in; 00064 } 00065 00070 Log& Log::operator<<(const string& in) { 00071 mutex.enterMutex(); 00072 sstream << in; 00073 mutex.leaveMutex(); 00074 if (!silent) cout << in; 00075 return *this; 00076 } 00077 00082 Log& Log::operator<<(const char* in) { 00083 mutex.enterMutex(); 00084 sstream << in; 00085 mutex.leaveMutex(); 00086 if (!silent) cout << in; 00087 return *this; 00088 } 00089 00094 Log& Log::operator<<(const double& in) { 00095 mutex.enterMutex(); 00096 sstream << in; 00097 mutex.leaveMutex(); 00098 if (!silent) cout << in; 00099 return *this; 00100 } 00101 00106 Log& Log::operator<<(const float& in) { 00107 mutex.enterMutex(); 00108 sstream << in; 00109 mutex.leaveMutex(); 00110 if (!silent) cout << in; 00111 return *this; 00112 } 00113 00118 Log& Log::operator<<(const int& in) { 00119 mutex.enterMutex(); 00120 sstream << in; 00121 mutex.leaveMutex(); 00122 if (!silent) cout << in; 00123 return *this; 00124 } 00125 00130 Log& Log::operator<<(const timespec& in) { 00131 mutex.enterMutex(); 00132 sstream << in.tv_sec << " " << in.tv_nsec << " "; 00133 mutex.leaveMutex(); 00134 if (!silent) cout << in.tv_sec << " " << in.tv_nsec << " "; 00135 return *this; 00136 } 00137 00143 void Log::saveToFile(const char* fname) { 00144 mutex.enterMutex(); 00145 ofstream fout(fname, std::ios::app); 00146 if (!fout.good()) { 00147 mutex.leaveMutex(); 00148 cout << "ERROR: Can't save log file (" << fname <<").\n"; 00149 throw std::runtime_error("Can't save log file."); 00150 } 00151 fout << sstream.str(); 00152 fout.close(); 00153 mutex.leaveMutex(); 00154 } 00155

Generated on Fri Dec 17 14:54:23 2004 for Psychological Test Framework by doxygen 1.3.8