vul_redirector.cxx
Go to the documentation of this file.
1 // This is core/vul/vul_redirector.cxx
2 //:
3 // \file
4 
5 #include <iostream>
6 #include <cstdio>
7 #ifdef _MSC_VER
8 # include <vcl_msvc_warnings.h>
9 #endif
10 
11 #include "vul_redirector.h"
12 
13 //----------------------------------------------------------------------
14 // This class is used as a stream buffer that can
15 // redirect output from cout, cerr, clog to a CoutWindow class.
16 class vul_redirector_streambuf : public std::streambuf
17 {
19  public:
21  int sync () override;
22  int overflow (int ch) override;
23  int underflow() override{return 0;}
24  std::streamsize xsputn (const char* text, std::streamsize n) override;
25 };
26 
28 {
30  std::streambuf* old_cerrbuf;
32  std::ostream* s;
33 };
34 
35 /////////////////////////////////////////////////////////////////////////////
36 // streambuf stuff
37 
39 {
40  std::ptrdiff_t n = pptr () - pbase ();
41  return (n && p->owner->putchunk ( pbase (), n) != n) ? EOF : 0;
42 }
43 
45 {
46  int n = static_cast<int>(pptr () - pbase ());
47  if (n && sync ())
48  return EOF;
49  if (ch != EOF)
50  {
51  char cbuf[1];
52  cbuf[0] = (char)ch;
53  if (p->owner->putchunk ( cbuf, 1) != 1)
54  return EOF;
55  }
56  pbump (-n); // Reset pptr().
57  return 0;
58 }
59 
60 std::streamsize vul_redirector_streambuf::xsputn (const char* text, std::streamsize n)
61 {
62  return sync () == EOF ? 0 : p->owner->putchunk ( text, n);
63 }
64 
65 //////////////// Data for debugging
66 
67 
69  p(new vul_redirector_data)
70 {
71  p->owner = this;
73  p->old_cerrbuf = s.rdbuf();
74  s.rdbuf(p->buf);
75  p->s = &s;
76 }
77 
79 {
80  p->s->rdbuf(p->old_cerrbuf);
81  delete p->buf;
82  delete p;
83 }
84 
86 {
87  return p->old_cerrbuf->pubsync();
88 }
89 
90 std::streamsize vul_redirector::put_passthru(char const* buf, std::streamsize n)
91 {
92  return p->old_cerrbuf->sputn(buf, n);
93 }
94 
95 //: Default action is just to pass text on the old stream.
96 std::streamsize vul_redirector::putchunk(char const* buf, std::streamsize n)
97 {
98  return put_passthru(buf, n);
99 }
virtual std::streamsize putchunk(char const *buf, std::streamsize n)
The filter function.
virtual ~vul_redirector()
Destroy redirector, restore stream to original.
int overflow(int ch) override
vul_redirector_data * p
Contains class for simplified redirection of iostreams.
vul_redirector_streambuf(vul_redirector_data *p_)
vul_redirector_streambuf * buf
int sync_passthru()
Sync original stream.
std::streamsize xsputn(const char *text, std::streamsize n) override
vul_redirector(std::ostream &s)
Attach redirector to std::ostream s, so that all future output to s goes through this->putchunk.
std::streambuf * old_cerrbuf
vul_redirector * owner
Simplified redirection of iostreams.
vul_redirector_data * p
std::streamsize put_passthru(char const *buf, std::streamsize n)
Put characters to original stream.