0

I want to write the date of the execution and the end of execution of a file in my log file.

I can't install anything, just use standard module ( I execute my code in command line with linux ).

I want something like this : [TRACE] 2014-07-24 14:18:50,2014-07-24 14:18:52

I have this result for the moment :

[TRACE] , Start date of execution : Aug 25 2014 : 10:43:02 End date of execution : Mon Aug 25 10:43:06 2014

here my code :

#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;  
}
shanks
  • 9
  • 1
  • 6
  • What exactly is the question? How to change the format in which the dates are printed..? – Michael Aug 25 '14 at 08:50
  • Firstly I want to know, if that's the date of execution of the file and then, yes I want this format : [TRACE],module_name,2014-07-24 14:18:50,2014-07-24 14:18:52 – shanks Aug 25 '14 at 08:52
  • You need to look at functions like `time` `localtime` and `asctime` or `strftime`. `__DATE__` etc refer to the date and time of compilation. http://en.cppreference.com/w/cpp/chrono/c/time http://en.cppreference.com/w/cpp/chrono/c/strftime – n. m. could be an AI Aug 25 '14 at 08:53
  • 1
    What is `module_name` supposed to be? – Jongware Aug 25 '14 at 08:53
  • @Jongware : for the moment module_name is not important – shanks Aug 25 '14 at 08:56
  • @n.m. could you show me an example please ? Because I'm novice in C++ and example is better than explications with sentences :) – shanks Aug 25 '14 at 08:57
  • Examples are in the updated comment. – n. m. could be an AI Aug 25 '14 at 08:58
  • possible duplicate of [How to format date and time string in C++](http://stackoverflow.com/questions/10289017/how-to-format-date-and-time-string-in-c) – Jongware Aug 25 '14 at 09:00
  • 1
    YEs, so my end date of execution is good, but the start date is not good ? – shanks Aug 25 '14 at 09:00
  • @Jongware : that's not the same thing. – shanks Aug 25 '14 at 09:29
  • Then if it's not "how do I format this time as (sample)", it is unclear to me what your *actual* question is. – Jongware Aug 25 '14 at 09:34
  • @Jongware : I juste want to write in a file, the date of the execution of a file ( stard/end date of execution ), I have this but, I thins my start date is the date of compilation ... and I want to final format like this : [TRACE] 2014-07-24 14:18:50,2014-07-24 14:18:52 – shanks Aug 25 '14 at 09:36

2 Answers2

1

You can use log4cpp library. It has lots of other features too. There are sample programs available on the following website.

http://log4cpp.sourceforge.net/

You just need to instantiate the appender based on the needs. I have used RollingFileAppender in my project where I needed the log file to be divided after some threshold (i.e. file size reaches 1MB). Then you need to set the pattern in which you want the logs to be written.

Hope this helps.

pragnesh
  • 1,240
  • 7
  • 18
  • 1
    The problem, it's I can't to install anything, I write my code in script and I execute it in command line ( Linux ). – shanks Aug 25 '14 at 08:58
  • 1
    Why can't you install anything? The suggested library is simple enough to get what you need. I suggest to you to edit your question in order to let us know better your situation. – Alessandro Suglia Aug 25 '14 at 09:02
  • Because in my office I don't have the right and we can't install anything, just use standard module :( , I want to obtain this format of the execution of my file : [TRACE],module_name,2014-07-24 14:18:50,2014-07-24 14:18:52 – shanks Aug 25 '14 at 09:05
  • Please [do not post an answer that consists essentially of a link](http://stackoverflow.com/questions/how-to-answer). Include the important points in your answer; leave the link for extra information or as a reference. – You Aug 25 '14 at 09:28
1

As many have commented, __DATE__ and __TIME__ refer to the time of compilation, not execution.
You'll need to retrieve the current time both at the start and at the end of the execution; you'll use the same method, whichever one you use, in both cases.

Here's an example of how you can format time using 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;
}

You would use it like this:

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;
}

Read the documentation for strftime to learn how to specify your own format.

molbdnilo
  • 64,751
  • 3
  • 43
  • 82