Visual Studio 2012-嵌入式输出控制台而不是cmd? [英] Visual Studio 2012 - Embedded output console instead of cmd?
问题描述
是否可以将嵌入式输出控制台作为VS2012窗口的一部分,而不是在运行时打开cmd?
Is it possible to have an embedded output console as part of the VS2012 window instead of opening cmd upon running?
例如,在Eclipse中,默认情况下将输出定向到控制台"窗格,如果有的话,我想在VS2012中实现类似的功能.
For example, in Eclipse output is directed to the "Console" pane by default, and I would like to achieve something similar in VS2012, if it is available.
推荐答案
以下是执行此操作的完整代码示例:
Here is a complete example of the code involved to do this:
http://blog.tomaka17.com /2011/07/redirectingcerr-and-clog-to-outputdebugstring/
基本上,作者创建了一个使用MSVC函数OutputDebugString()
的新std::basic_stringbuf
,并将其通过std::cerr.rdbuf(&newStreamBuf)
绑定到clog
和cerr
.这对于cout
也应适用.
Basically the author creates a new std::basic_stringbuf
that uses the MSVC function OutputDebugString()
and binds it to clog
and cerr
via std::cerr.rdbuf(&newStreamBuf)
. This should also work for cout
.
我实际上已经编译了他的代码,并且有一些问题,这是更正后的版本:
I actually compiled his code and had some issues with it, here is the corrected version:
cpp文件:
// redirectStreamBuf.cpp
//Feel free to modify target options here (for example #ifndef NDEBUG)
#ifdef _WIN32
#define ENABLE_MSVC_OUTPUT
#endif
#include <iostream>
#include <vector>
#include <sstream>
#ifdef ENABLE_MSVC_OUTPUT
#include <Windows.h>
#endif
#ifdef ENABLE_MSVC_OUTPUT
template<typename TChar, typename TTraits = std::char_traits<TChar>>
class OutputDebugStringBuf : public std::basic_stringbuf<TChar,TTraits> {
public:
typedef std::basic_stringbuf<TChar, TTraits> BaseClass;
explicit OutputDebugStringBuf() : _buffer(256) {
setg(nullptr, nullptr, nullptr);
setp(_buffer.data(), _buffer.data(), _buffer.data() + _buffer.size());
}
static_assert(std::is_same<TChar,char>::value || std::is_same<TChar,wchar_t>::value, "OutputDebugStringBuf only supports char and wchar_t types");
int sync() override try {
MessageOutputer<TChar,TTraits>()(pbase(), pptr());
setp(_buffer.data(), _buffer.data(), _buffer.data() + _buffer.size());
return 0;
} catch(...) {
return -1;
}
typename BaseClass::int_type overflow(typename BaseClass::int_type c = TTraits::eof()) override {
auto syncRet = sync();
if (c != TTraits::eof()) {
_buffer[0] = c;
setp(_buffer.data(), _buffer.data() + 1, _buffer.data() + _buffer.size());
}
return syncRet == -1 ? TTraits::eof() : 0;
}
private:
std::vector<TChar> _buffer;
template<typename TChar, typename TTraits>
struct MessageOutputer;
template<>
struct MessageOutputer<char, std::char_traits<char>> {
template<typename TIterator>
void operator()(TIterator begin, TIterator end) const {
std::string s(begin, end);
OutputDebugStringA(s.c_str());
}
};
template<>
struct MessageOutputer<wchar_t, std::char_traits<wchar_t>> {
template<typename TIterator>
void operator()(TIterator begin, TIterator end) const {
std::wstring s(begin, end);
OutputDebugStringW(s.c_str());
}
};
};
#endif
void RedirectStdoutToMSVC()
{
#ifdef ENABLE_MSVC_OUTPUT
static OutputDebugStringBuf<char> outputDebugBufChar;
static OutputDebugStringBuf<wchar_t> outputDebugBufWChar;
std::cout.rdbuf(&outputDebugBufChar);
std::cerr.rdbuf(&outputDebugBufChar);
std::clog.rdbuf(&outputDebugBufChar);
std::wcout.rdbuf(&outputDebugBufWChar);
std::wcerr.rdbuf(&outputDebugBufWChar);
std::wclog.rdbuf(&outputDebugBufWChar);
#endif
}
头文件:
// redirectStreamBuf.h
#ifndef REDIRECT_STREAM_BUF_H__
#define REDIRECT_STREAM_BUF_H__
void RedirectStdoutToMSVC();
#endif
这是用法以及一些测试用例:
And here is the usage along with some test cases:
#include "redirectStreamBuf.h"
#include <iostream>
int main(char *argv, int argc)
{
RedirectStdoutToMSVC();
std::cout << "Test cout" << std::endl;
std::cerr << "Test cerr" << std::endl;
std::clog << "Test clog" << std::endl;
std::wcout << L"Test wcout" << std::endl;
std::wcerr << L"Test wcerr" << std::endl;
std::wclog << L"Test wclog" << std::endl;
printf("This will not work :( \n");
system("PAUSE"); //Yes I know...
return 0;
}
我使用Visual Studio 2012测试了所有这些,并且工作正常.像printf
这样的C样式功能却无法正常工作...
I tested all this with Visual Studio 2012 and it works fine. C-Style functions like printf
do not work though...
这篇关于Visual Studio 2012-嵌入式输出控制台而不是cmd?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!