FileLogger.cpp

Go to the documentation of this file.
00001 ////////////////////////////////////////////////////////////////////////////////
00002 //    Scorched3D (c) 2000-2009
00003 //
00004 //    This file is part of Scorched3D.
00005 //
00006 //    Scorched3D is free software; you can redistribute it and/or modify
00007 //    it under the terms of the GNU General Public License as published by
00008 //    the Free Software Foundation; either version 2 of the License, or
00009 //    (at your option) any later version.
00010 //
00011 //    Scorched3D is distributed in the hope that it will be useful,
00012 //    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 //    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 //    GNU General Public License for more details.
00015 //
00016 //    You should have received a copy of the GNU General Public License
00017 //    along with Scorched3D; if not, write to the Free Software
00018 //    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00019 ////////////////////////////////////////////////////////////////////////////////
00020 
00021 #include <common/FileLogger.h>
00022 #include <common/Defines.h>
00023 #include <time.h>
00024 
00025 FileLogger::FileLogger(const std::string &fileName) : 
00026         size_(0), logFile_(0), fileName_(fileName)
00027 {
00028 
00029 }
00030 
00031 FileLogger::~FileLogger()
00032 {
00033 }
00034 
00035 void FileLogger::logMessage(LoggerInfo &info)
00036 {
00037         const unsigned int MaxSize = 256000;
00038         if (!logFile_ || (size_>MaxSize)) openFile(fileName_.c_str());
00039         if (!logFile_) return;
00040 
00041         // Log to file and flush file
00042         size_ += strlen(info.getMessage());
00043         fprintf(logFile_, "%s - %s\n", info.getTime(), info.getMessage());
00044         fflush(logFile_);
00045 }
00046 
00047 void FileLogger::openFile(const char *fileName)
00048 {
00049         size_ = 0;
00050         if (logFile_) fclose(logFile_);
00051 
00052         time_t theTime = time(0);
00053         struct tm *newtime = localtime(&theTime); 
00054 
00055         std::string logFileName = 
00056                 S3D::formatStringBuffer("%s-%i%02i%02i-%02i%02i%02i.log", fileName,
00057                         newtime->tm_year + 1900, newtime->tm_mon + 1, newtime->tm_mday,
00058                         newtime->tm_hour, newtime->tm_min, newtime->tm_sec);
00059         std::string fullLogFileName = S3D::getLogFile(logFileName.c_str());
00060         logFile_ = fopen(fullLogFileName.c_str(), "w");
00061 }

Generated on Mon Feb 16 15:14:48 2009 for Scorched3D by  doxygen 1.5.3