g++ -o example.out logExample.cpp
Enjoy:
//logExample.h file
#ifndef LOGEXAMPLE_H
#define LOGEXAMPLE_H
#include "stdio.h"
#include "stdlib.h"
#include
#include
using std::string;
class log_example
{
public:
log_example();
~log_example();
void printLog(std::string message);
void printLogError(std::string message);
private:
void printDateTime();
FILE * pFile;
};
#endif
//logExample.cpp file
#include "log_example.h"
log_example::log_example()
{
//create log file
char outstr[200];
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
strftime(outstr, sizeof(outstr), "%F", tmp);
std::string logFile(outstr);
logFile.append(".log");
pFile = fopen (logFile.c_str(),"a");
printLog( "log_example constructor called");
}
log_example::~log_example()
{
printLog( "log_example destructor called");
fclose (pFile);
}
void log_example::printDateTime()
{
char outstr[200];
time_t t;
struct tm *tmp;
t = time(NULL);
tmp = localtime(&t);
fputs( "\n" ,pFile);
strftime(outstr, sizeof(outstr), "%F", tmp);
fputs( outstr, pFile);
fputs( " " ,pFile);
strftime(outstr, sizeof(outstr), "%T", tmp);
fputs( outstr, pFile);
fputs( " " ,pFile);
}
void log_example::printLog(std::string message)
{
printDateTime();
fputs( message.c_str(),pFile);
fflush (pFile);
}
void log_example::printLogError(std::string message)
{
printDateTime();
fputs( "ERROR: ",pFile);
fputs( message.c_str(),pFile);
fflush (pFile);
}
0 comments:
Post a Comment