Created
November 3, 2012 14:12
-
-
Save fpersson/4007484 to your computer and use it in GitHub Desktop.
a simple log, useing c++11
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <fstream> | |
#include <string> | |
#include <chrono> | |
#include <ctime> | |
class FLog{ | |
public: | |
FLog(std::string logfile); | |
void info(std::string tag, std::string msg); | |
private: | |
void writeLog(std::string type, std:: string msg); | |
void rotateLog(); | |
bool fileExist(std::string &filename); | |
void moveFile(std::string src, std::string dest); | |
std::string m_Logfile; | |
int m_numLogs; // 5 default | |
bool m_showTime; //true as default | |
}; | |
FLog::FLog(std::string logfile):m_Logfile(logfile){ | |
m_showTime = true; | |
m_numLogs = 5; | |
rotateLog(); | |
} | |
void FLog::info(std::string tag, std::string msg){ | |
std::string logmsg; | |
logmsg.append(tag); | |
logmsg.append(" - "); | |
logmsg.append(msg); | |
writeLog("I", logmsg); | |
} | |
void FLog::writeLog(std::string type, std:: string msg){ | |
std::ofstream of; | |
of.open(m_Logfile, std::ios::app); | |
if(m_showTime){ | |
std::chrono::system_clock::time_point currentTime = std::chrono::system_clock::now(); | |
time_t tt = std::chrono::system_clock::to_time_t(currentTime); | |
std::string timeString = std::ctime(&tt); | |
timeString.erase(timeString.end()-1, timeString.end()); //tarbort newline | |
of << type << " " << timeString << " " << msg << std::endl; | |
}else{ | |
of << type << " " << msg << std::endl; | |
} | |
of.close(); | |
} | |
void FLog::rotateLog(){ | |
std::string srcLog; | |
std::string destLog; | |
if(fileExist(m_Logfile)){ | |
for(int i = m_numLogs-1; i > 0; --i){ | |
srcLog = m_Logfile; | |
destLog = m_Logfile; | |
srcLog.append("."); | |
int l = i-1; | |
srcLog.append(std::to_string(l)); | |
destLog.append("."); | |
destLog.append(std::to_string(i)); | |
if(fileExist(srcLog)){ | |
moveFile(srcLog, destLog); | |
} | |
srcLog.clear(); | |
srcLog.clear(); | |
} | |
moveFile(m_Logfile, "./messages.log.0"); | |
}else{ | |
std::cout << "No rotate." << std::endl; | |
} | |
} | |
bool FLog::fileExist(std::string &filename){ | |
std::ifstream ifile(filename); | |
return ifile; | |
} | |
void FLog::moveFile(std::string src, std::string dest){ | |
int result = std::rename( src.c_str(), dest.c_str() ); | |
if ( result != 0 ){ | |
std::perror( "Error renaming file" ); | |
} | |
} | |
int main(){ | |
std::cout << "*** STARTING APP ***" << std::endl; | |
FLog Log("./messages.log"); | |
auto start = std::chrono::steady_clock::now(); | |
for(int i = 0; i < 100000; i++){ | |
Log.info("TEST", "Hello, world!"); | |
} | |
auto end = std::chrono::steady_clock::now(); | |
auto diff = end - start; | |
std::cout << "Time: " << std::chrono::duration<double, std::milli>(diff).count() << " miliseconds" << std::endl; | |
std::cout << "***** EXIT APP *****" << std::endl; | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment