Ich schreibe ein Programm, das Schreiben des aktuellen Datums und der Uhrzeit in eine Protokolldatei erfordert, obwohl der Code funktioniert, gibt es eine Menge Code zu wiederholen. Der Code istWiederverwendung strftime in C++
#include<iostream>
#include<fstream>
#include<unistd.h>
using namespace std;
string logFile="/home/shared/c++/time.log";
char timeBuffer[80];
int main()
{
struct tm * timeInfo;
time_t rawtime;
ofstream vLog(logFile.c_str(), ios_base::app | ios_base::out);
{
{
time (&rawtime);
timeInfo = localtime(&rawtime);
strftime(timeBuffer,sizeof(timeBuffer),"%A %d %b %Y %r %Z",timeInfo);
string timeNow(timeBuffer);
cout << timeNow << " - Start of log." << endl;
vLog << timeNow << " - Start of log." << endl;
}
// Do a part of the code
{
time (&rawtime);
timeInfo = localtime(&rawtime);
strftime(timeBuffer,sizeof(timeBuffer),"%r",timeInfo);
string timeNow(timeBuffer);
cout << " " << timeNow << " - 1st Line of log." << endl;
vLog << " " << timeNow << " - 1st Line of log." << endl;
}
// Do more code
{
time (&rawtime);
timeInfo = localtime(&rawtime);
strftime(timeBuffer,sizeof(timeBuffer),"%r",timeInfo);
string timeNow(timeBuffer);
cout << " " << timeNow << " - 2nd Line of log." << endl;
vLog << " " << timeNow << " - 2nd Line of log." << endl;
}
// Do the last part of the code
{
time (&rawtime);
timeInfo = localtime(&rawtime);
strftime(timeBuffer,sizeof(timeBuffer),"%A %d %b %Y %r %Z",timeInfo);
string timeNow(timeBuffer);
cout << timeNow << " - End of log." << endl;
vLog << timeNow << " - End of log." << endl;
}
}
}
Ich habe zählen die {} auf jedem Abschnitt so, dass die Variablen nur für den einen bestimmten Codeblock verwendet werden. Wenn ich sie nicht benutze, bekomme ich Fehler beim Kompilieren.
time.cpp: In function "int main()":
time.cpp:54:29: error: redeclaration of "std::string timeNow"
string timeNow(timeBuffer);
^
time.cpp:45:11: error: "std::string timeNow" previously declared here
string timeNow(timeBuffer);
^
Mit den geschweiften Klammern, dann kompiliert und läuft ohne Probleme.
Die Informationen, die in die Protokolldatei geschrieben werden, sind unterschiedlich und müssen daher von der Uhrzeit getrennt sein.
Da ich neu in C++ bin, habe ich das Gefühl, das Problem zu verkomplizieren, so dass jede Anleitung geschätzt würde. Ich bin mit CentOS 7 und g ++ (GCC) 4.8.5 20.150.623 (Red Hat 4.8.5-11)
Grüße Bernstein-Marie
aktualisieren
Vielen Dank für die Hilfe. Der vollständige Code lautet jetzt:
#include<iostream>
#include<fstream>
#include<unistd.h>
std::string logFile="/home/shared/c++/time.log";
char timeBuffer[80];
void getTime(std::ofstream &vLog, const std::string &format_args, const std::string &message)
{
struct tm * timeInfo;
time_t rawtime;
time (&rawtime);
timeInfo = localtime(&rawtime);
strftime(timeBuffer,sizeof(timeBuffer),format_args.c_str(),timeInfo);
std::string timeNow(timeBuffer);
std::cout << timeNow << message << std::endl;
vLog<< timeNow << message << std::endl;
}
int main()
{
std::ofstream vLog(logFile.c_str(), std::ios_base::app | std::ios_base::out);
getTime(vLog, "%A %d %b %Y %r %Z", " - Start of logging");
// Do part of the code
getTime(vLog, " %r", " - 1st line of log");
// Do more code
getTime(vLog, " %r", " - 2nd line of log");
// Do the last part of the code
getTime(vLog, "%A %d %b %Y %r %Z", " - End of logging");
vLog << std::endl;
return (0);
}
Hoffentlich werden andere dies hilfreich finden.
Bernstein-Marie
Vielen Dank dafür, es hat den Code viel einfacher zu lesen und sehr flexibel aufgrund des Sendens der strftime Variablen und der Protokollnachricht gemacht –