VTK 錯誤消息不彈窗,輸出到日誌

#include "mainwindow.h"
#include <vtkFileOutputWindow.h>
#include <vtkSmartPointer.h>
#include <QApplication>
#include <QFile>
#include <QTextStream>
#include <QMutex>
#include <QDateTime>
#include <QTranslator>
#include <QDebug>

#include <iostream>     // std::cout
#include <cstdlib>      // std::exit
#include <new>          // std::set_new_handler
void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
    static QMutex mutex;
    mutex.lock();
    QDateTime time = QDateTime::currentDateTime();
    QString msgs = QString("%1 %2 (%3) %4")
        .arg(time.toString("yyyy-MM-dd hh:mm:ss"))
        .arg(context.file)
        .arg(context.line)
        .arg(msg);

    QString logPath = QString("%1/log/%2.txt").arg(QCoreApplication::applicationDirPath()).arg(time.toString("yyyy-MM-dd"));
    QFile file(logPath);
    file.open(QIODevice::WriteOnly | QIODevice::Append);
    QTextStream out(&file);
    out.setCodec("UTF-8");
    out << msgs << "\r\n";
    file.flush();
    file.close();
    mutex.unlock();
}

void no_memory () {
  std::cout << "Failed to allocate memory!\n";
  std::exit (1);
}
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    qInstallMessageHandler(outputMessage);
    QString logPath = QString("%1/log/Error.txt").arg(QCoreApplication::applicationDirPath());
    vtkSmartPointer<vtkFileOutputWindow> fileOutputWindow = vtkSmartPointer<vtkFileOutputWindow>::New();
    fileOutputWindow->SetFileName(qPrintable(logPath));
    vtkOutputWindow::SetInstance(fileOutputWindow);
    std::set_new_handler(no_memory);
    MainWindow w;
    w.show();

    return a.exec();
}

VTK解決提示錯誤消息的方式

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章