1 |
// qsamplerMessages.cpp |
// qsamplerMessages.cpp |
2 |
// |
// |
3 |
/**************************************************************************** |
/**************************************************************************** |
4 |
Copyright (C) 2003-2005, rncbc aka Rui Nuno Capela. All rights reserved. |
Copyright (C) 2004-2008, rncbc aka Rui Nuno Capela. All rights reserved. |
5 |
|
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 |
8 |
modify it under the terms of the GNU General Public License |
modify it under the terms of the GNU General Public License |
14 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
GNU General Public License for more details. |
GNU General Public License for more details. |
16 |
|
|
17 |
You should have received a copy of the GNU General Public License |
You should have received a copy of the GNU General Public License along |
18 |
along with this program; if not, write to the Free Software |
with this program; if not, write to the Free Software Foundation, Inc., |
19 |
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20 |
|
|
21 |
*****************************************************************************/ |
*****************************************************************************/ |
22 |
|
|
23 |
|
#include "qsamplerAbout.h" |
24 |
#include "qsamplerMessages.h" |
#include "qsamplerMessages.h" |
25 |
|
|
26 |
#include <qsocketnotifier.h> |
#include <QSocketNotifier> |
|
#include <qstringlist.h> |
|
|
#include <qtextedit.h> |
|
|
#include <qdatetime.h> |
|
|
#include <qtooltip.h> |
|
|
#include <qpixmap.h> |
|
27 |
|
|
28 |
#include "config.h" |
#include <QFile> |
29 |
|
#include <QTextEdit> |
30 |
|
#include <QTextCursor> |
31 |
|
#include <QTextStream> |
32 |
|
#include <QTextBlock> |
33 |
|
#include <QScrollBar> |
34 |
|
#include <QDateTime> |
35 |
|
#include <QIcon> |
36 |
|
|
37 |
#if !defined(WIN32) |
#if !defined(WIN32) |
38 |
#include <unistd.h> |
#include <unistd.h> |
39 |
#endif |
#endif |
40 |
|
|
41 |
|
|
42 |
|
namespace QSampler { |
43 |
|
|
44 |
// The default maximum number of message lines. |
// The default maximum number of message lines. |
45 |
#define QSAMPLER_MESSAGES_MAXLINES 1000 |
#define QSAMPLER_MESSAGES_MAXLINES 1000 |
46 |
|
|
49 |
#define QSAMPLER_MESSAGES_FDREAD 0 |
#define QSAMPLER_MESSAGES_FDREAD 0 |
50 |
#define QSAMPLER_MESSAGES_FDWRITE 1 |
#define QSAMPLER_MESSAGES_FDWRITE 1 |
51 |
|
|
|
|
|
52 |
//------------------------------------------------------------------------- |
//------------------------------------------------------------------------- |
53 |
// qsamplerMessages - Messages log dockable window. |
// QSampler::Messages - Messages log dockable window. |
54 |
// |
// |
55 |
|
|
56 |
// Constructor. |
// Constructor. |
57 |
qsamplerMessages::qsamplerMessages ( QWidget *pParent, const char *pszName ) |
Messages::Messages ( QWidget *pParent ) |
58 |
: QDockWindow(pParent, pszName) |
: QDockWidget(pParent) |
59 |
{ |
{ |
60 |
#if QT_VERSION >= 0x030200 |
// Surely a name is crucial (e.g.for storing geometry settings) |
61 |
m_pTextView->setTextFormat(Qt::LogText); |
QDockWidget::setObjectName("qsamplerMessages"); |
|
#endif |
|
|
// Initialize default message limit. |
|
|
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
|
62 |
|
|
63 |
// Intialize stdout capture stuff. |
// Intialize stdout capture stuff. |
64 |
m_pStdoutNotifier = NULL; |
m_pStdoutNotifier = NULL; |
65 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
66 |
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
67 |
|
|
68 |
// Surely a name is crucial (e.g.for storing geometry settings) |
// Create local text view widget. |
69 |
if (pszName == 0) |
m_pMessagesTextView = new QTextEdit(this); |
70 |
QDockWindow::setName("qsamplerMessages"); |
// QFont font(m_pMessagesTextView->font()); |
|
|
|
|
// Create local text view widget. |
|
|
m_pTextView = new QTextEdit(this); |
|
|
// QFont font(m_pTextView->font()); |
|
71 |
// font.setFamily("Fixed"); |
// font.setFamily("Fixed"); |
72 |
// m_pTextView->setFont(font); |
// m_pMessagesTextView->setFont(font); |
73 |
m_pTextView->setWordWrap(QTextEdit::NoWrap); |
m_pMessagesTextView->setLineWrapMode(QTextEdit::NoWrap); |
74 |
m_pTextView->setReadOnly(true); |
m_pMessagesTextView->setReadOnly(true); |
75 |
m_pTextView->setUndoRedoEnabled(false); |
m_pMessagesTextView->setUndoRedoEnabled(false); |
76 |
|
// m_pMessagesTextView->setTextFormat(Qt::LogText); |
77 |
// Prepare the dockable window stuff. |
|
78 |
QDockWindow::setWidget(m_pTextView); |
// Initialize default message limit. |
79 |
QDockWindow::setOrientation(Qt::Horizontal); |
m_iMessagesLines = 0; |
80 |
QDockWindow::setCloseMode(QDockWindow::Always); |
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
81 |
QDockWindow::setResizeEnabled(true); |
|
82 |
|
m_pMessagesLog = NULL; |
83 |
// Finally set the default caption and tooltip. |
|
84 |
QString sCaption = tr("Messages"); |
// Prepare the dockable window stuff. |
85 |
QToolTip::add(this, sCaption); |
QDockWidget::setWidget(m_pMessagesTextView); |
86 |
QDockWindow::setCaption(sCaption); |
QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures); |
87 |
|
QDockWidget::setAllowedAreas( |
88 |
|
Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); |
89 |
|
// Some specialties to this kind of dock window... |
90 |
|
QDockWidget::setMinimumHeight(120); |
91 |
|
|
92 |
|
// Finally set the default caption and tooltip. |
93 |
|
const QString& sCaption = tr("Messages"); |
94 |
|
QDockWidget::setWindowTitle(sCaption); |
95 |
|
// QDockWidget::setWindowIcon(QIcon(":/icons/qsamplerMessages.png")); |
96 |
|
QDockWidget::setToolTip(sCaption); |
97 |
} |
} |
98 |
|
|
99 |
|
|
100 |
// Destructor. |
// Destructor. |
101 |
qsamplerMessages::~qsamplerMessages (void) |
Messages::~Messages (void) |
102 |
{ |
{ |
103 |
|
setLogging(false); |
104 |
|
|
105 |
// No more notifications. |
// No more notifications. |
106 |
if (m_pStdoutNotifier) |
if (m_pStdoutNotifier) |
107 |
delete m_pStdoutNotifier; |
delete m_pStdoutNotifier; |
110 |
} |
} |
111 |
|
|
112 |
|
|
113 |
|
void Messages::showEvent ( QShowEvent *pEvent ) |
114 |
|
{ |
115 |
|
QDockWidget::showEvent(pEvent); |
116 |
|
emit visibilityChanged(isVisible()); |
117 |
|
} |
118 |
|
|
119 |
|
|
120 |
// Own stdout/stderr socket notifier slot. |
// Own stdout/stderr socket notifier slot. |
121 |
void qsamplerMessages::stdoutNotify ( int fd ) |
void Messages::stdoutNotify ( int fd ) |
122 |
{ |
{ |
123 |
#if !defined(WIN32) |
#if !defined(WIN32) |
124 |
char achBuffer[1024]; |
char achBuffer[1024]; |
132 |
|
|
133 |
|
|
134 |
// Stdout buffer handler -- now splitted by complete new-lines... |
// Stdout buffer handler -- now splitted by complete new-lines... |
135 |
void qsamplerMessages::appendStdoutBuffer ( const QString& s ) |
void Messages::appendStdoutBuffer ( const QString& s ) |
136 |
{ |
{ |
137 |
m_sStdoutBuffer.append(s); |
m_sStdoutBuffer.append(s); |
138 |
|
|
139 |
int iLength = m_sStdoutBuffer.findRev('\n') + 1; |
int iLength = m_sStdoutBuffer.lastIndexOf('\n'); |
140 |
if (iLength > 0) { |
if (iLength > 0) { |
141 |
QString sTemp = m_sStdoutBuffer.left(iLength); |
QString sTemp = m_sStdoutBuffer.left(iLength); |
142 |
m_sStdoutBuffer.remove(0, iLength); |
m_sStdoutBuffer.remove(0, iLength + 1); |
143 |
QStringList list = QStringList::split('\n', sTemp, true); |
QStringList list = sTemp.split('\n'); |
144 |
for (QStringList::Iterator iter = list.begin(); iter != list.end(); iter++) |
QStringListIterator iter(list); |
145 |
appendMessagesText(*iter); |
while (iter.hasNext()) |
146 |
} |
appendMessagesText(iter.next()); |
147 |
|
} |
148 |
} |
} |
149 |
|
|
150 |
|
|
151 |
// Stdout flusher -- show up any unfinished line... |
// Stdout flusher -- show up any unfinished line... |
152 |
void qsamplerMessages::flushStdoutBuffer (void) |
void Messages::flushStdoutBuffer (void) |
153 |
{ |
{ |
154 |
if (!m_sStdoutBuffer.isEmpty()) { |
if (!m_sStdoutBuffer.isEmpty()) { |
155 |
appendMessagesText(m_sStdoutBuffer); |
appendMessagesText(m_sStdoutBuffer); |
159 |
|
|
160 |
|
|
161 |
// Stdout capture accessors. |
// Stdout capture accessors. |
162 |
bool qsamplerMessages::isCaptureEnabled (void) |
bool Messages::isCaptureEnabled (void) |
163 |
{ |
{ |
164 |
return (bool) (m_pStdoutNotifier != NULL); |
return (bool) (m_pStdoutNotifier != NULL); |
165 |
} |
} |
166 |
|
|
167 |
void qsamplerMessages::setCaptureEnabled ( bool bCapture ) |
void Messages::setCaptureEnabled ( bool bCapture ) |
168 |
{ |
{ |
169 |
// Flush current buffer. |
// Flush current buffer. |
170 |
flushStdoutBuffer(); |
flushStdoutBuffer(); |
171 |
|
|
172 |
#if !defined(WIN32) |
#if !defined(WIN32) |
173 |
// Destroy if already enabled. |
// Destroy if already enabled. |
174 |
if (!bCapture && m_pStdoutNotifier) { |
if (!bCapture && m_pStdoutNotifier) { |
175 |
delete m_pStdoutNotifier; |
delete m_pStdoutNotifier; |
176 |
m_pStdoutNotifier = NULL; |
m_pStdoutNotifier = NULL; |
177 |
// Close the notification pipes. |
// Close the notification pipes. |
178 |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
179 |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
180 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
181 |
} |
} |
182 |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
183 |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
184 |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
185 |
} |
} |
186 |
} |
} |
187 |
// Are we going to make up the capture? |
// Are we going to make up the capture? |
188 |
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
189 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
190 |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
191 |
m_pStdoutNotifier = new QSocketNotifier(m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this); |
m_pStdoutNotifier = new QSocketNotifier( |
192 |
QObject::connect(m_pStdoutNotifier, SIGNAL(activated(int)), this, SLOT(stdoutNotify(int))); |
m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this); |
193 |
} |
QObject::connect(m_pStdoutNotifier, |
194 |
|
SIGNAL(activated(int)), |
195 |
|
SLOT(stdoutNotify(int))); |
196 |
|
} |
197 |
#endif |
#endif |
198 |
} |
} |
199 |
|
|
200 |
|
|
201 |
// Message font accessors. |
// Message font accessors. |
202 |
QFont qsamplerMessages::messagesFont (void) |
QFont Messages::messagesFont (void) |
203 |
{ |
{ |
204 |
return m_pTextView->font(); |
return m_pMessagesTextView->font(); |
205 |
} |
} |
206 |
|
|
207 |
void qsamplerMessages::setMessagesFont( const QFont& font ) |
void Messages::setMessagesFont ( const QFont& font ) |
208 |
{ |
{ |
209 |
m_pTextView->setFont(font); |
m_pMessagesTextView->setFont(font); |
210 |
} |
} |
211 |
|
|
212 |
|
|
213 |
// Maximum number of message lines accessors. |
// Maximum number of message lines accessors. |
214 |
int qsamplerMessages::messagesLimit (void) |
int Messages::messagesLimit (void) |
215 |
{ |
{ |
216 |
return m_iMessagesLimit; |
return m_iMessagesLimit; |
217 |
} |
} |
218 |
|
|
219 |
void qsamplerMessages::setMessagesLimit ( int iMessagesLimit ) |
void Messages::setMessagesLimit ( int iMessagesLimit ) |
220 |
{ |
{ |
221 |
m_iMessagesLimit = iMessagesLimit; |
m_iMessagesLimit = iMessagesLimit; |
222 |
m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3); |
m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3); |
223 |
#if QT_VERSION >= 0x030200 |
} |
224 |
m_pTextView->setMaxLogLines(iMessagesLimit); |
|
225 |
#endif |
// Messages logging stuff. |
226 |
|
bool Messages::isLogging (void) const |
227 |
|
{ |
228 |
|
return (m_pMessagesLog != NULL); |
229 |
|
} |
230 |
|
|
231 |
|
void Messages::setLogging ( bool bEnabled, const QString& sFilename ) |
232 |
|
{ |
233 |
|
if (m_pMessagesLog) { |
234 |
|
appendMessages(tr("Logging stopped --- %1 ---") |
235 |
|
.arg(QDateTime::currentDateTime().toString())); |
236 |
|
m_pMessagesLog->close(); |
237 |
|
delete m_pMessagesLog; |
238 |
|
m_pMessagesLog = NULL; |
239 |
|
} |
240 |
|
|
241 |
|
if (bEnabled) { |
242 |
|
m_pMessagesLog = new QFile(sFilename); |
243 |
|
if (m_pMessagesLog->open(QIODevice::Text | QIODevice::Append)) { |
244 |
|
appendMessages(tr("Logging started --- %1 ---") |
245 |
|
.arg(QDateTime::currentDateTime().toString())); |
246 |
|
} else { |
247 |
|
delete m_pMessagesLog; |
248 |
|
m_pMessagesLog = NULL; |
249 |
|
} |
250 |
|
} |
251 |
|
} |
252 |
|
|
253 |
|
|
254 |
|
// Messages log output method. |
255 |
|
void Messages::appendMessagesLog ( const QString& s ) |
256 |
|
{ |
257 |
|
if (m_pMessagesLog) { |
258 |
|
QTextStream(m_pMessagesLog) << s << endl; |
259 |
|
m_pMessagesLog->flush(); |
260 |
|
} |
261 |
|
} |
262 |
|
|
263 |
|
// Messages widget output method. |
264 |
|
void Messages::appendMessagesLine ( const QString& s ) |
265 |
|
{ |
266 |
|
// Check for message line limit... |
267 |
|
if (m_iMessagesLines > m_iMessagesHigh) { |
268 |
|
m_pMessagesTextView->setUpdatesEnabled(false); |
269 |
|
QTextCursor textCursor(m_pMessagesTextView->document()->begin()); |
270 |
|
while (m_iMessagesLines > m_iMessagesLimit) { |
271 |
|
// Move cursor extending selection |
272 |
|
// from start to next line-block... |
273 |
|
textCursor.movePosition( |
274 |
|
QTextCursor::NextBlock, QTextCursor::KeepAnchor); |
275 |
|
m_iMessagesLines--; |
276 |
|
} |
277 |
|
// Remove the excessive line-blocks... |
278 |
|
textCursor.removeSelectedText(); |
279 |
|
m_pMessagesTextView->setUpdatesEnabled(true); |
280 |
|
} |
281 |
|
|
282 |
|
m_pMessagesTextView->append(s); |
283 |
|
m_iMessagesLines++; |
284 |
} |
} |
285 |
|
|
286 |
|
|
287 |
// The main utility methods. |
// The main utility methods. |
288 |
void qsamplerMessages::appendMessages ( const QString& s ) |
void Messages::appendMessages ( const QString& s ) |
289 |
{ |
{ |
290 |
appendMessagesColor(s, "#999999"); |
appendMessagesColor(s, "#999999"); |
291 |
} |
} |
292 |
|
|
293 |
void qsamplerMessages::appendMessagesColor ( const QString& s, const QString &c ) |
void Messages::appendMessagesColor ( const QString& s, const QString &c ) |
294 |
{ |
{ |
295 |
appendMessagesText("<font color=\"" + c + "\">" + QTime::currentTime().toString("hh:mm:ss.zzz") + " " + s + "</font>"); |
QString sText = QTime::currentTime().toString("hh:mm:ss.zzz") + ' ' + s; |
296 |
|
|
297 |
|
appendMessagesLine("<font color=\"" + c + "\">" + sText + "</font>"); |
298 |
|
appendMessagesLog(sText); |
299 |
} |
} |
300 |
|
|
301 |
void qsamplerMessages::appendMessagesText ( const QString& s ) |
void Messages::appendMessagesText ( const QString& s ) |
302 |
{ |
{ |
303 |
#if QT_VERSION < 0x030200 |
appendMessagesLine(s); |
304 |
// Check for message line limit... |
appendMessagesLog(s); |
|
if (m_iMessagesLimit > 0) { |
|
|
int iParagraphs = m_pTextView->paragraphs(); |
|
|
if (iParagraphs > m_iMessagesHigh) { |
|
|
m_pTextView->setUpdatesEnabled(false); |
|
|
while (iParagraphs > m_iMessagesLimit) { |
|
|
m_pTextView->removeParagraph(0); |
|
|
iParagraphs--; |
|
|
} |
|
|
m_pTextView->scrollToBottom(); |
|
|
m_pTextView->setUpdatesEnabled(true); |
|
|
} |
|
|
} |
|
|
#endif |
|
|
m_pTextView->append(s); |
|
305 |
} |
} |
306 |
|
|
307 |
|
|
308 |
// One time scroll to the most recent message. |
// History reset. |
309 |
void qsamplerMessages::scrollToBottom (void) |
void Messages::clear (void) |
310 |
{ |
{ |
311 |
flushStdoutBuffer(); |
m_iMessagesLines = 0; |
312 |
m_pTextView->scrollToBottom(); |
m_pMessagesTextView->clear(); |
313 |
} |
} |
314 |
|
|
315 |
|
} // namespace QSampler |
316 |
|
|
317 |
|
|
318 |
// end of qsamplerMessages.cpp |
// end of qsamplerMessages.cpp |