ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
Consoles.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 <QListWidget>
11 #include <QString>
12 
13 #include <CVLog.h>
14 
15 #include <functional>
16 #include <utility>
17 
23 {
24  public:
27  explicit ConsoleWrapper(std::function<void(const QString &)> printFn)
28  : m_printFn(std::move(printFn))
29  {
30  }
31 
43  void write(const char *messagePart)
44  {
45 
46  const size_t len = strlen(messagePart);
47  const char *messageEnd = messagePart + len;
48  const char *start = messagePart;
49 
50  const char *newLinePos;
51  while ((newLinePos = strchr(start, '\n')) != nullptr)
52  {
53  m_currentMessage += QString::fromUtf8(start, static_cast<int>(newLinePos - start));
54  m_printFn(m_currentMessage);
55  m_currentMessage.clear();
56  start = newLinePos + 1;
57  }
58 
59  if (start != messageEnd)
60  {
61  m_currentMessage += QString::fromUtf8(start, static_cast<int>(messageEnd - start));
62  }
63  }
64 
65  void flush() const
66  {
67  m_printFn(m_currentMessage);
68  }
69 
70  private:
71  QString m_currentMessage{};
72  std::function<void(const QString &)> m_printFn;
73 };
74 
77 {
78  public:
79  ccConsoleOutput() = default;
80  explicit ccConsoleOutput(const char *prefix) : m_prefix(prefix) {}
81 
82  void write(const char *messagePart)
83  {
84  if (!messagePart)
85  {
86  CVLog::Warning("Found invalid message given!");
87  }
88  else
89  {
90  m_output.write(messagePart);
91  }
92  }
93 
94  bool isatty() const
95  {
96  return false;
97  }
98 
99  void flush() const
100  {
101  m_output.flush();
102  }
103 
104  private:
105  const QString m_prefix;
106  ConsoleWrapper m_output{[this](const QString &message)
107  {
108  if (m_prefix.isEmpty())
109  {
110  CVLog::Print(message);
111  }
112  else
113  {
114  CVLog::Print(m_prefix + message);
115  }
116  }};
117 };
118 
121 {
122  public:
123  ListWidgetConsole(QListWidget *view, const Qt::GlobalColor color) : m_view(view), m_brush(color)
124  {
125  }
126  ListWidgetConsole(QListWidget *view, const QColor &color) : m_view(view), m_brush() {}
127  explicit ListWidgetConsole(QListWidget *view) : m_view(view), m_brush() {}
128 
129  void write(const char *messagePart)
130  {
131  if (!messagePart)
132  {
133  CVLog::Warning("Found invalid message given!");
134  }
135  else
136  {
137  m_output.write(messagePart);
138  }
139  }
140  bool isatty() const
141  {
142  return false;
143  }
144 
145  void flush() const
146  {
147  m_output.flush();
148  }
149 
150  private:
151  QListWidget *m_view;
152  QBrush m_brush;
153  QString m_prefix;
154  ConsoleWrapper m_output{[this](const QString &message)
155  {
156  if (m_view)
157  {
158  auto *messageItem = new QListWidgetItem(message);
159  messageItem->setForeground(m_brush);
160  m_view->addItem(messageItem);
161  }
162  else
163  {
164  if (m_prefix.isEmpty())
165  {
166  CVLog::Print(message);
167  }
168  else
169  {
170  CVLog::Print(m_prefix + message);
171  }
172  }
173  }};
174 };
math::float4 color
static bool Warning(const char *format,...)
Prints out a formatted warning message in console.
Definition: CVLog.cpp:133
static bool Print(const char *format,...)
Prints out a formatted message in console.
Definition: CVLog.cpp:113
ConsoleWrapper(std::function< void(const QString &)> printFn)
Definition: Consoles.h:27
void write(const char *messagePart)
Definition: Consoles.h:43
void flush() const
Definition: Consoles.h:65
Writes messages to the QListWidget given.
Definition: Consoles.h:121
bool isatty() const
Definition: Consoles.h:140
ListWidgetConsole(QListWidget *view, const Qt::GlobalColor color)
Definition: Consoles.h:123
ListWidgetConsole(QListWidget *view)
Definition: Consoles.h:127
void flush() const
Definition: Consoles.h:145
ListWidgetConsole(QListWidget *view, const QColor &color)
Definition: Consoles.h:126
void write(const char *messagePart)
Definition: Consoles.h:129
Redirects messages to ACloudViewer's console output.
Definition: Consoles.h:77
bool isatty() const
Definition: Consoles.h:94
ccConsoleOutput()=default
void write(const char *messagePart)
Definition: Consoles.h:82
void flush() const
Definition: Consoles.h:99
ccConsoleOutput(const char *prefix)
Definition: Consoles.h:80
Definition: Eigen.h:85