-
Notifications
You must be signed in to change notification settings - Fork 56
Expand file tree
/
Copy pathwredirect.cpp
More file actions
45 lines (38 loc) · 962 Bytes
/
wredirect.cpp
File metadata and controls
45 lines (38 loc) · 962 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* $Id: wredirect.cpp,v 1.2 2017-07-29 08:50:36 markisch Exp $
*
* Redirect C++ output streams to wgnuplot text window
* (Caveat: does not compile with OpenWatcom)
*
*/
#include <iostream>
#include "wtext.h"
using namespace std;
// derived from code by Evan Teran
// http://stackoverflow.com/questions/311955/redirecting-cout-to-a-console-in-windows
class outbuf : public streambuf
{
public:
outbuf() {
setp(0, 0);
}
virtual int_type overflow(int_type c = traits_type::eof()) {
return fputc(c, stdout) == EOF ? traits_type::eof() : c;
}
};
extern "C" void
RedirectOutputStreams(int init)
{
static outbuf ob;
static streambuf * sb_cout;
static streambuf * sb_cerr;
if (init) {
// let cout/cerr use our custom streambuf
sb_cout = cout.rdbuf(&ob);
sb_cerr = cerr.rdbuf(&ob);
} else {
// make sure to restore the original so we don't get a crash on close!
cout.rdbuf(sb_cout);
cerr.rdbuf(sb_cerr);
}
}