1 |
// qsamplerMessages.cpp |
// qsamplerMessages.cpp |
2 |
// |
// |
3 |
/**************************************************************************** |
/**************************************************************************** |
4 |
Copyright (C) 2004-2017, rncbc aka Rui Nuno Capela. All rights reserved. |
Copyright (C) 2004-2020, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
Copyright (C) 2007, Christian Schoenebeck |
Copyright (C) 2007, Christian Schoenebeck |
6 |
|
|
7 |
This program is free software; you can redistribute it and/or |
This program is free software; you can redistribute it and/or |
26 |
#include <QSocketNotifier> |
#include <QSocketNotifier> |
27 |
|
|
28 |
#include <QFile> |
#include <QFile> |
29 |
#include <QTextEdit> |
#include <QTextBrowser> |
30 |
#include <QTextCursor> |
#include <QTextCursor> |
31 |
#include <QTextStream> |
#include <QTextStream> |
32 |
#include <QTextBlock> |
#include <QTextBlock> |
|
#include <QScrollBar> |
|
33 |
#include <QDateTime> |
#include <QDateTime> |
34 |
#include <QIcon> |
#include <QIcon> |
35 |
|
|
39 |
#endif |
#endif |
40 |
|
|
41 |
|
|
42 |
|
// 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 |
namespace QSampler { |
namespace QSampler { |
49 |
|
|
50 |
// The default maximum number of message lines. |
// The default maximum number of message lines. |
68 |
QDockWidget::setObjectName("qsamplerMessages"); |
QDockWidget::setObjectName("qsamplerMessages"); |
69 |
|
|
70 |
// Intialize stdout capture stuff. |
// Intialize stdout capture stuff. |
71 |
m_pStdoutNotifier = NULL; |
m_pStdoutNotifier = nullptr; |
72 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
73 |
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
74 |
|
|
75 |
// Create local text view widget. |
// Create local text view widget. |
76 |
m_pMessagesTextView = new QTextEdit(this); |
m_pMessagesTextView = new QTextBrowser(this); |
77 |
// QFont font(m_pMessagesTextView->font()); |
// QFont font(m_pMessagesTextView->font()); |
78 |
// font.setFamily("Fixed"); |
// font.setFamily("Fixed"); |
79 |
// m_pMessagesTextView->setFont(font); |
// m_pMessagesTextView->setFont(font); |
80 |
m_pMessagesTextView->setLineWrapMode(QTextEdit::NoWrap); |
m_pMessagesTextView->setLineWrapMode(QTextEdit::NoWrap); |
81 |
m_pMessagesTextView->setReadOnly(true); |
// m_pMessagesTextView->setReadOnly(true); |
82 |
m_pMessagesTextView->setUndoRedoEnabled(false); |
// m_pMessagesTextView->setUndoRedoEnabled(false); |
83 |
// m_pMessagesTextView->setTextFormat(Qt::LogText); |
// m_pMessagesTextView->setTextFormat(Qt::LogText); |
84 |
|
|
85 |
// Initialize default message limit. |
// Initialize default message limit. |
86 |
m_iMessagesLines = 0; |
m_iMessagesLines = 0; |
87 |
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
88 |
|
|
89 |
m_pMessagesLog = NULL; |
m_pMessagesLog = nullptr; |
90 |
|
|
91 |
// Prepare the dockable window stuff. |
// Prepare the dockable window stuff. |
92 |
QDockWidget::setWidget(m_pMessagesTextView); |
QDockWidget::setWidget(m_pMessagesTextView); |
162 |
{ |
{ |
163 |
m_sStdoutBuffer.append(s); |
m_sStdoutBuffer.append(s); |
164 |
|
|
165 |
|
processStdoutBuffer(); |
166 |
|
} |
167 |
|
|
168 |
|
void Messages::processStdoutBuffer (void) |
169 |
|
{ |
170 |
const int iLength = m_sStdoutBuffer.lastIndexOf('\n'); |
const int iLength = m_sStdoutBuffer.lastIndexOf('\n'); |
171 |
if (iLength > 0) { |
if (iLength > 0) { |
172 |
const QString& sTemp = m_sStdoutBuffer.left(iLength); |
const QString& sTemp = m_sStdoutBuffer.left(iLength); |
183 |
void Messages::flushStdoutBuffer (void) |
void Messages::flushStdoutBuffer (void) |
184 |
{ |
{ |
185 |
if (!m_sStdoutBuffer.isEmpty()) { |
if (!m_sStdoutBuffer.isEmpty()) { |
186 |
appendMessagesText(m_sStdoutBuffer); |
processStdoutBuffer(); |
187 |
m_sStdoutBuffer.clear(); |
m_sStdoutBuffer.clear(); |
188 |
} |
} |
189 |
} |
} |
192 |
// Stdout capture accessors. |
// Stdout capture accessors. |
193 |
bool Messages::isCaptureEnabled (void) |
bool Messages::isCaptureEnabled (void) |
194 |
{ |
{ |
195 |
return (m_pStdoutNotifier != NULL); |
return (m_pStdoutNotifier != nullptr); |
196 |
} |
} |
197 |
|
|
198 |
void Messages::setCaptureEnabled ( bool bCapture ) |
void Messages::setCaptureEnabled ( bool bCapture ) |
204 |
// Destroy if already enabled. |
// Destroy if already enabled. |
205 |
if (!bCapture && m_pStdoutNotifier) { |
if (!bCapture && m_pStdoutNotifier) { |
206 |
delete m_pStdoutNotifier; |
delete m_pStdoutNotifier; |
207 |
m_pStdoutNotifier = NULL; |
m_pStdoutNotifier = nullptr; |
208 |
// Close the notification pipes. |
// Close the notification pipes. |
209 |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
210 |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
212 |
} |
} |
213 |
} |
} |
214 |
// Are we going to make up the capture? |
// Are we going to make up the capture? |
215 |
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
if (bCapture && m_pStdoutNotifier == nullptr && ::pipe(m_fdStdout) == 0) { |
216 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
217 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
218 |
m_pStdoutNotifier = new QSocketNotifier( |
m_pStdoutNotifier = new QSocketNotifier( |
252 |
// Messages logging stuff. |
// Messages logging stuff. |
253 |
bool Messages::isLogging (void) const |
bool Messages::isLogging (void) const |
254 |
{ |
{ |
255 |
return (m_pMessagesLog != NULL); |
return (m_pMessagesLog != nullptr); |
256 |
} |
} |
257 |
|
|
258 |
void Messages::setLogging ( bool bEnabled, const QString& sFilename ) |
void Messages::setLogging ( bool bEnabled, const QString& sFilename ) |
262 |
.arg(QDateTime::currentDateTime().toString())); |
.arg(QDateTime::currentDateTime().toString())); |
263 |
m_pMessagesLog->close(); |
m_pMessagesLog->close(); |
264 |
delete m_pMessagesLog; |
delete m_pMessagesLog; |
265 |
m_pMessagesLog = NULL; |
m_pMessagesLog = nullptr; |
266 |
} |
} |
267 |
|
|
268 |
if (bEnabled) { |
if (bEnabled) { |
272 |
.arg(QDateTime::currentDateTime().toString())); |
.arg(QDateTime::currentDateTime().toString())); |
273 |
} else { |
} else { |
274 |
delete m_pMessagesLog; |
delete m_pMessagesLog; |
275 |
m_pMessagesLog = NULL; |
m_pMessagesLog = nullptr; |
276 |
} |
} |
277 |
} |
} |
278 |
} |
} |
282 |
void Messages::appendMessagesLog ( const QString& s ) |
void Messages::appendMessagesLog ( const QString& s ) |
283 |
{ |
{ |
284 |
if (m_pMessagesLog) { |
if (m_pMessagesLog) { |
285 |
QTextStream(m_pMessagesLog) << s << endl; |
QTextStream(m_pMessagesLog) |
286 |
|
<< QTime::currentTime().toString("hh:mm:ss.zzz") |
287 |
|
<< ' ' << s << endl; |
288 |
m_pMessagesLog->flush(); |
m_pMessagesLog->flush(); |
289 |
} |
} |
290 |
} |
} |
316 |
// The main utility methods. |
// The main utility methods. |
317 |
void Messages::appendMessages ( const QString& s ) |
void Messages::appendMessages ( const QString& s ) |
318 |
{ |
{ |
319 |
appendMessagesColor(s, "#999999"); |
appendMessagesColor(s, Qt::gray); |
320 |
} |
} |
321 |
|
|
322 |
void Messages::appendMessagesColor ( const QString& s, const QString &c ) |
void Messages::appendMessagesColor ( const QString& s, const QColor& rgb ) |
323 |
{ |
{ |
324 |
const QString& sText |
appendMessagesLine("<font color=\"" + rgb.name() + "\">" + s + "</font>"); |
325 |
= QTime::currentTime().toString("hh:mm:ss.zzz") + ' ' + s; |
appendMessagesLog(s); |
|
appendMessagesLine("<font color=\"" + c + "\">" + sText + "</font>"); |
|
|
appendMessagesLog(sText); |
|
326 |
} |
} |
327 |
|
|
328 |
void Messages::appendMessagesText ( const QString& s ) |
void Messages::appendMessagesText ( const QString& s ) |