1 |
capela |
94 |
// qsamplerMessages.cpp |
2 |
|
|
// |
3 |
|
|
/**************************************************************************** |
4 |
capela |
3758 |
Copyright (C) 2004-2020, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
capela |
1464 |
Copyright (C) 2007, Christian Schoenebeck |
6 |
capela |
94 |
|
7 |
|
|
This program is free software; you can redistribute it and/or |
8 |
|
|
modify it under the terms of the GNU General Public License |
9 |
|
|
as published by the Free Software Foundation; either version 2 |
10 |
|
|
of the License, or (at your option) any later version. |
11 |
|
|
|
12 |
|
|
This program is distributed in the hope that it will be useful, |
13 |
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
|
|
GNU General Public License for more details. |
16 |
|
|
|
17 |
capela |
920 |
You should have received a copy of the GNU General Public License along |
18 |
|
|
with this program; if not, write to the Free Software Foundation, Inc., |
19 |
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
capela |
94 |
|
21 |
|
|
*****************************************************************************/ |
22 |
|
|
|
23 |
capela |
1465 |
#include "qsamplerAbout.h" |
24 |
capela |
94 |
#include "qsamplerMessages.h" |
25 |
|
|
|
26 |
capela |
1465 |
#include <QSocketNotifier> |
27 |
capela |
1738 |
|
28 |
|
|
#include <QFile> |
29 |
capela |
3788 |
#include <QTextBrowser> |
30 |
capela |
1465 |
#include <QTextCursor> |
31 |
capela |
1738 |
#include <QTextStream> |
32 |
capela |
1465 |
#include <QTextBlock> |
33 |
|
|
#include <QDateTime> |
34 |
|
|
#include <QIcon> |
35 |
schoenebeck |
1461 |
|
36 |
capela |
3358 |
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
37 |
capela |
115 |
#include <unistd.h> |
38 |
capela |
2669 |
#include <fcntl.h> |
39 |
capela |
115 |
#endif |
40 |
|
|
|
41 |
capela |
1558 |
|
42 |
capela |
3759 |
// Deprecated QTextStreamFunctions/Qt namespaces workaround. |
43 |
|
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) |
44 |
|
|
#define endl Qt::endl |
45 |
|
|
#endif |
46 |
|
|
|
47 |
|
|
|
48 |
capela |
1558 |
namespace QSampler { |
49 |
|
|
|
50 |
capela |
94 |
// The default maximum number of message lines. |
51 |
|
|
#define QSAMPLER_MESSAGES_MAXLINES 1000 |
52 |
|
|
|
53 |
|
|
// Notification pipe descriptors |
54 |
|
|
#define QSAMPLER_MESSAGES_FDNIL -1 |
55 |
|
|
#define QSAMPLER_MESSAGES_FDREAD 0 |
56 |
|
|
#define QSAMPLER_MESSAGES_FDWRITE 1 |
57 |
|
|
|
58 |
capela |
2582 |
|
59 |
capela |
94 |
//------------------------------------------------------------------------- |
60 |
capela |
1558 |
// QSampler::Messages - Messages log dockable window. |
61 |
capela |
94 |
// |
62 |
|
|
|
63 |
|
|
// Constructor. |
64 |
capela |
1558 |
Messages::Messages ( QWidget *pParent ) |
65 |
capela |
1739 |
: QDockWidget(pParent) |
66 |
capela |
94 |
{ |
67 |
capela |
1465 |
// Surely a name is crucial (e.g.for storing geometry settings) |
68 |
|
|
QDockWidget::setObjectName("qsamplerMessages"); |
69 |
|
|
|
70 |
capela |
1739 |
// Intialize stdout capture stuff. |
71 |
capela |
3555 |
m_pStdoutNotifier = nullptr; |
72 |
capela |
1739 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
73 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
74 |
capela |
94 |
|
75 |
capela |
1465 |
// Create local text view widget. |
76 |
capela |
3788 |
m_pMessagesTextView = new QTextBrowser(this); |
77 |
capela |
1738 |
// QFont font(m_pMessagesTextView->font()); |
78 |
capela |
94 |
// font.setFamily("Fixed"); |
79 |
capela |
1738 |
// m_pMessagesTextView->setFont(font); |
80 |
|
|
m_pMessagesTextView->setLineWrapMode(QTextEdit::NoWrap); |
81 |
capela |
3788 |
// m_pMessagesTextView->setReadOnly(true); |
82 |
|
|
// m_pMessagesTextView->setUndoRedoEnabled(false); |
83 |
capela |
1738 |
// m_pMessagesTextView->setTextFormat(Qt::LogText); |
84 |
capela |
94 |
|
85 |
capela |
1465 |
// Initialize default message limit. |
86 |
|
|
m_iMessagesLines = 0; |
87 |
|
|
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
88 |
|
|
|
89 |
capela |
3555 |
m_pMessagesLog = nullptr; |
90 |
capela |
1738 |
|
91 |
capela |
1465 |
// Prepare the dockable window stuff. |
92 |
capela |
1738 |
QDockWidget::setWidget(m_pMessagesTextView); |
93 |
capela |
3793 |
// QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures); |
94 |
capela |
1465 |
QDockWidget::setAllowedAreas( |
95 |
|
|
Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); |
96 |
capela |
772 |
// Some specialties to this kind of dock window... |
97 |
capela |
1465 |
QDockWidget::setMinimumHeight(120); |
98 |
capela |
94 |
|
99 |
capela |
1465 |
// Finally set the default caption and tooltip. |
100 |
|
|
const QString& sCaption = tr("Messages"); |
101 |
|
|
QDockWidget::setWindowTitle(sCaption); |
102 |
|
|
// QDockWidget::setWindowIcon(QIcon(":/icons/qsamplerMessages.png")); |
103 |
|
|
QDockWidget::setToolTip(sCaption); |
104 |
capela |
94 |
} |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
// Destructor. |
108 |
capela |
1558 |
Messages::~Messages (void) |
109 |
capela |
94 |
{ |
110 |
capela |
1739 |
// Turn off and close logging. |
111 |
capela |
1738 |
setLogging(false); |
112 |
|
|
|
113 |
capela |
1739 |
// No more notifications. |
114 |
|
|
if (m_pStdoutNotifier) |
115 |
|
|
delete m_pStdoutNotifier; |
116 |
capela |
94 |
|
117 |
capela |
1739 |
// No need to delete child widgets, Qt does it all for us. |
118 |
capela |
94 |
} |
119 |
|
|
|
120 |
|
|
|
121 |
capela |
2848 |
// Set stdout/stderr blocking mode. |
122 |
|
|
bool Messages::stdoutBlock ( int fd, bool bBlock ) const |
123 |
|
|
{ |
124 |
capela |
3358 |
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
125 |
capela |
2848 |
const int iFlags = ::fcntl(fd, F_GETFL, 0); |
126 |
|
|
const bool bNonBlock = bool(iFlags & O_NONBLOCK); |
127 |
|
|
if (bBlock && bNonBlock) |
128 |
|
|
bBlock = (::fcntl(fd, F_SETFL, iFlags & ~O_NONBLOCK) == 0); |
129 |
|
|
else |
130 |
|
|
if (!bBlock && !bNonBlock) |
131 |
|
|
bBlock = (::fcntl(fd, F_SETFL, iFlags | O_NONBLOCK) != 0); |
132 |
|
|
#endif |
133 |
|
|
return bBlock; |
134 |
|
|
} |
135 |
|
|
|
136 |
|
|
|
137 |
capela |
94 |
// Own stdout/stderr socket notifier slot. |
138 |
capela |
1558 |
void Messages::stdoutNotify ( int fd ) |
139 |
capela |
94 |
{ |
140 |
capela |
3358 |
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
141 |
capela |
2669 |
// Set non-blocking reads, if not already... |
142 |
capela |
2848 |
const bool bBlock = stdoutBlock(fd, false); |
143 |
capela |
2669 |
// Read as much as is available... |
144 |
|
|
QString sTemp; |
145 |
capela |
1739 |
char achBuffer[1024]; |
146 |
capela |
2669 |
const int cchBuffer = sizeof(achBuffer) - 1; |
147 |
|
|
int cchRead = ::read(fd, achBuffer, cchBuffer); |
148 |
|
|
while (cchRead > 0) { |
149 |
|
|
achBuffer[cchRead] = (char) 0; |
150 |
|
|
sTemp.append(achBuffer); |
151 |
capela |
2848 |
cchRead = (bBlock ? 0 : ::read(fd, achBuffer, cchBuffer)); |
152 |
capela |
1739 |
} |
153 |
capela |
2669 |
// Needs to be non-empty... |
154 |
|
|
if (!sTemp.isEmpty()) |
155 |
|
|
appendStdoutBuffer(sTemp); |
156 |
capela |
94 |
#endif |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
|
160 |
|
|
// Stdout buffer handler -- now splitted by complete new-lines... |
161 |
capela |
1558 |
void Messages::appendStdoutBuffer ( const QString& s ) |
162 |
capela |
94 |
{ |
163 |
capela |
1465 |
m_sStdoutBuffer.append(s); |
164 |
capela |
94 |
|
165 |
capela |
3788 |
processStdoutBuffer(); |
166 |
|
|
} |
167 |
|
|
|
168 |
|
|
void Messages::processStdoutBuffer (void) |
169 |
|
|
{ |
170 |
capela |
2582 |
const int iLength = m_sStdoutBuffer.lastIndexOf('\n'); |
171 |
capela |
1465 |
if (iLength > 0) { |
172 |
capela |
2582 |
const QString& sTemp = m_sStdoutBuffer.left(iLength); |
173 |
schoenebeck |
1474 |
m_sStdoutBuffer.remove(0, iLength + 1); |
174 |
capela |
1465 |
QStringList list = sTemp.split('\n'); |
175 |
|
|
QStringListIterator iter(list); |
176 |
|
|
while (iter.hasNext()) |
177 |
|
|
appendMessagesText(iter.next()); |
178 |
|
|
} |
179 |
capela |
94 |
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
// Stdout flusher -- show up any unfinished line... |
183 |
capela |
1558 |
void Messages::flushStdoutBuffer (void) |
184 |
capela |
94 |
{ |
185 |
capela |
1739 |
if (!m_sStdoutBuffer.isEmpty()) { |
186 |
capela |
3788 |
processStdoutBuffer(); |
187 |
capela |
2669 |
m_sStdoutBuffer.clear(); |
188 |
capela |
1739 |
} |
189 |
capela |
94 |
} |
190 |
|
|
|
191 |
|
|
|
192 |
|
|
// Stdout capture accessors. |
193 |
capela |
1558 |
bool Messages::isCaptureEnabled (void) |
194 |
capela |
94 |
{ |
195 |
capela |
3555 |
return (m_pStdoutNotifier != nullptr); |
196 |
capela |
94 |
} |
197 |
|
|
|
198 |
capela |
1558 |
void Messages::setCaptureEnabled ( bool bCapture ) |
199 |
capela |
94 |
{ |
200 |
capela |
1739 |
// Flush current buffer. |
201 |
|
|
flushStdoutBuffer(); |
202 |
capela |
94 |
|
203 |
capela |
3358 |
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
204 |
capela |
1465 |
// Destroy if already enabled. |
205 |
|
|
if (!bCapture && m_pStdoutNotifier) { |
206 |
|
|
delete m_pStdoutNotifier; |
207 |
capela |
3555 |
m_pStdoutNotifier = nullptr; |
208 |
capela |
1465 |
// Close the notification pipes. |
209 |
|
|
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
210 |
|
|
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
211 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
212 |
|
|
} |
213 |
|
|
} |
214 |
|
|
// Are we going to make up the capture? |
215 |
capela |
3555 |
if (bCapture && m_pStdoutNotifier == nullptr && ::pipe(m_fdStdout) == 0) { |
216 |
capela |
1465 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
217 |
|
|
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
218 |
|
|
m_pStdoutNotifier = new QSocketNotifier( |
219 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this); |
220 |
|
|
QObject::connect(m_pStdoutNotifier, |
221 |
|
|
SIGNAL(activated(int)), |
222 |
|
|
SLOT(stdoutNotify(int))); |
223 |
|
|
} |
224 |
capela |
94 |
#endif |
225 |
|
|
} |
226 |
|
|
|
227 |
|
|
|
228 |
|
|
// Message font accessors. |
229 |
capela |
1558 |
QFont Messages::messagesFont (void) |
230 |
capela |
94 |
{ |
231 |
capela |
1739 |
return m_pMessagesTextView->font(); |
232 |
capela |
94 |
} |
233 |
|
|
|
234 |
capela |
1738 |
void Messages::setMessagesFont ( const QFont& font ) |
235 |
capela |
94 |
{ |
236 |
capela |
1739 |
m_pMessagesTextView->setFont(font); |
237 |
capela |
94 |
} |
238 |
|
|
|
239 |
|
|
|
240 |
|
|
// Maximum number of message lines accessors. |
241 |
capela |
1558 |
int Messages::messagesLimit (void) |
242 |
capela |
94 |
{ |
243 |
capela |
1739 |
return m_iMessagesLimit; |
244 |
capela |
94 |
} |
245 |
|
|
|
246 |
capela |
1558 |
void Messages::setMessagesLimit ( int iMessagesLimit ) |
247 |
capela |
94 |
{ |
248 |
capela |
1739 |
m_iMessagesLimit = iMessagesLimit; |
249 |
|
|
m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3); |
250 |
capela |
94 |
} |
251 |
|
|
|
252 |
capela |
1738 |
// Messages logging stuff. |
253 |
|
|
bool Messages::isLogging (void) const |
254 |
|
|
{ |
255 |
capela |
3555 |
return (m_pMessagesLog != nullptr); |
256 |
capela |
1738 |
} |
257 |
capela |
94 |
|
258 |
capela |
1738 |
void Messages::setLogging ( bool bEnabled, const QString& sFilename ) |
259 |
capela |
94 |
{ |
260 |
capela |
1738 |
if (m_pMessagesLog) { |
261 |
|
|
appendMessages(tr("Logging stopped --- %1 ---") |
262 |
|
|
.arg(QDateTime::currentDateTime().toString())); |
263 |
|
|
m_pMessagesLog->close(); |
264 |
|
|
delete m_pMessagesLog; |
265 |
capela |
3555 |
m_pMessagesLog = nullptr; |
266 |
capela |
1738 |
} |
267 |
|
|
|
268 |
|
|
if (bEnabled) { |
269 |
|
|
m_pMessagesLog = new QFile(sFilename); |
270 |
|
|
if (m_pMessagesLog->open(QIODevice::Text | QIODevice::Append)) { |
271 |
|
|
appendMessages(tr("Logging started --- %1 ---") |
272 |
|
|
.arg(QDateTime::currentDateTime().toString())); |
273 |
|
|
} else { |
274 |
|
|
delete m_pMessagesLog; |
275 |
capela |
3555 |
m_pMessagesLog = nullptr; |
276 |
capela |
1738 |
} |
277 |
|
|
} |
278 |
capela |
94 |
} |
279 |
|
|
|
280 |
capela |
1738 |
|
281 |
|
|
// Messages log output method. |
282 |
|
|
void Messages::appendMessagesLog ( const QString& s ) |
283 |
capela |
94 |
{ |
284 |
capela |
1738 |
if (m_pMessagesLog) { |
285 |
capela |
3788 |
QTextStream(m_pMessagesLog) |
286 |
|
|
<< QTime::currentTime().toString("hh:mm:ss.zzz") |
287 |
|
|
<< ' ' << s << endl; |
288 |
capela |
1738 |
m_pMessagesLog->flush(); |
289 |
|
|
} |
290 |
capela |
94 |
} |
291 |
|
|
|
292 |
capela |
1738 |
// Messages widget output method. |
293 |
|
|
void Messages::appendMessagesLine ( const QString& s ) |
294 |
capela |
94 |
{ |
295 |
capela |
1738 |
// Check for message line limit... |
296 |
|
|
if (m_iMessagesLines > m_iMessagesHigh) { |
297 |
|
|
m_pMessagesTextView->setUpdatesEnabled(false); |
298 |
|
|
QTextCursor textCursor(m_pMessagesTextView->document()->begin()); |
299 |
capela |
1465 |
while (m_iMessagesLines > m_iMessagesLimit) { |
300 |
|
|
// Move cursor extending selection |
301 |
|
|
// from start to next line-block... |
302 |
|
|
textCursor.movePosition( |
303 |
|
|
QTextCursor::NextBlock, QTextCursor::KeepAnchor); |
304 |
|
|
m_iMessagesLines--; |
305 |
|
|
} |
306 |
|
|
// Remove the excessive line-blocks... |
307 |
|
|
textCursor.removeSelectedText(); |
308 |
capela |
1738 |
m_pMessagesTextView->setUpdatesEnabled(true); |
309 |
|
|
} |
310 |
capela |
1465 |
|
311 |
capela |
1738 |
m_pMessagesTextView->append(s); |
312 |
capela |
1465 |
m_iMessagesLines++; |
313 |
capela |
94 |
} |
314 |
|
|
|
315 |
|
|
|
316 |
capela |
1738 |
// The main utility methods. |
317 |
|
|
void Messages::appendMessages ( const QString& s ) |
318 |
|
|
{ |
319 |
capela |
3788 |
appendMessagesColor(s, Qt::gray); |
320 |
capela |
1738 |
} |
321 |
|
|
|
322 |
capela |
3788 |
void Messages::appendMessagesColor ( const QString& s, const QColor& rgb ) |
323 |
capela |
1738 |
{ |
324 |
capela |
3788 |
appendMessagesLine("<font color=\"" + rgb.name() + "\">" + s + "</font>"); |
325 |
|
|
appendMessagesLog(s); |
326 |
capela |
1738 |
} |
327 |
|
|
|
328 |
|
|
void Messages::appendMessagesText ( const QString& s ) |
329 |
|
|
{ |
330 |
|
|
appendMessagesLine(s); |
331 |
|
|
appendMessagesLog(s); |
332 |
|
|
} |
333 |
|
|
|
334 |
|
|
|
335 |
capela |
1465 |
// History reset. |
336 |
capela |
1558 |
void Messages::clear (void) |
337 |
capela |
94 |
{ |
338 |
capela |
1465 |
m_iMessagesLines = 0; |
339 |
capela |
1738 |
m_pMessagesTextView->clear(); |
340 |
capela |
94 |
} |
341 |
|
|
|
342 |
capela |
1558 |
} // namespace QSampler |
343 |
capela |
94 |
|
344 |
capela |
1558 |
|
345 |
capela |
94 |
// end of qsamplerMessages.cpp |