成品日志函数

1、可以选择显示时间、日期时间、不显示时间和日期

2、要养成使用define简化代码和当开关的习惯

cpp 复制代码
#include <iostream>
#include <fstream>
#include <chrono>
#include <ctime>
#include <cstdarg>
#include <sstream>
#include <thread>

#define LOG_TO_CONSOLE_AND_FILE//时间
//#define LOG_TO_CONSOLE_AND_FILE_WITH_DATE//日期 时间
//都注释掉 就不显示时间和日期了

#ifdef LOG_TO_CONSOLE_AND_FILE_WITH_DATE
#define LOG(format, ...) LogToConsoleAndFile(std::to_string(index) + windowName + ".txt", true,  format, __VA_ARGS__)
#elif defined(LOG_TO_CONSOLE_AND_FILE)
#define LOG(format, ...) LogToConsoleAndFile(std::to_string(index) + windowName + ".txt", false, format, __VA_ARGS__)
#else
#define LOG(format, ...) LogToConsole(format, __VA_ARGS__)
#endif

void LogToConsole(const char* format, ...)
{
    va_list args;
    va_start(args, format);
    vprintf(format, args);
    va_end(args);
}

void LogToConsoleAndFile(const std::string& filename, bool includeDate, const char* format, ...)
{
    // 打开文件以追加写入日志
    std::ofstream file(filename, std::ios::app);
    if (!file.is_open())
    {
        std::cerr << "无法打开日志文件:" << filename << std::endl;
        return;
    }
    // 获取时间戳
    
    auto now = std::chrono::system_clock::now();
    std::time_t currentTime = std::chrono::system_clock::to_time_t(now);
    std::tm timeInfo;
#ifdef _WIN32
    localtime_s(&timeInfo, &currentTime);
#else
    localtime_r(&currentTime, &timeInfo);
#endif
    char timeBuffer[128];
    if (includeDate)
    {
        //std::strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %T", &timeInfo);//%T ==%H:%M:%S
        std::strftime(timeBuffer, sizeof(timeBuffer), "%Y-%m-%d %H:%M:%S", &timeInfo);
    }
    else
    {
        //std::strftime(timeBuffer, sizeof(timeBuffer), "%T", &timeInfo);
        std::strftime(timeBuffer, sizeof(timeBuffer), "%H:%M:%S", &timeInfo);
    }

    // 格式化日志消息
    va_list args;
    va_start(args, format);
    char buffer[256];
    vsnprintf(buffer, sizeof(buffer), format, args);
    va_end(args);

    // 在控制台和文件中输出日志消息
    std::printf("%s %s", timeBuffer, buffer);
    file << timeBuffer << " " << buffer;
}

void ThreadFunction(int index, const std::string& windowName)
{
    // 设置窗口名字
    std::string filename = std::to_string(index) + windowName;
    long x1{3243}, y1{423}, x2{42}, y2{-55};
    // 在不同线程中写入日志
    LOG("---%d行------窗口:%s-----------点击羽毛,坐标%d,%d,%d,%d--------\n", 23, filename.c_str(), x1, y1, x2, y2);
    //甚至简化的只用一个字母
    //l("---%d行------窗口:%s-----------点击羽毛,坐标%d,%d,%d,%d--------\n", 23, filename.c_str(), x1, y1, x2, y2);
}

int main()
{
    std::thread thread1(ThreadFunction, 0, "窗口1");
    std::thread thread2(ThreadFunction, 100, "窗口2");

    thread1.join();
    thread2.join();

    return 0;
}
相关推荐
还债大湿兄2 小时前
《C++内存泄漏8大战场:Qt/MFC实战详解 + 面试高频陷阱破解》
c++·qt·mfc
珊瑚里的鱼5 小时前
LeetCode 692题解 | 前K个高频单词
开发语言·c++·算法·leetcode·职场和发展·学习方法
AI+程序员在路上5 小时前
QTextCodec的功能及其在Qt5及Qt6中的演变
开发语言·c++·qt
Risehuxyc5 小时前
C++卸载了会影响电脑正常使用吗?解析C++运行库的作用与卸载后果
开发语言·c++
景彡先生8 小时前
C++编译期计算:常量表达式(constexpr)全解析
服务器·c++
tan77º9 小时前
【Linux网络编程】应用层自定义协议与序列化
linux·运维·服务器·网络·c++·tcp/ip
悠哉清闲9 小时前
Android Studio C++/JNI/Kotlin 示例 三
c++·kotlin·android studio
AI迅剑10 小时前
模块三:现代C++工程实践(4篇)第二篇《性能调优:Profile驱动优化与汇编级分析》
汇编·c++
byte轻骑兵11 小时前
【Bluedroid】蓝牙协议栈enable流程深度解析
android·c++·bluedroid
Jooolin12 小时前
【C++】: list介绍以及模拟实现
c++·ai编程·编程语言