Computer Assisted Medical Intervention Tool Kit version 6.0
 
Loading...
Searching...
No Matches
ConsoleStream.h
Go to the documentation of this file.
1/*****************************************************************************
2 * $CAMITK_LICENCE_BEGIN$
3 *
4 * CamiTK - Computer Assisted Medical Intervention ToolKit
5 * (c) 2001-2025 Univ. Grenoble Alpes, CNRS, Grenoble INP - UGA, TIMC, 38000 Grenoble, France
6 *
7 * Visit http://camitk.imag.fr for more information
8 *
9 * This file is part of CamiTK.
10 *
11 * CamiTK is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
14 *
15 * CamiTK is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details.
19 *
20 * You should have received a copy of the GNU Lesser General Public License
21 * version 3 along with CamiTK. If not, see <http://www.gnu.org/licenses/>.
22 *
23 * $CAMITK_LICENCE_END$
24 ****************************************************************************/
25
26#ifndef ConsoleStream_H
27#define ConsoleStream_H
28
29// -- stl stuff
30#include <iostream>
31#include <streambuf>
32#include <string>
33#include <mutex>
34
35// -- QT stuff
36#include <QObject>
37#include <QTextEdit>
38#include <QMetaObject>
39
40namespace camitk {
74class ConsoleStream : public QObject, public std::basic_streambuf<char> {
75public:
77 ConsoleStream(QObject *parent = nullptr)
78 : QObject(parent),
79 myStream(nullptr),
80 previousBuffer(nullptr),
81 logTextEdit(nullptr) {}
82
84 ~ConsoleStream() override {
85 flushPending();
86 free();
87 }
88
90 void setStream(std::ostream* stream) {
91 free();
92 myStream = stream;
93 previousBuffer = stream->rdbuf();
94 stream->rdbuf(this);
95 }
96
98 void setTextEdit(QTextEdit* textEdit) {
99 logTextEdit = textEdit;
100 }
101
103 void init(std::ostream* stream, QTextEdit* textEdit) {
104 setTextEdit(textEdit);
105 setStream(stream);
106 }
107
109 void free() {
110 if (previousBuffer != nullptr && myStream != nullptr) {
111 myStream->rdbuf(previousBuffer);
112 myStream = nullptr;
113 previousBuffer = nullptr;
114 }
115 }
116
117signals:
119 void appendText(QString text);
120
121protected:
122
124 int_type overflow(int_type v) override {
125 if (v == '\n') {
126 flushPending();
127 }
128 else {
129 buffer += static_cast<char>(v);
130 }
131 return v;
132 }
133
135 std::streamsize xsputn(const char* p, std::streamsize n) override {
136 buffer.append(p, p + n);
137
138 std::string::size_type pos = 0;
139 while ((pos = buffer.find('\n')) != std::string::npos) {
140 std::string line = buffer.substr(0, pos);
141 buffer.erase(0, pos + 1);
142 emitText(QString::fromLocal8Bit(line.c_str()));
143 }
144
145 return n;
146 }
147
148private:
149 void emitText(const QString &text) {
150 if (!logTextEdit)
151 return;
152
153 // emit across threads safely
154 QMetaObject::invokeMethod(logTextEdit, [text, this]() {
155 logTextEdit->moveCursor(QTextCursor::End);
156 logTextEdit->insertPlainText(text + "\n");
157 logTextEdit->moveCursor(QTextCursor::End);
158 }, Qt::QueuedConnection);
159 }
160
161 void flushPending() {
162 if (!buffer.empty()) {
163 emitText(QString::fromLocal8Bit(buffer.c_str()));
164 buffer.clear();
165 }
166 }
167
168 std::ostream* myStream;
169 std::streambuf* previousBuffer;
170 std::string buffer;
171 QTextEdit* logTextEdit;
172};
173
174}
175
176
177
178#endif
Provides a console windows, within the CamiTK application.
Definition ConsoleStream.h:74
void appendText(QString text)
use a signal to print safely even if cout/cerr are written from other thread
std::streamsize xsputn(const char *p, std::streamsize n) override
rewriting of the inherited method xsputn
Definition ConsoleStream.h:135
void free()
reset the state as it was before (stream use the old buffer again)
Definition ConsoleStream.h:109
void setTextEdit(QTextEdit *textEdit)
set the log QTextEdit
Definition ConsoleStream.h:98
ConsoleStream(QObject *parent=nullptr)
default constructor, init(..) have to be called later, before first use
Definition ConsoleStream.h:77
~ConsoleStream() override
destructor: use free() to restore previous stream output buffer
Definition ConsoleStream.h:84
int_type overflow(int_type v) override
rewriting of the inherited method overflow
Definition ConsoleStream.h:124
void init(std::ostream *stream, QTextEdit *textEdit)
initialize ConsoleStream using both input stream and output text edit
Definition ConsoleStream.h:103
void setStream(std::ostream *stream)
set the value for the buffer to be replaced by the ConsoleStream
Definition ConsoleStream.h:90
Definition Action.cpp:40