Ecrire les dates d'executions d'un fichier dans un fichier log
Bonjour à tous,
j'essaie d'écrire les dates d'execution du fichier ( date debut et date de fin d'execution ) dans mon fichier.
J'aimerai avoir ce format : [TRACE] 2014-07-24 14:18:50,2014-07-24 14:18:52
mais pour le moment, voici mon résultat : [TRACE] , Start date of execution : Aug 25 2014 : 10:43:02 End date of execution : Mon Aug 25 10:43:06 2014
De plus, j'utilise __TIME__ et __DATE__ pour récupérer la date de début mais je pense que c'est la date de début .... mais de compilation et non d'execution :(
Quelqu'un peut-il me donner un coup de main ?
Voici mon code :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
|
#include <iostream>
#include <string>
#include <fstream>
#include <ctime>
using namespace std;
void startDateExecution(fstream& fichier) {
if(fichier)
{
fichier << "[TRACE STUC]" << " , " << "Start date of execution : " << __DATE__ << " : " << __TIME__ << endl;
}
else
cerr << "Impossible d'ouvrir le fichier" << endl;
}
void endDateExecution(fstream& fichier) {
time_t result = time(NULL);
fichier << "End date of execution : " << asctime(localtime(&result)) << endl;
fichier.close();
}
void displayDate(fstream& fichier) {
startDateExecution(fichier);
endDateExecution(fichier);
}
int main(){
fstream fichier("trace.log", ios::out | ios::trunc);
displayDate(fichier);
return 0;
} |
En parcourant le web j'ai vu comme qu'on peut utiliser strftime ( mais je ne sais pas comment et surtout je ne sais pas comment intégrer ce code ci-dessous au mien ) :roll:
Code:
1 2 3 4 5 6 7 8 9
|
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;
} |
... et son implementation :
Code:
1 2 3 4 5 6 7 8 9 10 11
|
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;
} |