Qtを使用してGUIアプリケーションを作成しています。このアプリケーションは、エラーメッセージをstderrに送信することがあるサードパーティのDLLにリンクしています。これらのエラーメッセージをGUI内のウィンドウに表示したいのですが。
多くの検索を行った後でも、(std :: cerrではなく)stderrをリダイレクトする確立された方法が見つからなかったため、次のクラスを自分で作成しました。
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からシンボルを見つけることができないため、アプリケーションはコンパイルされません。
ですから、私がここでやろうとしていることには明らかに何か深刻な問題があります。誰か助けてもらえますか?
ありがとう、スティーブン。