ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
LogWidget.h
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #pragma once
9 
10 #include <QtGui>
11 #include <QtWidgets>
12 #include <fstream>
13 #include <iostream>
14 
15 namespace cloudViewer {
16 
17 template <class Elem = char, class Tr = std::char_traits<Elem>>
18 class StandardOutputRedirector : public std::basic_streambuf<Elem, Tr> {
19  typedef void (*cb_func_ptr)(const Elem*, std::streamsize count, void* data);
20 
21 public:
22  StandardOutputRedirector(std::ostream& stream,
23  cb_func_ptr cb_func,
24  void* data)
25  : stream_(stream), cb_func_(cb_func), data_(data) {
26  buf_ = stream_.rdbuf(this);
27  }
28 
29  ~StandardOutputRedirector() { stream_.rdbuf(buf_); }
30 
31  std::streamsize xsputn(const Elem* ptr, std::streamsize count) {
32  cb_func_(ptr, count, data_);
33  return count;
34  }
35 
36  typename Tr::int_type overflow(typename Tr::int_type v) {
37  Elem ch = Tr::to_char_type(v);
38  cb_func_(&ch, 1, data_);
39  return Tr::not_eof(v);
40  }
41 
42 private:
43  std::basic_ostream<Elem, Tr>& stream_;
44  std::streambuf* buf_;
45  cb_func_ptr cb_func_;
46  void* data_;
47 };
48 
49 class LogWidget : public QWidget {
50 public:
51  LogWidget(QWidget* parent, const int max_num_blocks = 100000);
52  ~LogWidget();
53 
54  void Append(const std::string& text);
55  void Flush();
56  void Clear();
57 
58 private:
59  static void Update(const char* text,
60  std::streamsize count,
61  void* text_box_ptr);
62 
63  void SaveLog();
64 
65  QMutex mutex_;
66  std::string text_queue_;
67  // Buffer for CVLog output (accumulates until newline)
68  std::string cvlog_buffer_;
69  QPlainTextEdit* text_box_;
70  std::ofstream log_file_;
74 };
75 
76 } // namespace cloudViewer
int count
LogWidget(QWidget *parent, const int max_num_blocks=100000)
Definition: LogWidget.cpp:20
void Append(const std::string &text)
Definition: LogWidget.cpp:89
std::streamsize xsputn(const Elem *ptr, std::streamsize count)
Definition: LogWidget.h:31
StandardOutputRedirector(std::ostream &stream, cb_func_ptr cb_func, void *data)
Definition: LogWidget.h:22
Tr::int_type overflow(typename Tr::int_type v)
Definition: LogWidget.h:36
Generic file read and write utility for python interface.