C++实现log记录

1.需要在头文件中定义log对象以及控件ID

const int       IDC_LISTBOX_LOG = 12345;
CListCtrl       m_logList;

2.在初始化代码中添加CListCtrl控件的初始化代码

//获取当前坐标
    CRect rcClient;
	GetClientRect(&rcClient);
	//设置log控件位置
	CRect rcListBox;
	rcListBox.left = rcClient.left ;
	rcListBox.top = rcClient.top;
	rcListBox.bottom = rcClient.bottom ;
	CRect rcScreen;
	SystemParametersInfo(SPI_GETWORKAREA, 0, &rcScreen, 0);
	rcListBox.right = rcListBox.left + 600.0f * rcScreen.Width()/1920;
	//在指定位置创建log控件
	m_logList.Create(WS_CHILD | WS_VISIBLE | WS_BORDER | LVS_ALIGNLEFT, rcListBox, this,     IDC_LISTBOX_LOG);
	LONG lStyle;
	m_logList.SetBkColor(RGB(200, 200, 200));
	lStyle = GetWindowLong(m_logList.m_hWnd, GWL_STYLE);
	lStyle &= ~LVS_TYPEMASK;
	lStyle |= LVS_REPORT;
	SetWindowLong(m_logList.m_hWnd, GWL_STYLE, lStyle);
	m_logList.ShowWindow(SW_SHOW);
	DWORD dwStyle = m_logList.GetExtendedStyle();
	dwStyle |= LVS_EX_FULLROWSELECT;
	dwStyle |= LVS_EX_GRIDLINES;
	dwStyle |= LVS_EX_INFOTIP;
	//dwStyle |= LVS_EX_TRANSPARENTBKGND;
	m_logList.SetExtendedStyle(dwStyle);

	m_logList.EnableWindow(TRUE);
	CString strItem;
	m_logList.InsertColumn(0, L"时间", LVCFMT_LEFT, 95 * rcScreen.Width() / 1920);
	m_logList.InsertColumn(1, L"内容", LVCFMT_LEFT,500 * rcScreen.Width() / 1920);
	m_logList.EnableWindow(TRUE);
	m_logList.Invalidate();
	m_logList.ShowWindow(SW_SHOW);
	m_logList.BringWindowToTop();

3.接着在使用的时候,直接对于该控件插入对象

    SYSTEMTIME dt;
	::GetLocalTime(&dt);
	CString sTimestamp;
	sTimestamp.Format(_T("%02d:%02d:%02d:%03d"), dt.wHour, dt.wMinute, dt.wSecond, dt.wMilliseconds);
	//log产生时间
	m_logList.InsertItem(0, L"");
	m_logList.SetItemText(0, 0, sTimestamp);
    //log内容
	CString cstr2(L"Button1");
	m_logList.SetItemText(0, 1, cstr2);
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在Qt中,可以使用QLoggingCategory和QtMessageHandler类来实现日志记录功能。 首先,需要定义一个QLoggingCategory对象来表示日志类别,可以在全局范围内定义或在需要的地方定义。例如: ```c++ Q_LOGGING_CATEGORY(myLog, "my.category") ``` 然后,需要定义一个自定义的QtMessageHandler函数来处理日志消息,并将其设置为Qt的全局消息处理程序。例如: ```c++ void myMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) { QByteArray localMsg = message.toLocal8Bit(); switch (type) { case QtDebugMsg: fprintf(stderr, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtInfoMsg: fprintf(stderr, "Info: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtWarningMsg: fprintf(stderr, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtCriticalMsg: fprintf(stderr, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); break; case QtFatalMsg: fprintf(stderr, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), context.file, context.line, context.function); abort(); } } ``` 最后,在程序的初始化阶段,将自定义的QtMessageHandler函数设置为Qt的全局消息处理程序,并启用日志记录。例如: ```c++ qInstallMessageHandler(myMessageHandler); qSetMessagePattern("[%{category}] %{message}"); qDebug() << "Application started."; ``` 以上代码将Qt的全局消息处理程序设置为自定义的myMessageHandler函数,并设置日志记录格式为"[category] message",然后使用qDebug()宏记录一条日志消息。 当需要记录日志时,可以使用QLoggingCategory对象的各种宏来记录不同级别的日志消息。例如: ```c++ qCDebug(myLog) << "Debug message"; qCInfo(myLog) << "Info message"; qCWarning(myLog) << "Warning message"; qCCritical(myLog) << "Critical message"; ``` 以上代码将分别记录调试、信息、警告和严重错误日志消息,其中myLog是之前定义的QLoggingCategory对象。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值