Skip to content

Instantly share code, notes, and snippets.

@devdilson
Created April 1, 2019 10:23
Show Gist options
  • Save devdilson/3d7803a198c7b461866c0efbfe372e71 to your computer and use it in GitHub Desktop.
Save devdilson/3d7803a198c7b461866c0efbfe372e71 to your computer and use it in GitHub Desktop.
A C++ class to log information
#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);
}
#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