c++ 简单log输出文件

 

主文件 

#include "SimpleLog.h"
using namespace std;

static void writeFile();
void log();
int main()
{
    std::cout << "Hello World!\n";
    SimpleLog log;
    log.Write("test2");
    double db = 10.05;
    log.Write("dobule:", db);
}

 

SimpleLog.h

#pragma once
#include<ctime>
#include <string>
#include <string>
#include <fstream>
using namespace std;


class SimpleLog
{
private:
	tm mPCreateFileTime;
	ofstream mOutfile;
	void CreateFile();
	void CheckCreate();
public:
	SimpleLog();
	~SimpleLog();
	void Write(string log);
	void Write(string log, double value);
};

SimpleLog.cpp 

#include "SimpleLog.h"
#include<string.h>
//#include <sstream>std::stringstream ss;
#include <sstream>;

SimpleLog::SimpleLog()
{
	time_t now = time(0);
	localtime_s(&mPCreateFileTime, &now);
	CreateFile();
}

SimpleLog::~SimpleLog()
{
	mOutfile.close();
}

void SimpleLog::Write(string log)
{
	CheckCreate();
	mOutfile << log << endl;
}

void SimpleLog::Write(string log , double value)
{
	CheckCreate();
	ostringstream stream;
	stream << value;
	string str = stream.str();
	mOutfile << log<< str << endl;
}

void SimpleLog::CreateFile()
{
	char* date = new char[20];
	strftime(date, 20, "%F", &mPCreateFileTime);
	string fileName = date;
	mOutfile.open("logs/" + fileName + ".txt", ios::app);

}

void SimpleLog::CheckCreate()
{
	time_t now = time(0);
	tm newTm;
	localtime_s(&newTm, &now);
	if (mPCreateFileTime.tm_year != newTm.tm_year) {
		return;
	}
	if (mPCreateFileTime.tm_mon != newTm.tm_year) {
		return;
	}
	if (mPCreateFileTime.tm_mday != newTm.tm_year) {
		return;
	}
}

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值