C++ 在C+中重用strftime+;

C++ 在C+中重用strftime+;,c++,strftime,C++,Strftime,我正在编写一个程序,需要将当前日期和时间写入日志文件,尽管代码正常工作,但仍有大量重复代码。代码是 #include<iostream> #include<fstream> #include<unistd.h> using namespace std; string logFile="/home/shared/c++/time.log"; char timeBuffer[80]; int main() { struct tm * timeInf

我正在编写一个程序,需要将当前日期和时间写入日志文件,尽管代码正常工作,但仍有大量重复代码。代码是

#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;
                }
        }
}
在包含大括号的情况下,它编译并运行时不会出现任何问题

写入日志文件的信息会有所不同,因此需要与时间分开

因为我对C++是新的,我觉得我已经把问题复杂化了,所以任何指导都会被理解的。 我正在运行CentOS 7和g++(GCC)4.8.5 20150623(Red Hat 4.8.5-11)

问候 琥珀玛丽

更新 谢谢你的帮助。完整的代码现在是:

#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);
}
#包括
#包括
#包括
std::string logFile=“/home/shared/c++/time.log”;
字符时间缓冲区[80];
void getTime(std::ofstream&vLog,const std::string&format_args,const std::string&message)
{
结构tm*时间信息;
时间与时间;
时间(&rawtime);
timeInfo=localtime(&rawtime);
strftime(timeBuffer,sizeof(timeBuffer),format_args.c_str(),timeInfo);
std::字符串timeNow(timeBuffer);

std::cout问题在于,您正在重新定义变量“timeNow”,并调用其构造函数为其提供“timeBuffer”值。这与括号一起工作,因为每个括号都是一个新的作用域。“timeNow”的每个重新声明对于它的所有其他声明都是不可见的,因此编译器不会产生混淆。要消除这种情况,请使用赋值而不是尝试重新声明变量。使用
timeNow=timeBuffer

而不是

string timeNow(timeBuffer);
我尝试了这段代码,它在我这方面使用g++工作:

 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);
                        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);
                        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);
                        timeNow = timeBuffer;
                        cout << timeNow << " - End of log." << endl;
                        vLog << timeNow << " - End of log." << endl;
}
intmain()
{
结构tm*时间信息;
时间与时间;
流vLog(logFile.c_str(),ios_base::app | ios_base::out);
时间(&rawtime);
timeInfo=localtime(&rawtime);
strftime(timeBuffer,sizeof(timeBuffer),“%A%d%b%Y%r%Z”,timeInfo);
字符串timeNow(timeBuffer);

cout编写一个函数来包装代码。参数应该是流、strftime格式参数和日志消息(“-1st line of code”,…)

void foo(流和vLog、常量字符串和格式参数、常量字符串和消息)
{
结构tm*时间信息;
时间与时间;
时间(&rawtime);
timeInfo=localtime(&rawtime);
strftime(timeBuffer,sizeof(timeBuffer),format_args.c_str(),timeInfo);
字符串timeNow(timeBuffer);
是的,这是可能的:)

让我们首先考虑整个时间业务的获取和格式化。重复代码中唯一的变量是格式字符串,因此让我们将其作为参数:

std::string formattedCurrentTime(char const *const format) {
    std::time_t const rawTime = std::time(nullptr);
    std::tm const *const timeInfo = std::localtime(&rawTime);

    char buffer[80];
    std::size_t const length = std::strftime(buffer, sizeof buffer, format, timeInfo);

    return {buffer, buffer + length};
}

然后我们可以看看日志行的重复。由于整个
操作符,一般的解决方案有点难以实现。我已经按照您的建议更改了代码,它对我也很有效,所以这是一个很好的改进。有没有办法根据格式使用一次或两次时间代码,然后使用它来编写到日志?谢谢,由于发送strftime变量和日志消息,它使代码更易于阅读,并且非常灵活
void foo(ofstream &vLog, const string &format_args, const string &message)
{
    struct tm * timeInfo;
    time_t rawtime;

    time (&rawtime);
    timeInfo = localtime(&rawtime);
    strftime(timeBuffer,sizeof(timeBuffer),format_args.c_str(),timeInfo);
    string timeNow(timeBuffer);
    cout << timeNow << message << endl;
    vLog<< timeNow << message << endl;
}

int main()
{
    ofstream vLog(logFile.c_str(), ios_base::app | ios_base::out);

    foo(vLog, "%A %d %b %Y %r %Z", " - 1st Line of log.");
    // Do a part of the code
    foo(/* second call */);
    // Other stuff
    foo(/* 3rd call */);
    // ...

    return 0;
}
std::string formattedCurrentTime(char const *const format) {
    std::time_t const rawTime = std::time(nullptr);
    std::tm const *const timeInfo = std::localtime(&rawTime);

    char buffer[80];
    std::size_t const length = std::strftime(buffer, sizeof buffer, format, timeInfo);

    return {buffer, buffer + length};
}
auto const logBookend = [&](std::string const &message) {
    auto const timeNow = formattedCurrentTime("%A %d %b %Y %r %Z");

    std::cout << timeNow << " - " << message << std::endl;
    vLog      << timeNow << " - " << message << std::endl;
};

auto const logLine = [&](std::string const &message) {
    auto const timeNow = formattedCurrentTime("%r");

    std::cout << timeNow << " - " << message << std::endl;
    vLog      << timeNow << " - " << message << std::endl;
};