Go to the documentation of this file.00001
00019 #include <iostream>
00020 #include <cstring>
00021 #include <fstream>
00022 #include <cstdio>
00023 #include <sys/times.h>
00024 #include <sys/time.h>
00025
00026 #include "log.h"
00027
00028 CLog globalLog;
00029
00030 CLog::CLog()
00031 {
00032 debugLevel = 0;
00033 inputLevel = 0;
00034 }
00035 void CLog::setInputLevel(int inputLevel)
00036 {
00037 this->inputLevel = inputLevel;
00038 }
00039
00040 void CLog::setDebugLevel(int debugLevel)
00041 {
00042 this->debugLevel = debugLevel;
00043 }
00044
00045 void CLog::writeLine(std::string line, bool append)
00046 {
00047 std::ofstream newFile;
00048 newFile.open(this->filename.c_str(), std::ios::out | append ? std::ios::app : std::ios::trunc);
00049
00050 if (newFile)
00051 newFile << getTimeAsStr() << ": " << line << "\n";
00052 newFile.close();
00053 }
00054
00055 std::string CLog::getTimeAsStr()
00056 {
00057 static struct timeval tstart;
00058 static struct timezone tz;
00059 struct tm* ptm;
00060
00061 gettimeofday(&tstart, &tz);
00062 ptm = localtime(&tstart.tv_sec);
00063 char time[1024];
00064 strftime(time, sizeof (time), "%Y-%m-%d %H:%M:%S", ptm);
00065 return time;
00066 }
00067 std::string CLog::generateLogname(std::string path)
00068 {
00069 char buffer[1024];
00070 std::string time = getTimeAsStr();
00071 sprintf(buffer, "%s/logs/log_%s", path.c_str(), time.c_str());
00072 return buffer;
00073 }
00074
00075 void CLog::setFilename(std::string filename)
00076 {
00077 this->filename = filename;
00078 }