Windows C ++:如何重定向stderr调用fprintf? [英] Windows C++: How can I redirect stderr for calls to fprintf?

查看:218
本文介绍了Windows C ++:如何重定向stderr调用fprintf?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在自己的自定义包装器中包装来自BSD项目的现有C ++代码,并且希望尽可能少地将它集成到我们的代码中。此代码使用 fprintf 打印到 stderr ,以便记录/报告错误。

I am wrapping existing C++ code from a BSD project in our own custom wrapper and I want to integrate it to our code with as few changes as possible. This code uses fprintf to print to stderr in order to log / report errors.

我想重定向到同一过程中的另一个地方。在Unix上我使用socketpair和线程:socket的一端是我发送 stderr (通过调用 dup2 )而另一端在一个线程中被监视,在那里我可以处理输出。

I want to redirect this to an alternative place within the same process. On Unix I have done this with a socketpair and a thread: one end of the socket is where I send stderr (via a call to dup2) and the other end is monitored in a thread, where I can then process the output.

这不适用于Windows,虽然因为一个套接字不同于一个文件

This does not work on windows though because a socket is not the same as a file handle.

我在网上找到的所有文档都显示了如何重定向子进程的输出,这不是我想要的。如何在同一个进程中重定向 stderr 获得输出时的某种回调? (在你说之前,我试过 SetStdHandle ,但是找不到任何方式使这个工作)...

All documents I have found on the web show how to redirect output from a child process, which is not what I want. How can I redirect stderr within the same process getting a callback of some sort when output is written? (and before you say so, I've tried SetStdHandle but cannot find any way to make this work)...

推荐答案

你可以在Windows上使用类似的技术,你只需要为同样的概念使用不同的单词。 :)本文:http://msdn.microsoft.com/en-us/library/ms682499.aspx使用一个win32管道来处理来自另一个进程的I / O,你只需要在同一个线程中做同样的事情处理。当然,在你的情况下,从过程中的任何地方的所有输出到stderr将被重定向到你的消费者。

You can use a similar technique on Windows, you just need to use different words for the same concepts. :) This article: http://msdn.microsoft.com/en-us/library/ms682499.aspx uses a win32 pipe to handle I/O from another process, you just have to do the same thing with threads within the same process. Of course, in your case all output to stderr from anywhere in the process will be redirected to your consumer.

实际上,你可能需要的其他部分的拼图 fdopen open_osfhandle 。事实上,这里是我几年前发布的一些代码的相关示例:

Actually, other pieces of the puzzle you may need are fdopen and open_osfhandle. In fact, here's a related example from some code I released years ago:

DWORD CALLBACK DoDebugThread(void *)
{
    AllocConsole();
    SetConsoleTitle("Copilot Debugger");
    // The following is a really disgusting hack to make stdin and stdout attach
    // to the newly created console using the MSVC++ libraries. I hope other
    // operating systems don't need this kind of kludge.. :)
    stdout->_file = _open_osfhandle((long)GetStdHandle(STD_OUTPUT_HANDLE), _O_TEXT);
    stdin->_file  = _open_osfhandle((long)GetStdHandle(STD_INPUT_HANDLE), _O_TEXT);
    debug();
    stdout->_file = -1;
    stdin->_file  = -1;
    FreeConsole();
    CPU_run();
    return 0;
}

在这种情况下,主进程是一个GUI进程,与stdio句柄。它打开一个控制台,然后把正确的句柄放入stdout和stdin,所以debug()函数(它被设计为一个stdio交互式函数)可以与新创建的控制台进行交互。你应该能够打开一些管道,并做同样的事情来重定向stderr。

In this case, the main process was a GUI process which doesn't start with stdio handles at all. It opens a console, then shoves the right handles into stdout and stdin so the debug() function (which was designed as a stdio interactive function) can interact with the newly created console. You should be able to open some pipes and do the same sort of thing to redirect stderr.

这篇关于Windows C ++:如何重定向stderr调用fprintf?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆