2019-4-20 qDebug重定向以及线程的暂停问题

1.qDebug重定向

void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QMutex mutex;
mutex.lock();

QString text;
switch((int)type)
{
case QtDebugMsg:
    text = QString("Debug:");
    break;

case QtWarningMsg:
    text = QString("Warning:");
    break;

case QtCriticalMsg:
    text = QString("Critical:");
    break;

case QtFatalMsg:
    text = QString("Fatal:");
    break;
case QtInfoMsg:
    text = QString("Info:");
    break;
default:
    break;
}

QString context_info = QString("File:(%1) Line:(%2)").arg(QString(context.file)).arg(context.line);
QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
QString current_date = QString("(%1)").arg(current_date_time);
//QString message = QString("%1 %2 %3 %4").arg(text).arg(context_info).arg(msg).arg(current_date);
QString message = QString("%1 %2").arg(text).arg(context_info);
message.append(msg);// .arg(msg).arg(current_date);
message.append(current_date);

QFile file("log_update.txt");
file.open(QIODevice::WriteOnly | QIODevice::Append);
QTextStream text_stream(&file);
text_stream << message << "\r\n";
file.flush();
file.close();

mutex.unlock();

}

int main(int argc, char *argv[])
{

qInstallMessageHandler(outputMessage);

QApplication a(argc, argv);
dealogin w;
w.show();

return a.exec();

}

release版本下Pro下要加DEFINES += QT_MESSAGELOGCONTEXT,否则输出重定向中没有文件名和行号。

2.QT线程终止不要用terminate方法 (线程暂停与重启)
Warning: This function is dangerous and its use is discouraged. The thread can be terminated at any point in its code path. Threads can be terminated while modifying data. There is no chance for the thread to clean up after itself, unlock any held mutexes, etc. In short, use this function only if absolutely necessary.

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值