1 |
// qsamplerMessages.cpp |
// qsamplerMessages.cpp |
2 |
// |
// |
3 |
/**************************************************************************** |
/**************************************************************************** |
4 |
Copyright (C) 2004-2014, 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 |
34 |
#include <QDateTime> |
#include <QDateTime> |
35 |
#include <QIcon> |
#include <QIcon> |
36 |
|
|
37 |
#if !defined(WIN32) |
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
38 |
#include <unistd.h> |
#include <unistd.h> |
39 |
|
#include <fcntl.h> |
40 |
|
#endif |
41 |
|
|
42 |
|
|
43 |
|
// Deprecated QTextStreamFunctions/Qt namespaces workaround. |
44 |
|
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0) |
45 |
|
#define endl Qt::endl |
46 |
#endif |
#endif |
47 |
|
|
48 |
|
|
69 |
QDockWidget::setObjectName("qsamplerMessages"); |
QDockWidget::setObjectName("qsamplerMessages"); |
70 |
|
|
71 |
// Intialize stdout capture stuff. |
// Intialize stdout capture stuff. |
72 |
m_pStdoutNotifier = NULL; |
m_pStdoutNotifier = nullptr; |
73 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
74 |
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
75 |
|
|
87 |
m_iMessagesLines = 0; |
m_iMessagesLines = 0; |
88 |
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
89 |
|
|
90 |
m_pMessagesLog = NULL; |
m_pMessagesLog = nullptr; |
91 |
|
|
92 |
// Prepare the dockable window stuff. |
// Prepare the dockable window stuff. |
93 |
QDockWidget::setWidget(m_pMessagesTextView); |
QDockWidget::setWidget(m_pMessagesTextView); |
119 |
} |
} |
120 |
|
|
121 |
|
|
122 |
|
// Set stdout/stderr blocking mode. |
123 |
|
bool Messages::stdoutBlock ( int fd, bool bBlock ) const |
124 |
|
{ |
125 |
|
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
126 |
|
const int iFlags = ::fcntl(fd, F_GETFL, 0); |
127 |
|
const bool bNonBlock = bool(iFlags & O_NONBLOCK); |
128 |
|
if (bBlock && bNonBlock) |
129 |
|
bBlock = (::fcntl(fd, F_SETFL, iFlags & ~O_NONBLOCK) == 0); |
130 |
|
else |
131 |
|
if (!bBlock && !bNonBlock) |
132 |
|
bBlock = (::fcntl(fd, F_SETFL, iFlags | O_NONBLOCK) != 0); |
133 |
|
#endif |
134 |
|
return bBlock; |
135 |
|
} |
136 |
|
|
137 |
|
|
138 |
// Own stdout/stderr socket notifier slot. |
// Own stdout/stderr socket notifier slot. |
139 |
void Messages::stdoutNotify ( int fd ) |
void Messages::stdoutNotify ( int fd ) |
140 |
{ |
{ |
141 |
#if !defined(WIN32) |
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
142 |
|
// Set non-blocking reads, if not already... |
143 |
|
const bool bBlock = stdoutBlock(fd, false); |
144 |
|
// Read as much as is available... |
145 |
|
QString sTemp; |
146 |
char achBuffer[1024]; |
char achBuffer[1024]; |
147 |
const int cchBuffer = ::read(fd, achBuffer, sizeof(achBuffer) - 1); |
const int cchBuffer = sizeof(achBuffer) - 1; |
148 |
if (cchBuffer > 0) { |
int cchRead = ::read(fd, achBuffer, cchBuffer); |
149 |
achBuffer[cchBuffer] = (char) 0; |
while (cchRead > 0) { |
150 |
appendStdoutBuffer(achBuffer); |
achBuffer[cchRead] = (char) 0; |
151 |
|
sTemp.append(achBuffer); |
152 |
|
cchRead = (bBlock ? 0 : ::read(fd, achBuffer, cchBuffer)); |
153 |
} |
} |
154 |
|
// Needs to be non-empty... |
155 |
|
if (!sTemp.isEmpty()) |
156 |
|
appendStdoutBuffer(sTemp); |
157 |
#endif |
#endif |
158 |
} |
} |
159 |
|
|
180 |
{ |
{ |
181 |
if (!m_sStdoutBuffer.isEmpty()) { |
if (!m_sStdoutBuffer.isEmpty()) { |
182 |
appendMessagesText(m_sStdoutBuffer); |
appendMessagesText(m_sStdoutBuffer); |
183 |
m_sStdoutBuffer.truncate(0); |
m_sStdoutBuffer.clear(); |
184 |
} |
} |
185 |
} |
} |
186 |
|
|
188 |
// Stdout capture accessors. |
// Stdout capture accessors. |
189 |
bool Messages::isCaptureEnabled (void) |
bool Messages::isCaptureEnabled (void) |
190 |
{ |
{ |
191 |
return (m_pStdoutNotifier != NULL); |
return (m_pStdoutNotifier != nullptr); |
192 |
} |
} |
193 |
|
|
194 |
void Messages::setCaptureEnabled ( bool bCapture ) |
void Messages::setCaptureEnabled ( bool bCapture ) |
196 |
// Flush current buffer. |
// Flush current buffer. |
197 |
flushStdoutBuffer(); |
flushStdoutBuffer(); |
198 |
|
|
199 |
#if !defined(WIN32) |
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32) |
200 |
// Destroy if already enabled. |
// Destroy if already enabled. |
201 |
if (!bCapture && m_pStdoutNotifier) { |
if (!bCapture && m_pStdoutNotifier) { |
202 |
delete m_pStdoutNotifier; |
delete m_pStdoutNotifier; |
203 |
m_pStdoutNotifier = NULL; |
m_pStdoutNotifier = nullptr; |
204 |
// Close the notification pipes. |
// Close the notification pipes. |
205 |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
206 |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
208 |
} |
} |
209 |
} |
} |
210 |
// Are we going to make up the capture? |
// Are we going to make up the capture? |
211 |
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
if (bCapture && m_pStdoutNotifier == nullptr && ::pipe(m_fdStdout) == 0) { |
212 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
213 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
214 |
m_pStdoutNotifier = new QSocketNotifier( |
m_pStdoutNotifier = new QSocketNotifier( |
248 |
// Messages logging stuff. |
// Messages logging stuff. |
249 |
bool Messages::isLogging (void) const |
bool Messages::isLogging (void) const |
250 |
{ |
{ |
251 |
return (m_pMessagesLog != NULL); |
return (m_pMessagesLog != nullptr); |
252 |
} |
} |
253 |
|
|
254 |
void Messages::setLogging ( bool bEnabled, const QString& sFilename ) |
void Messages::setLogging ( bool bEnabled, const QString& sFilename ) |
258 |
.arg(QDateTime::currentDateTime().toString())); |
.arg(QDateTime::currentDateTime().toString())); |
259 |
m_pMessagesLog->close(); |
m_pMessagesLog->close(); |
260 |
delete m_pMessagesLog; |
delete m_pMessagesLog; |
261 |
m_pMessagesLog = NULL; |
m_pMessagesLog = nullptr; |
262 |
} |
} |
263 |
|
|
264 |
if (bEnabled) { |
if (bEnabled) { |
268 |
.arg(QDateTime::currentDateTime().toString())); |
.arg(QDateTime::currentDateTime().toString())); |
269 |
} else { |
} else { |
270 |
delete m_pMessagesLog; |
delete m_pMessagesLog; |
271 |
m_pMessagesLog = NULL; |
m_pMessagesLog = nullptr; |
272 |
} |
} |
273 |
} |
} |
274 |
} |
} |