File: automate_ostream_demuxed.hh

package info (click to toggle)
monotone 0.48-3
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 20,096 kB
  • ctags: 8,077
  • sloc: cpp: 81,000; sh: 6,402; perl: 1,241; lisp: 1,045; makefile: 655; python: 566; sql: 112; ansic: 52
file content (156 lines) | stat: -rw-r--r-- 4,278 bytes parent folder | download
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright (C) 2009 and later by various people
// see monotone commit logs for details and authors
//
// This program is made available under the GNU GPL version 2.0 or
// greater. See the accompanying file COPYING for details.
//
// This program is distributed WITHOUT ANY WARRANTY; without even the
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
// PURPOSE.

#ifndef __AUTOMATE_OSTREAM_DEMUXED_HH__
#define __AUTOMATE_OSTREAM_DEMUXED_HH__

#include <iostream>

#include "automate_ostream.hh"
#include "simplestring_xform.hh"

template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
class basic_automate_streambuf_demuxed : public std::basic_streambuf<_CharT, _Traits>
{
  typedef _Traits traits_type;
  typedef typename _Traits::int_type int_type;
  size_t _bufsize;
  std::basic_ostream<_CharT, _Traits> *mystdout;
  std::basic_ostream<_CharT, _Traits> *errout;
public:
  basic_automate_streambuf_demuxed(std::ostream & out, std::ostream & err,
                                   size_t bufsize) :
    std::streambuf(),
    _bufsize(bufsize),
    mystdout(&out),
    errout(&err)
  {
    _CharT * inbuf = new _CharT[_bufsize];
    setp(inbuf, inbuf + _bufsize);
  }

  ~basic_automate_streambuf_demuxed() { }

  void end_cmd()
  {
    _M_sync();
  }

  virtual int sync()
  {
    _M_sync();
    return 0;
  }

  void write_out_of_band(char type, std::string const & data)
  {
    // FIXME: ignore tickers, because we'd have to track their state
    // here and they would pollute the output too much anyways
    if (type == 't')
      return;

    std::string out;
    i18n_format prefix;
    if (type == 'w')
      prefix = F("%s: remote warning: ") % prog_name;
    else if (type == 'e')
      prefix = F("%s: remote error: ") % prog_name;
    else if (type == 'p')
      prefix = F("%s: remote message: ") % prog_name;
    else
      I(false);

    prefix_lines_with(prefix.str(), data, out);
    (*errout) << out << std::endl;
  }

  void write_headers(std::vector<std::pair<std::string,std::string> > const & headers)
  {
    i18n_format prefix = F("%s: remote header: ") % prog_name;
    for (std::vector<std::pair<std::string, std::string> >::const_iterator h = headers.begin();
       h != headers.end(); ++h)
      {
        (*errout) << prefix.str() << h->first << ": " << h->second << std::endl;
      }
  }

  int_type overflow(int_type c = traits_type::eof())
  {
    sync();
    sputc(c);
    return 0;
  }
private:
  void _M_sync()
  {
    if (!mystdout)
      {
        setp(this->pbase(), this->pbase() + _bufsize);
        return;
      }
    int num = this->pptr() - this->pbase();
    if (num)
      {
        (*mystdout) << std::basic_string<_CharT, _Traits>(this->pbase(), num);
        setp(this->pbase(), this->pbase() + _bufsize);
        mystdout->flush();
      }
  }
};

template<typename _CharT, typename _Traits = std::char_traits<_CharT> >
struct basic_automate_ostream_demuxed : public basic_automate_ostream<_CharT, _Traits>
{
  typedef basic_automate_streambuf_demuxed<_CharT, _Traits> streambuf_type;
  streambuf_type _M_autobuf;
  int errcode;

public:
  basic_automate_ostream_demuxed(std::basic_ostream<_CharT, _Traits> &out,
                                 std::basic_ostream<_CharT, _Traits> &err,
                                 size_t blocksize)
    : _M_autobuf(out, err, blocksize)
  { this->init(&_M_autobuf); }

  virtual ~basic_automate_ostream_demuxed()
  {}

  streambuf_type *
  rdbuf() const
  { return const_cast<streambuf_type *>(&_M_autobuf); }

  virtual void end_cmd(int error)
  {
    errcode = error;
    _M_autobuf.end_cmd();
  }

  int get_error() const
  { return errcode; }

  virtual void write_out_of_band(char type, std::string const & data)
  { _M_autobuf.write_out_of_band(type, data); }

  virtual void write_headers(std::vector<std::pair<std::string,std::string> > const & headers)
  { _M_autobuf.write_headers(headers); }
};

typedef basic_automate_streambuf_demuxed<char> automate_streambuf_demuxed;
typedef basic_automate_ostream_demuxed<char> automate_ostream_demuxed;

#endif

// Local Variables:
// mode: C++
// fill-column: 76
// c-file-style: "gnu"
// indent-tabs-mode: nil
// End:
// vim: et:sw=2:sts=2:ts=2:cino=>2s,{s,\:s,+s,t0,g0,^-2,e-2,n-2,p2s,(0,=s: