c++ 日志模块

#include <iostream>
#include <string>
#include <fstream>
#include <time.h>
#include <sys/time.h>
#include <unistd.h>

using namespace std;
int main()
{

	ofstream  ofs("a.log");
	time_t tm = time(0);
	struct tm* t1 = localtime(&tm);

	//int year = t1->tm_year + 1900;
	//int mon = t1->tm_mon;
	//int day = t1->tm_mday;
	int hour = t1->tm_hour;
	int min = t1->tm_min;
	int sec = t1->tm_sec;

	int pid = getpid();
	timeval tv;
	gettimeofday(&tv, 0);

	//char str[] = { 0 };
	string log = "[";
	//sprintf(str, "%d|%02d:%02d:%02d:%ld|%s|%04d|	%s]", pid, hour, min, sec, tv.tv_usec, __FILE__, __LINE__, "warning");
	log += std::to_string(pid) + "|" + std::to_string(hour) + ":" + std::to_string(min) + ":" + std::to_string(sec) + ":"
		+ std::to_string(tv.tv_usec) + "|" + string(__FILE__) + "|" + std::to_string(__LINE__) + "|	" + "warning]";

	//log += string(str);
	log += "	this is a test....";
	ofs << log << endl;
	ofs << log << endl;
	ofs << log << endl;
	ofs << log << endl;
	ofs << log << endl;
	ofs << log << endl;

	return 0;

}

 

 

或:

2024-59-01 18:59:20.806 eventType 8000
2024-59-01 18:59:20.807 dbcc_devicetype 5

 

posted @ 2024-11-05 19:59  微笑的''80  阅读(10)  评论(0)    收藏  举报