我想咨询一下如何在日志消息中添加时间戳并将信息保存到文本文件中。例如,如果我打开设备,输出将显示如下:
[16/03/2021 3:16:00] 设备开启
谢谢!
答案 0 :(得分:1)
这个怎么样:
auto currentTime = std::chrono::system_clock::now(); // get the time
auto formattedTime = std::chrono::system_clock::to_time_t(currentTime); // convert it to time_t type (loses some precision)
std::cout << std::ctime(&formattedTime) << std::endl; // print it formatted