OpenLB 1.8.1
Loading...
Searching...
No Matches
consoleToFileWriter.h
Go to the documentation of this file.
1/* Lattice Boltzmann sample, written in C++, using the OpenLB
2 * library
3 *
4 * Copyright (C) 2025 Philipp Spelten
5 * E-mail contact: info@openlb.net
6 * The most recent release of OpenLB can be downloaded at
7 * <http://www.openlb.net/>
8 *
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public
20 * License along with this program; if not, write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
23*/
24
25#ifndef CONSOLETOFILEWRITER_H
26#define CONSOLETOFILEWRITER_H
27
28#include <iostream>
29#include <fstream>
30#include <streambuf>
31
32namespace olb {
33
34class DoubleBuffer : public std::streambuf {
35public:
36 DoubleBuffer(std::streambuf* consoleBuf, std::streambuf* fileBuf) : consoleBuffer(consoleBuf), fileBuffer(fileBuf) {}
37
38protected:
39 // This function is called for every character written to the stream
40 virtual int overflow(int c) override {
41 if (c != EOF) {
42 // Write the character to both buffers
43 if (consoleBuffer->sputc(c) == EOF || fileBuffer->sputc(c) == EOF) {
44 return EOF;
45 }
46 }
47 return c;
48 }
49
50 // Synchronize both buffers
51 virtual int sync() override {
52 if (consoleBuffer->pubsync() == 0 && fileBuffer->pubsync() == 0) {
53 return 0;
54 }
55 return -1;
56 }
57
58private:
59 std::streambuf* consoleBuffer; // Original console buffer
60 std::streambuf* fileBuffer; // File buffer
61
62};
63
64}
65
66#endif
DoubleBuffer(std::streambuf *consoleBuf, std::streambuf *fileBuf)
virtual int overflow(int c) override
virtual int sync() override
Top level namespace for all of OpenLB.