C++ 如何在C++;

C++ 如何在C++;,c++,trace,logfiles,logfile,C++,Trace,Logfiles,Logfile,我想在日志文件中写入文件的执行日期和执行结束日期 我不能安装任何东西,只能使用标准模块(我在linux的命令行中执行代码) 我想要这样的东西: [TRACE]2014-07-2414:18:502014-07-2414:18:52 目前我有一个结果: [TRACE],开始执行日期:2014年8月25日:10:43:02 执行结束日期:2014年8月25日星期一10:43:06 这是我的代码: #include <iostream> #include <string> #i

我想在日志文件中写入文件的执行日期和执行结束日期

我不能安装任何东西,只能使用标准模块(我在linux的命令行中执行代码)

我想要这样的东西:
[TRACE]2014-07-2414:18:502014-07-2414:18:52

目前我有一个结果:

[TRACE],开始执行日期:2014年8月25日:10:43:02
执行结束日期:2014年8月25日星期一10:43:06

这是我的代码:

#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;  
}
#包括
#包括
#包括
#包括
使用名称空间std;
void startDateExecution(流和文件){
如果(菲希尔)
{

文件您可以使用log4cpp库。它还具有许多其他功能。以下网站提供了示例程序

您只需要根据需要实例化appender。我在项目中使用了RollingFileAppender,其中我需要在某个阈值(即文件大小达到1MB)后分割日志文件。然后您需要设置希望写入日志的模式


希望这能有所帮助。

正如许多人所评论的那样,
\uuuuuuuuuuuuu
\uuuuuuuuuuuuuuuu
指的是编译时间,而不是执行时间。
您将需要在执行的开始和结束时检索当前时间;在这两种情况下,您将使用相同的方法,无论使用哪种方法

下面是一个如何使用
strftime
格式化时间的示例

std::string format(time_t when)
{
    char timestr[256] = {0};
    const char* my_format = "%m/%d/%y @ %H:%M:%S"; 
    std::strftime(timestr, sizeof(timestr), my_format, std::localtime(&when));
    return timestr;
}
您可以这样使用它:

int main()
{
    time_t start = std::time(NULL);
    // Do stuff
    time_t end = std::time(NULL);

   std::cout << "Start: " << format(start) << std::endl
             << "End: "   << format(end)   << std::endl;
}
intmain()
{
时间\u t开始=标准::时间(空);
//做事
时间=标准::时间(空);

std::cout问题到底是什么?如何更改打印日期的格式..?首先,我想知道这是否是文件的执行日期,然后,是的,我想要这种格式:[跟踪],module_name,2014-07-24 14:18:502014-07-24 14:18:52您需要查看
time
localtime
asctime
strftime
日期
等函数指的是编译的日期和时间。
module_name
应该是什么?是的,所以我的执行结束日期很好,但是art date不好?问题是,我不能安装任何东西,我用脚本编写代码,然后在命令行(Linux)中执行。为什么你不能安装任何东西?建议的库非常简单,可以满足你的需要。我建议你编辑你的问题,以便让我们更好地了解你的情况。因为在我的办公室,我没有权限,我们无法安装任何东西,只需使用标准模块:(,我想获得我的文件执行的这种格式:[跟踪],module_name,2014-07-24 14:18:502014-07-24 14:18:52请在您的答案中包含要点;留下链接以获取更多信息或作为参考。您能给我看一下我的代码吗,因为我试图使用您的代码,但我有错误。