OutputDebugString 输出时间

在C++ debug模式下,一些集成开发环境(IDE)也提供了自带的控制台输出窗口来显示程序的运行结果。在Visual Studio中,可以使用OutputDebugString函数将输出信息打印到“输出”窗口。

打印年月日

void debugPrint(int nSendOrder)
{
	auto now = std::chrono::system_clock::now();
	// 转换为时间字符串
	time_t t = std::chrono::system_clock::to_time_t(now);
	char buf[26];
	ctime_s(buf, sizeof buf, &t);
	// 去掉末尾的换行符
	buf[24] = '\0';
	CStringA bufstr(buf);
	CStringA tempstr;
	tempstr.Format("    SendThread : send id %d , send queue size is %d\n", nSendOrder, m_pSendQueue->size());
	bufstr = "---------------------------" + bufstr + tempstr;
	// 输出到调试器
	OutputDebugStringA(bufstr);
}

输出为:

---------------------------Mon Sep 18 09:49:45 2023 SendThread : send id 1 , send queue size is 9

打印时间精确到毫秒

void debugPrint(CStringA tempstr)
{
	auto now = std::chrono::system_clock::now();
	// 转换为时间字符串
	auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
	auto t = std::chrono::system_clock::to_time_t(now_ms);
	std::stringstream ss;
	ss << std::put_time(std::localtime(&t), "%Y-%m-%d %H:%M:%S");
	auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(now_ms.time_since_epoch()) % 1000;
	ss << '.' << std::setfill('0') << std::setw(3) << milliseconds.count();
	std::string buf = ss.str();
	CStringA bufstr(buf.c_str());
	bufstr = "================================== time: " + bufstr + "   " + tempstr + "==================================\n";
	// 输出到调试器
	OutputDebugStringA(bufstr);
}

输出:time: 2023-09-15 11:34:16.532

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值