ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
log_widget.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 #include "util/option_manager.h"
16 
17 namespace colmap {
18 
19 template <class Elem = char, class Tr = std::char_traits<Elem>>
20 class StandardOutputRedirector : public std::basic_streambuf<Elem, Tr> {
21  typedef void (*cb_func_ptr)(const Elem*, std::streamsize count, void* data);
22 
23 public:
24  StandardOutputRedirector(std::ostream& stream,
25  cb_func_ptr cb_func,
26  void* data)
27  : stream_(stream), cb_func_(cb_func), data_(data) {
28  buf_ = stream_.rdbuf(this);
29  };
30 
31  ~StandardOutputRedirector() { stream_.rdbuf(buf_); }
32 
33  std::streamsize xsputn(const Elem* ptr, std::streamsize count) {
34  cb_func_(ptr, count, data_);
35  return count;
36  }
37 
38  typename Tr::int_type overflow(typename Tr::int_type v) {
39  Elem ch = Tr::to_char_type(v);
40  cb_func_(&ch, 1, data_);
41  return Tr::not_eof(v);
42  }
43 
44 private:
45  std::basic_ostream<Elem, Tr>& stream_;
46  std::streambuf* buf_;
47  cb_func_ptr cb_func_;
48  void* data_;
49 };
50 
51 class LogWidget : public QWidget {
52 public:
53  LogWidget(QWidget* parent, const int max_num_blocks = 100000);
54  ~LogWidget();
55 
56  void Append(const std::string& text);
57  void Flush();
58  void Clear();
59 
60 private:
61  static void Update(const char* text,
62  std::streamsize count,
63  void* text_box_ptr);
64 
65  void SaveLog();
66 
67  QMutex mutex_;
68  std::string text_queue_;
69  QPlainTextEdit* text_box_;
70  std::ofstream log_file_;
74 };
75 
76 } // namespace colmap
int count
LogWidget(QWidget *parent, const int max_num_blocks=100000)
Definition: log_widget.cc:36
void Append(const std::string &text)
Definition: log_widget.cc:94
std::streamsize xsputn(const Elem *ptr, std::streamsize count)
Definition: log_widget.h:33
Tr::int_type overflow(typename Tr::int_type v)
Definition: log_widget.h:38
StandardOutputRedirector(std::ostream &stream, cb_func_ptr cb_func, void *data)
Definition: log_widget.h:24
GraphType data
Definition: graph_cut.cc:138