logfile.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00029 #include "logfile.h"
00030
00031
00040 LogFile::LogFile(string name,bool verbose,bool keep,string level,string format) {
00041 this->setLogLevel(this->strToLogLevel(level));
00042 this->setVerbose(verbose);
00043 this->setKeepFiles(keep);
00044 this->setPeriodFormat(format);
00045 this->baseFileName = name;
00046 this->stream = new ofstream();
00047 }
00048
00052 LogFile::~LogFile() {
00053 this->close();
00054 delete this->stream;
00055 }
00056
00062 bool LogFile::open() {
00063 bool openned;
00064 this->period = this->systemPeriod();
00065 this->stream->open((this->baseFileName+this->period+".log").c_str(),ios::out|ios::app);
00066 openned = this->stream->is_open();
00067 if (openned) {
00068 this->beginLog();
00069 }
00070 return openned;
00071 }
00072
00076 void LogFile::close() {
00077 this->endLog();
00078 this->stream->flush();
00079 this->stream->close();
00080 }
00081
00085 void LogFile::reopen() {
00086 this->close();
00087 this->open();
00088 }
00089
00094 string LogFile::systemPeriod() {
00095 time_t now ;
00096 time(&now);
00097 char timeFormat[50];
00098 strftime(timeFormat,50,this->getPeriodFormat().c_str(),localtime(&now));
00099 return timeFormat;
00100 }
00101
00112 bool LogFile::log(string line,log_level ll) {
00113 time_t now ;
00114 char timeFormat[50];
00115 string logline,pastperiod;
00116
00117
00118
00119 if((this->systemPeriod() != this->period)) {
00120 pastperiod = this->period ;
00121 this->reopen();
00122 if(!this->keepFiles) {
00123 remove((this->baseFileName+pastperiod+".log").c_str());
00124 }
00125 }
00126 if ( ll != NOTUSED ) {
00127 switch (this->level) {
00128 case NOTUSED : ;
00129 break;
00130 case NOTHING : return true ;
00131 break;
00132 case ERROR : ;
00133 break;
00134 case WARNING : if((ll!=ERROR)&&(ll!=WARNING)) return true ;
00135 break;
00136 case INFO : if(ll!=INFO) return true ;
00137 break;
00138 default : ;
00139 }
00140 }
00141 time(&now);
00142 strftime(timeFormat,16,"%b %e %X",localtime(&now));
00143 logline = (string)timeFormat + " " +this->getLevelTag(ll)+ line ;
00144 if ( this->verbose ){
00145 cout << logline << endl;
00146 }
00147 (*this->stream) << logline << endl;
00148 return (*this->stream);
00149 }
00150
00156 log_level LogFile::strToLogLevel(string ll) {
00157 if(ll=="error")
00158 return ERROR;
00159 else if (ll=="warning")
00160 return WARNING;
00161 else if (ll=="info")
00162 return INFO;
00163 else if (ll=="nothing")
00164 return NOTHING;
00165 else
00166 return NOTUSED;
00167 }
00168
00173 void LogFile::setPeriodFormat(string format) {
00174 this->periodFormat = format;
00175 }
00176
00181 string LogFile::getPeriodFormat() {
00182 return this->periodFormat;
00183 }
00184
00189 void LogFile::setLogLevel(string ll) {
00190 this->level = this->strToLogLevel(ll) ;
00191 }
00192
00197 void LogFile::setLogLevel(log_level ll) {
00198 this->level = ll ;
00199 }
00200
00205 log_level LogFile::getLogLevel() {
00206 return this->level;
00207 }
00208
00213 void LogFile::setKeepFiles(bool state) {
00214 this->keepFiles = state ;
00215 }
00216
00221 bool LogFile::getKeepFiles() {
00222 return this->keepFiles;
00223 }
00224
00229 void LogFile::setVerbose(bool state) {
00230 this->verbose = state;
00231 }
00232
00237 bool LogFile::getVerbose() {
00238 return this->verbose;
00239 }
00240
00245 bool LogFile::checkFile() {
00246 struct stat stats;
00247 if (stat((this->baseFileName+this->period+".log").c_str(),&stats)==0)
00248 return true;
00249 else
00250 return false;
00251 }
00252
00256 void LogFile::beginLog() {
00257 time_t now ;
00258 time(&now);
00259 char timeFormat[24];
00260 strftime(timeFormat,25,"%a %b %e %X %Y",localtime(&now));
00261 (*this->stream) << "**** BEGIN LOGGING AT "<< timeFormat << endl << endl ;
00262 }
00263
00267 void LogFile::endLog() {
00268 time_t now ;
00269 time(&now);
00270 char timeFormat[24];
00271 strftime(timeFormat,25,"%a %b %e %X %Y",localtime(&now));
00272 (*this->stream) << "**** ENDING LOGGING AT " << timeFormat << endl << endl ;
00273 }
00274
00280 string LogFile::getLevelTag(log_level ll) {
00281 switch(ll) {
00282 case NOTUSED : return "";
00283 break;
00284 case NOTHING : return "";
00285 break;
00286 case ERROR : return "{E} ";
00287 break;
00288 case WARNING : return "{W} ";
00289 break;
00290 case INFO : return "{I} " ;
00291 break;
00292 default : return "";
00293 }
00294 }
00295