【发布时间】:2014-10-18 09:08:01
【问题描述】:
我想在我的日志文件中写入文件的执行日期和执行结束时间。
我不能安装任何东西,只能使用标准模块(我在 linux 命令行中执行我的代码)。
我想要这样的东西:
[TRACE] 2014-07-24 14:18:50,2014-07-24 14:18:52
我暂时有这个结果:
[TRACE] , Start date of execution : Aug 25 2014 : 10:43:02
End date of execution : Mon Aug 25 10:43:06 2014
这里是我的代码:
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
using namespace std;
void startDateExecution(fstream& file) {
if(fichier)
{
file << "[TRACE]" << " , " << "Start date of execution : " << __DATE__ << " : " << __TIME__ << endl;
}
else
cerr << "Unable to open file" << endl;
}
void endDateExecution(fstream& file) {
time_t result = time(NULL);
file << "End date of execution : " << asctime(localtime(&result)) << endl;
file.close();
}
void displayDate(fstream& file) {
startDateExecution(file);
endDateExecution(file);
}
int main(){
fstream file("trace.log", ios::out | ios::trunc);
displayDate(file);
return 0;
}
【问题讨论】:
-
问题到底是什么?如何更改打印日期的格式..?
-
首先我想知道,如果那是文件的执行日期,然后,是的,我想要这种格式:[TRACE],module_name,2014-07-24 14:18:50,2014 -07-24 14:18:52
-
您需要查看
timelocaltime和asctime或strftime等函数。__DATE__等是指编译的日期和时间。 en.cppreference.com/w/cpp/chrono/c/timeen.cppreference.com/w/cpp/chrono/c/strftime -
module_name应该是什么? -
是的,所以我的执行结束日期是好的,但开始日期不好?
标签: c++ trace logfiles logfile