1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2011
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
10 //
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License along
17 // with this program; if not, write to the Free Software Foundation, Inc.,
18 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 ////////////////////////////////////////////////////////////////////////////////
20
21 #include <common/FileLogger.h>
22 #include <common/Defines.h>
23 #include <time.h>
24
FileLogger(const std::string & fileName)25 FileLogger::FileLogger(const std::string &fileName) :
26 size_(0), logFile_(0), fileName_(fileName), fileCount_(0)
27 {
28
29 }
30
~FileLogger()31 FileLogger::~FileLogger()
32 {
33 }
34
logMessage(LoggerInfo & info)35 void FileLogger::logMessage(LoggerInfo &info)
36 {
37 const unsigned int MaxSize = 256000;
38 if (!logFile_ || (size_>MaxSize)) openFile(fileName_.c_str());
39 if (!logFile_) return;
40
41 // Log to file and flush file
42 size_ += (unsigned int) strlen(info.getMessage());
43 fprintf(logFile_, "%s - %s\n", info.getTime(), info.getMessage());
44 fflush(logFile_);
45 }
46
openFile(const char * fileName)47 void FileLogger::openFile(const char *fileName)
48 {
49 size_ = 0;
50 if (logFile_) fclose(logFile_);
51
52 time_t theTime = time(0);
53 struct tm *newtime = localtime(&theTime);
54
55 std::string logFileName =
56 S3D::formatStringBuffer("%s-%i%02i%02i-%02i%02i%02i-%u.log", fileName,
57 newtime->tm_year + 1900, newtime->tm_mon + 1, newtime->tm_mday,
58 newtime->tm_hour, newtime->tm_min, newtime->tm_sec, fileCount_);
59 fileCount_++;
60
61 std::string fullLogFileName = S3D::getLogFile(logFileName.c_str());
62 logFile_ = fopen(fullLogFileName.c_str(), "w");
63 }
64