Created
April 1, 2019 10:23
-
-
Save devdilson/3d7803a198c7b461866c0efbfe372e71 to your computer and use it in GitHub Desktop.
A C++ class to log information
This file contains 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 "CLog.h" | |
#include <time.h> | |
#include <cstdarg> | |
#include <iostream> | |
#include <fstream> | |
void CLog::Log(string format, ...) { | |
char buf[1024] = { 0 }; | |
va_list args; | |
va_start(args, format); | |
time_t rawtime; | |
struct tm * timeinfo; | |
char buffer[80]; | |
time(&rawtime); | |
timeinfo = localtime(&rawtime); | |
strftime(buffer, sizeof(buffer), "%d-%m-%Y %H:%M:%S", timeinfo); | |
std::string str(buffer); | |
vsprintf(buf, format.c_str(), args); | |
ofstream os; | |
os.open(filePath->c_str(), ios::app); | |
os << buffer; | |
os << " - "; | |
os << buf; | |
os << "\r\n"; | |
va_end(args); | |
} |
This file contains 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 <string> | |
using namespace std; | |
class CLog { | |
private: | |
string *filePath; | |
public: | |
CLog(const char *file) { | |
remove(file); | |
this->filePath = new string(file); | |
} | |
~CLog() { | |
delete filePath; | |
} | |
void Log(string format, ...); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment