00001 #include <game.h>
00002
00003 #include <iostream>
00004 #include <fstream>
00005 #include <string>
00006 #include <sys/types.h>
00007 #include <sys/stat.h>
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018 logger::logger() : base("logger")
00019 {
00020 files = new std::vector<std::ofstream*>;
00021 }
00022
00023 logger::~logger()
00024 {
00025 delete files;
00026 }
00027
00028 int logger::init(coeur* c)
00029 {
00030 base::init(0,c);
00031 return 1;
00032 }
00033
00034 int logger::close()
00035 {
00036 base::close();
00037 return 1;
00038 }
00039
00040 int logger::createFile(stringc name, int idClass)
00041 {
00042 int idLog = files->size();
00043 stringc path = "log/";
00044 mkdir(path.c_str(),01777);
00045 path += name + "/";
00046 mkdir(path.c_str(),01777);
00047 path += stringc(idClass) + ".log";
00048 std::ofstream* file = new std::ofstream(path.c_str());
00049 files->push_back(file);
00050 write(idLog,stringc("Class : " ) + name);
00051 write(idLog,stringc("Id : ") + stringc(idClass));
00052 write(idLog,stringc("Begin log" ));
00053 write(idLog,stringc("------------------------------------------------------------"));
00054 if (name != stringc("logger"))
00055 log( stringc("Logger create a new file for class ") + name + " of id " + stringc(idClass) + ". The log id is " + stringc(idLog));
00056 else
00057 write(idLog, stringc("The log id of this logger is ") + stringc(idLog));
00058 return idLog;
00059 }
00060
00061 int logger::write(int idLog,stringc text)
00062 {
00063 text = stringc(mycore->getTime()/1000) + ":" + text + "\n";
00064 (*files->at(idLog)) << text.c_str();
00065 files->at(idLog)->flush();
00066 return 1;
00067 }
00068
00069 int logger::closeFile(int idLog)
00070 {
00071 log(stringc("Close the log of id ") + stringc(idLog));
00072 write(idLog,stringc("------------------------------------------------------------"));
00073 write(idLog,stringc("End log"));
00074 files->at(idLog)->close();
00075 delete files->at(idLog);
00076 return 1;
00077 }
00078
00079
00080 bool logger::OnEvent(const SEvent& event)
00081 {
00082 return false;
00083 }
00084
00085