windows添加debug信息,并存储txt文件

.h

#ifndef FILE_UTIL_H
#define FILE_UTIL_H

#include <string>

void donghaodebug(const std::string& content);

#endif // FILE_UTIL_H

.cpp

#include "donghaodebug.h"
#include <fstream>
#include<ctime>

void donghaodebug(const std::string& content) {

    std::string filename="./donghaodebug.txt";
    std::ofstream file(filename, std::ios::app);
    static bool firstcall=true;


    if (file.is_open()) {
        if(firstcall)
        {
            firstcall=false;

            time_t currentTime = time(nullptr);
            struct tm* localTime = localtime(&currentTime);
            char timeStr[20];
            strftime(timeStr, sizeof(timeStr), "%Y-%m-%d %H:%M:%S", localTime);


            file << "-------------------------新debug信息-----------------------------" << std::endl;
            file << "时间: " << timeStr << std::endl;


        }

        file << content << std::endl;
        file.close();
    } else {
        std::ofstream newFile(filename);
        newFile << content << std::endl;
        newFile.close();
    }
}

posted @ 2024-05-28 13:32  东岸  阅读(22)  评论(0)    收藏  举报