3

我正在使用 Qt 编写一个 GUI 应用程序,它链接到有时会向 stderr 发送错误消息的第三方 DLL。我希望这些错误消息显示在我的 GUI 的窗口中。

即使经过大量搜索,我也找不到一种既定的方式来重定向 stderr(而不是 std::cerr),所以我自己编写了以下类:

class StdErrRedirect : public QObject
{
    Q_OBJECT

public:
    // Constructor
    StdErrRedirect(QTextEdit *errorLog,
                   QObject   *parent = NULL);

    // Destructor
    ~StdErrRedirect();

private slots:
    void fileChanged(const QString &filename);

private:    
    QFile              tmp;
    QFileSystemWatcher watcher;
    QString            tmpFileNameQtFormat;
    QString            tmpFileNameNativeFormat;

    QTextEdit *m_errorLog;
    QString   oldContent;
};

StdErrRedirect::StdErrRedirect(QTextEdit *errorLog,
                               QObject   *parent)
    : QObject(parent)
{
    // Store the pointer to the error log window
    m_errorLog = errorLog;

    // Create a temporary filename: first find the path:
    tmpFileNameQtFormat = QDir::tempPath();

    // Make sure the closing slash is present:
    if (!tmpFileNameQtFormat.endsWith(QChar('/')))
        tmpFileNameQtFormat.append(QChar('/'));

    // Add the file name itself:
    tmpFileNameQtFormat.append("nb_stderrlog");

    // Obtain a version of the filename in the operating system's native format:
    tmpFileNameNativeFormat = QDir::toNativeSeparators(tmpFileNameQtFormat);

    // Set up redirection to this file:
    freopen(tmpFileNameNativeFormat.toAscii().constData(), "a+", stderr);

    // Initialise the QFileSystemWatcher:
    connect(&watcher, SIGNAL(fileChanged(const QString &)),
            this,     SLOT(fileChanged(const QString &)));
    watcher.addPath(tmpFileNameQtFormat);

    tmp.setFileName(tmpFileNameQtFormat);
}

StdErrRedirect::~StdErrRedirect()
{
    // Ensure the temporary file is properly deleted:
    fclose(stderr);
    tmp.close();
    tmp.open(QIODevice::ReadWrite);
    tmp.remove();
}

void StdErrRedirect::fileChanged(const QString &filename)
{
    tmp.open(QIODevice::ReadOnly);
    QTextStream stream(&tmp);
    QString content = stream.readAll();
    tmp.close();

    // Identify what's new, and just send this to the window:
    int newchars = content.size() - oldContent.size();
    if (newchars)
    {
        m_errorLog -> append(content.right(newchars));
        oldContent = content;
    }
}

如果我使用以下方法从我的主窗口实例化它:

errorLog = new QTextEdit;
redirector = new StdErrRedirect(errorLog);

...然后我写到 stderr 的所有内容都会出现在窗口中。

到现在为止还挺好。问题是 DLL 的输出仍然没有。在调用发出错误的 DLL 函数时,如果我输入代码:

if (error != _OK)
{
    error.PrintErrorTrace();
    fprintf(stderr, "Should have printed an error \r\n");
    fflush(stderr);
    //fsync(_fileno(stderr));    Linux version
    _commit(_fileno(stderr));
    return;
}

...然后出现“应该打印错误”文本,但错误消息本身没有。

现在,我在某处读到这可能是因为在应用程序开始时加载 DLL正在设置重定向,因此它自己的 stderr 通道不受影响。因此,我应该能够通过在设置重定向之后动态加载 DLL 来解决此问题。

那么,这是我的问题:我该怎么做?我可以尝试将以下代码放在我的应用程序的开头:

QLibrary extlib;
extlib.setFileName("libname");
extlib.setLoadHints(QLibrary::ResolveAllSymbolsHint);
extlib.load();

...但它本身没有效果。我认为这是因为链接器仍在将库设置为自动打开。但是,如果我从链接器中删除 DLL(我使用的是 VS2008,所以我从依赖项列表中删除了 extlib.lib),那么应用程序将无法编译,因为编译器无法从 DLL 中找到符号。

所以我在这里尝试做的事情显然有很大的错误。有人可以帮忙吗?

谢谢,斯蒂芬。

4

2 回答 2

2

DLL 真的写入stderr吗?还是写信给GetStdHandle(STD_ERROR_HANDLE)? 第一个映射到第二个,最初。但是,freopen()您只需更改映射即可。写入的任何内容STD_ERROR_HANDLE仍然会去那里。

要重定向每个人的错误输出,您需要SetStdHandle.

于 2010-07-08T13:18:02.707 回答
0

只有一个标准错误,所以我的猜测是问题在于您需要动态加载 dll,而在于您的重定向代码中的某处。您的重定向代码采用 Linux 风格编写,在 Windows 中的工作方式有所不同。

如果您可以在 Linux 上测试您的应用程序,这将有助于查明问题。如果它在 Linux 上运行,那它就是重定向代码。

无论如何,您应该阅读有关重定向和 windows的更多信息,因为我认为您现在尝试做的事情不会对您有所帮助。

于 2010-07-09T06:40:19.990 回答