monotone

monotone Mtn Source Tree

Root/src/automate_ostream_demuxed.hh

1// Copyright (C) 2009 and later by various people
2// see monotone commit logs for details and authors
3//
4// This program is made available under the GNU GPL version 2.0 or
5// greater. See the accompanying file COPYING for details.
6//
7// This program is distributed WITHOUT ANY WARRANTY; without even the
8// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
9// PURPOSE.
10
11#ifndef __AUTOMATE_OSTREAM_DEMUXED_HH__
12#define __AUTOMATE_OSTREAM_DEMUXED_HH__
13
14#include <iostream>
15
16#include "automate_ostream.hh"
17#include "simplestring_xform.hh"
18
19template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
20class basic_automate_streambuf_demuxed : public std::basic_streambuf<_CharT, _Traits>
21{
22 typedef _Traits traits_type;
23 typedef typename _Traits::int_type int_type;
24 size_t _bufsize;
25 std::basic_ostream<_CharT, _Traits> *mystdout;
26 std::basic_ostream<_CharT, _Traits> *errout;
27public:
28 basic_automate_streambuf_demuxed(std::ostream & out, std::ostream & err,
29 size_t bufsize) :
30 std::basic_streambuf<_CharT, _Traits>(),
31 _bufsize(bufsize),
32 mystdout(&out),
33 errout(&err)
34 {
35 _CharT * inbuf = new _CharT[_bufsize];
36 this->setp(inbuf, inbuf + _bufsize);
37 }
38
39 ~basic_automate_streambuf_demuxed() { }
40
41 void end_cmd()
42 {
43 _M_sync();
44 }
45
46 virtual int sync()
47 {
48 _M_sync();
49 return 0;
50 }
51
52 void write_out_of_band(char type, std::string const & data)
53 {
54 // FIXME: ignore tickers, because we'd have to track their state
55 // here and they would pollute the output too much anyways
56 if (type == 't')
57 return;
58
59 std::string out;
60 i18n_format prefix;
61 if (type == 'w')
62 prefix = F("%s: remote warning: ") % prog_name;
63 else if (type == 'e')
64 prefix = F("%s: remote error: ") % prog_name;
65 else if (type == 'p')
66 prefix = F("%s: remote message: ") % prog_name;
67 else
68 I(false);
69
70 prefix_lines_with(prefix.str(), data, out);
71 (*errout) << out << std::endl;
72 }
73
74 void write_headers(std::vector<std::pair<std::string,std::string> > const & headers)
75 {
76 i18n_format prefix = F("%s: remote header: ") % prog_name;
77 for (std::vector<std::pair<std::string, std::string> >::const_iterator h = headers.begin();
78 h != headers.end(); ++h)
79 {
80 (*errout) << prefix.str() << h->first << ": " << h->second << std::endl;
81 }
82 }
83
84 int_type overflow(int_type c = traits_type::eof())
85 {
86 sync();
87 this->sputc(c);
88 return 0;
89 }
90private:
91 void _M_sync()
92 {
93 if (!mystdout)
94 {
95 this->setp(this->pbase(), this->pbase() + _bufsize);
96 return;
97 }
98 int num = this->pptr() - this->pbase();
99 if (num)
100 {
101 (*mystdout) << std::basic_string<_CharT, _Traits>(this->pbase(), num);
102 this->setp(this->pbase(), this->pbase() + _bufsize);
103 mystdout->flush();
104 }
105 }
106};
107
108template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
109struct basic_automate_ostream_demuxed : public basic_automate_ostream<_CharT, _Traits>
110{
111 typedef basic_automate_streambuf_demuxed<_CharT, _Traits> streambuf_type;
112 streambuf_type _M_autobuf;
113 int errcode;
114
115public:
116 basic_automate_ostream_demuxed(std::basic_ostream<_CharT, _Traits> &out,
117 std::basic_ostream<_CharT, _Traits> &err,
118 size_t blocksize)
119 : _M_autobuf(out, err, blocksize)
120 { this->init(&_M_autobuf); }
121
122 virtual ~basic_automate_ostream_demuxed()
123 {}
124
125 streambuf_type *
126 rdbuf() const
127 { return const_cast<streambuf_type *>(&_M_autobuf); }
128
129 virtual void end_cmd(int error)
130 {
131 errcode = error;
132 _M_autobuf.end_cmd();
133 }
134
135 int get_error() const
136 { return errcode; }
137
138 virtual void write_out_of_band(char type, std::string const & data)
139 { _M_autobuf.write_out_of_band(type, data); }
140
141 virtual void write_headers(std::vector<std::pair<std::string,std::string> > const & headers)
142 { _M_autobuf.write_headers(headers); }
143};
144
145typedef basic_automate_streambuf_demuxed<char> automate_streambuf_demuxed;
146typedef basic_automate_ostream_demuxed<char> automate_ostream_demuxed;
147
148#endif
149
150// Local Variables:
151// mode: C++
152// fill-column: 76
153// c-file-style: "gnu"
154// indent-tabs-mode: nil
155// End:
156// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s:

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status