1 |
// qsamplerMessages.cpp |
// qsamplerMessages.cpp |
2 |
// |
// |
3 |
/**************************************************************************** |
/**************************************************************************** |
4 |
Copyright (C) 2004-2014, rncbc aka Rui Nuno Capela. All rights reserved. |
Copyright (C) 2004-2021, 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 |
|
|
36 |
#if !defined(WIN32) |
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
37 |
#include <unistd.h> |
#include <unistd.h> |
38 |
|
#include <fcntl.h> |
39 |
|
#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 |
#endif |
46 |
|
|
47 |
|
|
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); |
93 |
QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures); |
// QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures); |
94 |
QDockWidget::setAllowedAreas( |
QDockWidget::setAllowedAreas( |
95 |
Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); |
Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); |
96 |
// Some specialties to this kind of dock window... |
// Some specialties to this kind of dock window... |
118 |
} |
} |
119 |
|
|
120 |
|
|
121 |
|
#if defined(Q_CC_GNU) || defined(Q_CC_MINGW) |
122 |
|
#pragma GCC diagnostic push |
123 |
|
#pragma GCC diagnostic ignored "-Wunused-parameter" |
124 |
|
#endif |
125 |
|
|
126 |
|
// Set stdout/stderr blocking mode. |
127 |
|
bool Messages::stdoutBlock ( int fd, bool bBlock ) const |
128 |
|
{ |
129 |
|
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
130 |
|
const int iFlags = ::fcntl(fd, F_GETFL, 0); |
131 |
|
const bool bNonBlock = bool(iFlags & O_NONBLOCK); |
132 |
|
if (bBlock && bNonBlock) |
133 |
|
bBlock = (::fcntl(fd, F_SETFL, iFlags & ~O_NONBLOCK) == 0); |
134 |
|
else |
135 |
|
if (!bBlock && !bNonBlock) |
136 |
|
bBlock = (::fcntl(fd, F_SETFL, iFlags | O_NONBLOCK) != 0); |
137 |
|
#endif |
138 |
|
return bBlock; |
139 |
|
} |
140 |
|
|
141 |
|
|
142 |
// Own stdout/stderr socket notifier slot. |
// Own stdout/stderr socket notifier slot. |
143 |
void Messages::stdoutNotify ( int fd ) |
void Messages::stdoutNotify ( int fd ) |
144 |
{ |
{ |
145 |
#if !defined(WIN32) |
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
146 |
|
// Set non-blocking reads, if not already... |
147 |
|
const bool bBlock = stdoutBlock(fd, false); |
148 |
|
// Read as much as is available... |
149 |
|
QString sTemp; |
150 |
char achBuffer[1024]; |
char achBuffer[1024]; |
151 |
const int cchBuffer = ::read(fd, achBuffer, sizeof(achBuffer) - 1); |
const int cchBuffer = sizeof(achBuffer) - 1; |
152 |
if (cchBuffer > 0) { |
int cchRead = ::read(fd, achBuffer, cchBuffer); |
153 |
achBuffer[cchBuffer] = (char) 0; |
while (cchRead > 0) { |
154 |
appendStdoutBuffer(achBuffer); |
achBuffer[cchRead] = (char) 0; |
155 |
|
sTemp.append(achBuffer); |
156 |
|
cchRead = (bBlock ? 0 : ::read(fd, achBuffer, cchBuffer)); |
157 |
} |
} |
158 |
|
// Needs to be non-empty... |
159 |
|
if (!sTemp.isEmpty()) |
160 |
|
appendStdoutBuffer(sTemp); |
161 |
#endif |
#endif |
162 |
} |
} |
163 |
|
|
164 |
|
#if defined(Q_CC_GNU) || defined(Q_CC_MINGW) |
165 |
|
#pragma GCC diagnostic pop |
166 |
|
#endif |
167 |
|
|
168 |
|
|
169 |
// Stdout buffer handler -- now splitted by complete new-lines... |
// Stdout buffer handler -- now splitted by complete new-lines... |
170 |
void Messages::appendStdoutBuffer ( const QString& s ) |
void Messages::appendStdoutBuffer ( const QString& s ) |
171 |
{ |
{ |
172 |
m_sStdoutBuffer.append(s); |
m_sStdoutBuffer.append(s); |
173 |
|
|
174 |
|
processStdoutBuffer(); |
175 |
|
} |
176 |
|
|
177 |
|
void Messages::processStdoutBuffer (void) |
178 |
|
{ |
179 |
const int iLength = m_sStdoutBuffer.lastIndexOf('\n'); |
const int iLength = m_sStdoutBuffer.lastIndexOf('\n'); |
180 |
if (iLength > 0) { |
if (iLength > 0) { |
181 |
const QString& sTemp = m_sStdoutBuffer.left(iLength); |
const QString& sTemp = m_sStdoutBuffer.left(iLength); |
192 |
void Messages::flushStdoutBuffer (void) |
void Messages::flushStdoutBuffer (void) |
193 |
{ |
{ |
194 |
if (!m_sStdoutBuffer.isEmpty()) { |
if (!m_sStdoutBuffer.isEmpty()) { |
195 |
appendMessagesText(m_sStdoutBuffer); |
processStdoutBuffer(); |
196 |
m_sStdoutBuffer.truncate(0); |
m_sStdoutBuffer.clear(); |
197 |
} |
} |
198 |
} |
} |
199 |
|
|
201 |
// Stdout capture accessors. |
// Stdout capture accessors. |
202 |
bool Messages::isCaptureEnabled (void) |
bool Messages::isCaptureEnabled (void) |
203 |
{ |
{ |
204 |
return (m_pStdoutNotifier != NULL); |
return (m_pStdoutNotifier != nullptr); |
205 |
} |
} |
206 |
|
|
207 |
void Messages::setCaptureEnabled ( bool bCapture ) |
void Messages::setCaptureEnabled ( bool bCapture ) |
209 |
// Flush current buffer. |
// Flush current buffer. |
210 |
flushStdoutBuffer(); |
flushStdoutBuffer(); |
211 |
|
|
212 |
#if !defined(WIN32) |
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
213 |
// Destroy if already enabled. |
// Destroy if already enabled. |
214 |
if (!bCapture && m_pStdoutNotifier) { |
if (!bCapture && m_pStdoutNotifier) { |
215 |
delete m_pStdoutNotifier; |
delete m_pStdoutNotifier; |
216 |
m_pStdoutNotifier = NULL; |
m_pStdoutNotifier = nullptr; |
217 |
// Close the notification pipes. |
// Close the notification pipes. |
218 |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
219 |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
221 |
} |
} |
222 |
} |
} |
223 |
// Are we going to make up the capture? |
// Are we going to make up the capture? |
224 |
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
if (bCapture && m_pStdoutNotifier == nullptr && ::pipe(m_fdStdout) == 0) { |
225 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
226 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
227 |
m_pStdoutNotifier = new QSocketNotifier( |
m_pStdoutNotifier = new QSocketNotifier( |
261 |
// Messages logging stuff. |
// Messages logging stuff. |
262 |
bool Messages::isLogging (void) const |
bool Messages::isLogging (void) const |
263 |
{ |
{ |
264 |
return (m_pMessagesLog != NULL); |
return (m_pMessagesLog != nullptr); |
265 |
} |
} |
266 |
|
|
267 |
void Messages::setLogging ( bool bEnabled, const QString& sFilename ) |
void Messages::setLogging ( bool bEnabled, const QString& sFilename ) |
271 |
.arg(QDateTime::currentDateTime().toString())); |
.arg(QDateTime::currentDateTime().toString())); |
272 |
m_pMessagesLog->close(); |
m_pMessagesLog->close(); |
273 |
delete m_pMessagesLog; |
delete m_pMessagesLog; |
274 |
m_pMessagesLog = NULL; |
m_pMessagesLog = nullptr; |
275 |
} |
} |
276 |
|
|
277 |
if (bEnabled) { |
if (bEnabled) { |
281 |
.arg(QDateTime::currentDateTime().toString())); |
.arg(QDateTime::currentDateTime().toString())); |
282 |
} else { |
} else { |
283 |
delete m_pMessagesLog; |
delete m_pMessagesLog; |
284 |
m_pMessagesLog = NULL; |
m_pMessagesLog = nullptr; |
285 |
} |
} |
286 |
} |
} |
287 |
} |
} |
291 |
void Messages::appendMessagesLog ( const QString& s ) |
void Messages::appendMessagesLog ( const QString& s ) |
292 |
{ |
{ |
293 |
if (m_pMessagesLog) { |
if (m_pMessagesLog) { |
294 |
QTextStream(m_pMessagesLog) << s << endl; |
QTextStream(m_pMessagesLog) |
295 |
|
<< QTime::currentTime().toString("hh:mm:ss.zzz") |
296 |
|
<< ' ' << s << endl; |
297 |
m_pMessagesLog->flush(); |
m_pMessagesLog->flush(); |
298 |
} |
} |
299 |
} |
} |
325 |
// The main utility methods. |
// The main utility methods. |
326 |
void Messages::appendMessages ( const QString& s ) |
void Messages::appendMessages ( const QString& s ) |
327 |
{ |
{ |
328 |
appendMessagesColor(s, "#999999"); |
appendMessagesColor(s, Qt::gray); |
329 |
} |
} |
330 |
|
|
331 |
void Messages::appendMessagesColor ( const QString& s, const QString &c ) |
void Messages::appendMessagesColor ( const QString& s, const QColor& rgb ) |
332 |
{ |
{ |
333 |
const QString& sText |
appendMessagesLine("<font color=\"" + rgb.name() + "\">" + s + "</font>"); |
334 |
= QTime::currentTime().toString("hh:mm:ss.zzz") + ' ' + s; |
appendMessagesLog(s); |
|
appendMessagesLine("<font color=\"" + c + "\">" + sText + "</font>"); |
|
|
appendMessagesLog(sText); |
|
335 |
} |
} |
336 |
|
|
337 |
void Messages::appendMessagesText ( const QString& s ) |
void Messages::appendMessagesText ( const QString& s ) |