将日期和时间添加到 C++ 中的日志消息

时间:2021-03-16 07:18:29

标签: c++ timestamp

我想咨询一下如何在日志消息中添加时间戳并将信息保存到文本文件中。例如,如果我打开设备,输出将显示如下:

[16/03/2021 3:16:00] 设备开启

谢谢!

1 个答案:

答案 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