source: rrlib_logging/messages/tFanOutBuffer.h @ 141:df696adabe8f

14.08
Last change on this file since 141:df696adabe8f was 141:df696adabe8f, checked in by Max Reichardt <mreichardt@…>, 3 years ago

Fixes compiler warning with gcc 9.3 ('catching polymorphic type by value')

File size: 5.6 KB
Line 
1//
2// You received this file as part of RRLib
3// Robotics Research Library
4//
5// Copyright (C) Finroc GbR (finroc.org)
6//
7// This program is free software; you can redistribute it and/or modify
8// it under the terms of the GNU General Public License as published by
9// the Free Software Foundation; either version 2 of the License, or
10// (at your option) any later version.
11//
12// This program is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16//
17// You should have received a copy of the GNU General Public License along
18// with this program; if not, write to the Free Software Foundation, Inc.,
19// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20//
21//----------------------------------------------------------------------
22/*!\file    rrlib/logging/messages/tFanOutBuffer.h
23 *
24 * \author  Tobias Foehst
25 *
26 * \date    2010-06-23
27 *
28 * \brief   Contains tFanOutBuffer
29 *
30 * \b tFanOutBuffer
31 *
32 * tFanOutBuffer is a specialization of std::streambuf with multiple
33 * sinks. Using a std::ostream for output internally uses a std::streambuf
34 * for the low level operations necessary to print stream input to a file
35 * or terminal, etc. By specializing std::streambuf in this class it is
36 * possible to fan out the stream's content to several other stream buffers,
37 * like tee in UNIX shells.
38 *
39 * Having an empty buffer vector also implements a null stream that does
40 * not open /dev/null to swallow all input.
41 *
42 */
43//----------------------------------------------------------------------
44#ifndef __rrlib__logging__include_guard__
45#error Invalid include directive. Try #include "rrlib/logging/messages.h" instead.
46#endif
47
48#ifndef __rrlib__logging__tFanOutBuffer_h__
49#define __rrlib__logging__tFanOutBuffer_h__
50
51#include "rrlib/logging/messages/tFormattingBuffer.h"
52
53//----------------------------------------------------------------------
54// External includes (system with <>, local with "")
55//----------------------------------------------------------------------
56#include <streambuf>
57#include <vector>
58
59//----------------------------------------------------------------------
60// Internal includes with ""
61//----------------------------------------------------------------------
62
63//----------------------------------------------------------------------
64// Debugging
65//----------------------------------------------------------------------
66
67//----------------------------------------------------------------------
68// Namespace declaration
69//----------------------------------------------------------------------
70namespace rrlib
71{
72namespace logging
73{
74
75//----------------------------------------------------------------------
76// Forward declarations / typedefs / enums
77//----------------------------------------------------------------------
78
79//----------------------------------------------------------------------
80// Class declaration
81//----------------------------------------------------------------------
82//! A specialization of std::streambuf with multiple sinks
83/*! Using a std::ostream for output internally uses a std::streambuf
84 * for the low level operations necessary to print stream input to a file
85 * or terminal, etc. By specializing std::streambuf in this class it is
86 * possible to fan out the stream's content to several other stream buffers,
87 * like tee in UNIX shells.
88 *
89 *  Having an empty buffer vector also implements a null stream that does
90 *  not open /dev/null to swallow all input.
91 *
92 */
93class tFanOutBuffer : public tFormattingBuffer
94{
95
96//----------------------------------------------------------------------
97// Public methods and typedefs
98//----------------------------------------------------------------------
99public:
100
101  tFanOutBuffer();
102
103  /*! Add a sink (stream buffer) to this buffer
104   *
105   * This buffer forwards its input to a list of target stream buffers.
106   * This method is used to add such a sink to the list.
107   *
108   * \param stream_buffer   The output stream buffer that should be added as sink
109   */
110  inline void AddSink(tFormattingBuffer stream_buffer)
111  {
112    this->formatting_buffers.push_back(stream_buffer);
113  }
114
115  inline void AddSink(std::streambuf &stream_buffer)
116  {
117    try
118    {
119      tFormattingBuffer &formatting_buffer = dynamic_cast<tFormattingBuffer &>(stream_buffer);
120      this->formatting_buffers.push_back(formatting_buffer);
121    }
122    catch (const std::bad_cast&)
123    {
124      this->buffers.push_back(&stream_buffer);
125    }
126  }
127
128  /*! Clear the buffer's list of sinks
129   *
130   * This method completely clears the list of sinks.
131   * After calling, the stream that uses this buffer acts as
132   * null stream, swallowing every input without generating any
133   * output.
134   */
135  inline void Clear()
136  {
137    this->formatting_buffers.clear();
138    this->buffers.clear();
139  }
140
141  virtual void SetColor(tFormattingBufferEffect effect, tFormattingBufferColor color);
142
143  virtual void ResetColor();
144
145  virtual void InitializeMultiLinePadding();
146
147  virtual void MarkEndOfPrefixForMultiLinePadding();
148
149
150//----------------------------------------------------------------------
151// Private fields and methods
152//----------------------------------------------------------------------
153private:
154
155  std::vector<tFormattingBuffer> formatting_buffers;
156  std::vector<std::streambuf *> buffers;
157
158  virtual int_type overflow(int_type c);
159
160  virtual int sync();
161
162};
163
164//----------------------------------------------------------------------
165// End of namespace declaration
166//----------------------------------------------------------------------
167}
168}
169
170#endif
Note: See TracBrowser for help on using the repository browser.