1 |
capela |
94 |
// qsamplerMessages.cpp |
2 |
|
|
// |
3 |
|
|
/**************************************************************************** |
4 |
capela |
2441 |
Copyright (C) 2004-2013, 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 |
1465 |
#include <QTextEdit> |
30 |
|
|
#include <QTextCursor> |
31 |
capela |
1738 |
#include <QTextStream> |
32 |
capela |
1465 |
#include <QTextBlock> |
33 |
schoenebeck |
1461 |
#include <QScrollBar> |
34 |
capela |
1465 |
#include <QDateTime> |
35 |
|
|
#include <QIcon> |
36 |
schoenebeck |
1461 |
|
37 |
capela |
115 |
#if !defined(WIN32) |
38 |
|
|
#include <unistd.h> |
39 |
|
|
#endif |
40 |
|
|
|
41 |
capela |
1558 |
|
42 |
|
|
namespace QSampler { |
43 |
|
|
|
44 |
capela |
94 |
// The default maximum number of message lines. |
45 |
|
|
#define QSAMPLER_MESSAGES_MAXLINES 1000 |
46 |
|
|
|
47 |
|
|
// Notification pipe descriptors |
48 |
|
|
#define QSAMPLER_MESSAGES_FDNIL -1 |
49 |
|
|
#define QSAMPLER_MESSAGES_FDREAD 0 |
50 |
|
|
#define QSAMPLER_MESSAGES_FDWRITE 1 |
51 |
|
|
|
52 |
|
|
//------------------------------------------------------------------------- |
53 |
capela |
1558 |
// QSampler::Messages - Messages log dockable window. |
54 |
capela |
94 |
// |
55 |
|
|
|
56 |
|
|
// Constructor. |
57 |
capela |
1558 |
Messages::Messages ( QWidget *pParent ) |
58 |
capela |
1739 |
: QDockWidget(pParent) |
59 |
capela |
94 |
{ |
60 |
capela |
1465 |
// Surely a name is crucial (e.g.for storing geometry settings) |
61 |
|
|
QDockWidget::setObjectName("qsamplerMessages"); |
62 |
|
|
|
63 |
capela |
1739 |
// Intialize stdout capture stuff. |
64 |
|
|
m_pStdoutNotifier = NULL; |
65 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
66 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL; |
67 |
capela |
94 |
|
68 |
capela |
1465 |
// Create local text view widget. |
69 |
capela |
1738 |
m_pMessagesTextView = new QTextEdit(this); |
70 |
|
|
// QFont font(m_pMessagesTextView->font()); |
71 |
capela |
94 |
// font.setFamily("Fixed"); |
72 |
capela |
1738 |
// m_pMessagesTextView->setFont(font); |
73 |
|
|
m_pMessagesTextView->setLineWrapMode(QTextEdit::NoWrap); |
74 |
|
|
m_pMessagesTextView->setReadOnly(true); |
75 |
|
|
m_pMessagesTextView->setUndoRedoEnabled(false); |
76 |
|
|
// m_pMessagesTextView->setTextFormat(Qt::LogText); |
77 |
capela |
94 |
|
78 |
capela |
1465 |
// Initialize default message limit. |
79 |
|
|
m_iMessagesLines = 0; |
80 |
|
|
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES); |
81 |
|
|
|
82 |
capela |
1738 |
m_pMessagesLog = NULL; |
83 |
|
|
|
84 |
capela |
1465 |
// Prepare the dockable window stuff. |
85 |
capela |
1738 |
QDockWidget::setWidget(m_pMessagesTextView); |
86 |
capela |
1465 |
QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures); |
87 |
|
|
QDockWidget::setAllowedAreas( |
88 |
|
|
Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea); |
89 |
capela |
772 |
// Some specialties to this kind of dock window... |
90 |
capela |
1465 |
QDockWidget::setMinimumHeight(120); |
91 |
capela |
94 |
|
92 |
capela |
1465 |
// 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 |
capela |
94 |
} |
98 |
|
|
|
99 |
|
|
|
100 |
|
|
// Destructor. |
101 |
capela |
1558 |
Messages::~Messages (void) |
102 |
capela |
94 |
{ |
103 |
capela |
1739 |
// Turn off and close logging. |
104 |
capela |
1738 |
setLogging(false); |
105 |
|
|
|
106 |
capela |
1739 |
// No more notifications. |
107 |
|
|
if (m_pStdoutNotifier) |
108 |
|
|
delete m_pStdoutNotifier; |
109 |
capela |
94 |
|
110 |
capela |
1739 |
// No need to delete child widgets, Qt does it all for us. |
111 |
capela |
94 |
} |
112 |
|
|
|
113 |
|
|
|
114 |
|
|
// Own stdout/stderr socket notifier slot. |
115 |
capela |
1558 |
void Messages::stdoutNotify ( int fd ) |
116 |
capela |
94 |
{ |
117 |
|
|
#if !defined(WIN32) |
118 |
capela |
1739 |
char achBuffer[1024]; |
119 |
|
|
int cchBuffer = ::read(fd, achBuffer, sizeof(achBuffer) - 1); |
120 |
|
|
if (cchBuffer > 0) { |
121 |
|
|
achBuffer[cchBuffer] = (char) 0; |
122 |
|
|
appendStdoutBuffer(achBuffer); |
123 |
|
|
} |
124 |
capela |
94 |
#endif |
125 |
|
|
} |
126 |
|
|
|
127 |
|
|
|
128 |
|
|
// Stdout buffer handler -- now splitted by complete new-lines... |
129 |
capela |
1558 |
void Messages::appendStdoutBuffer ( const QString& s ) |
130 |
capela |
94 |
{ |
131 |
capela |
1465 |
m_sStdoutBuffer.append(s); |
132 |
capela |
94 |
|
133 |
schoenebeck |
1474 |
int iLength = m_sStdoutBuffer.lastIndexOf('\n'); |
134 |
capela |
1465 |
if (iLength > 0) { |
135 |
|
|
QString sTemp = m_sStdoutBuffer.left(iLength); |
136 |
schoenebeck |
1474 |
m_sStdoutBuffer.remove(0, iLength + 1); |
137 |
capela |
1465 |
QStringList list = sTemp.split('\n'); |
138 |
|
|
QStringListIterator iter(list); |
139 |
|
|
while (iter.hasNext()) |
140 |
|
|
appendMessagesText(iter.next()); |
141 |
|
|
} |
142 |
capela |
94 |
} |
143 |
|
|
|
144 |
|
|
|
145 |
|
|
// Stdout flusher -- show up any unfinished line... |
146 |
capela |
1558 |
void Messages::flushStdoutBuffer (void) |
147 |
capela |
94 |
{ |
148 |
capela |
1739 |
if (!m_sStdoutBuffer.isEmpty()) { |
149 |
|
|
appendMessagesText(m_sStdoutBuffer); |
150 |
|
|
m_sStdoutBuffer.truncate(0); |
151 |
|
|
} |
152 |
capela |
94 |
} |
153 |
|
|
|
154 |
|
|
|
155 |
|
|
// Stdout capture accessors. |
156 |
capela |
1558 |
bool Messages::isCaptureEnabled (void) |
157 |
capela |
94 |
{ |
158 |
capela |
1739 |
return (bool) (m_pStdoutNotifier != NULL); |
159 |
capela |
94 |
} |
160 |
|
|
|
161 |
capela |
1558 |
void Messages::setCaptureEnabled ( bool bCapture ) |
162 |
capela |
94 |
{ |
163 |
capela |
1739 |
// Flush current buffer. |
164 |
|
|
flushStdoutBuffer(); |
165 |
capela |
94 |
|
166 |
|
|
#if !defined(WIN32) |
167 |
capela |
1465 |
// Destroy if already enabled. |
168 |
|
|
if (!bCapture && m_pStdoutNotifier) { |
169 |
|
|
delete m_pStdoutNotifier; |
170 |
|
|
m_pStdoutNotifier = NULL; |
171 |
|
|
// Close the notification pipes. |
172 |
|
|
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
173 |
|
|
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
174 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
175 |
|
|
} |
176 |
|
|
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) { |
177 |
|
|
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]); |
178 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL; |
179 |
|
|
} |
180 |
|
|
} |
181 |
|
|
// Are we going to make up the capture? |
182 |
|
|
if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) { |
183 |
|
|
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO); |
184 |
|
|
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO); |
185 |
|
|
m_pStdoutNotifier = new QSocketNotifier( |
186 |
|
|
m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this); |
187 |
|
|
QObject::connect(m_pStdoutNotifier, |
188 |
|
|
SIGNAL(activated(int)), |
189 |
|
|
SLOT(stdoutNotify(int))); |
190 |
|
|
} |
191 |
capela |
94 |
#endif |
192 |
|
|
} |
193 |
|
|
|
194 |
|
|
|
195 |
|
|
// Message font accessors. |
196 |
capela |
1558 |
QFont Messages::messagesFont (void) |
197 |
capela |
94 |
{ |
198 |
capela |
1739 |
return m_pMessagesTextView->font(); |
199 |
capela |
94 |
} |
200 |
|
|
|
201 |
capela |
1738 |
void Messages::setMessagesFont ( const QFont& font ) |
202 |
capela |
94 |
{ |
203 |
capela |
1739 |
m_pMessagesTextView->setFont(font); |
204 |
capela |
94 |
} |
205 |
|
|
|
206 |
|
|
|
207 |
|
|
// Maximum number of message lines accessors. |
208 |
capela |
1558 |
int Messages::messagesLimit (void) |
209 |
capela |
94 |
{ |
210 |
capela |
1739 |
return m_iMessagesLimit; |
211 |
capela |
94 |
} |
212 |
|
|
|
213 |
capela |
1558 |
void Messages::setMessagesLimit ( int iMessagesLimit ) |
214 |
capela |
94 |
{ |
215 |
capela |
1739 |
m_iMessagesLimit = iMessagesLimit; |
216 |
|
|
m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3); |
217 |
capela |
94 |
} |
218 |
|
|
|
219 |
capela |
1738 |
// Messages logging stuff. |
220 |
|
|
bool Messages::isLogging (void) const |
221 |
|
|
{ |
222 |
|
|
return (m_pMessagesLog != NULL); |
223 |
|
|
} |
224 |
capela |
94 |
|
225 |
capela |
1738 |
void Messages::setLogging ( bool bEnabled, const QString& sFilename ) |
226 |
capela |
94 |
{ |
227 |
capela |
1738 |
if (m_pMessagesLog) { |
228 |
|
|
appendMessages(tr("Logging stopped --- %1 ---") |
229 |
|
|
.arg(QDateTime::currentDateTime().toString())); |
230 |
|
|
m_pMessagesLog->close(); |
231 |
|
|
delete m_pMessagesLog; |
232 |
|
|
m_pMessagesLog = NULL; |
233 |
|
|
} |
234 |
|
|
|
235 |
|
|
if (bEnabled) { |
236 |
|
|
m_pMessagesLog = new QFile(sFilename); |
237 |
|
|
if (m_pMessagesLog->open(QIODevice::Text | QIODevice::Append)) { |
238 |
|
|
appendMessages(tr("Logging started --- %1 ---") |
239 |
|
|
.arg(QDateTime::currentDateTime().toString())); |
240 |
|
|
} else { |
241 |
|
|
delete m_pMessagesLog; |
242 |
|
|
m_pMessagesLog = NULL; |
243 |
|
|
} |
244 |
|
|
} |
245 |
capela |
94 |
} |
246 |
|
|
|
247 |
capela |
1738 |
|
248 |
|
|
// Messages log output method. |
249 |
|
|
void Messages::appendMessagesLog ( const QString& s ) |
250 |
capela |
94 |
{ |
251 |
capela |
1738 |
if (m_pMessagesLog) { |
252 |
|
|
QTextStream(m_pMessagesLog) << s << endl; |
253 |
|
|
m_pMessagesLog->flush(); |
254 |
|
|
} |
255 |
capela |
94 |
} |
256 |
|
|
|
257 |
capela |
1738 |
// Messages widget output method. |
258 |
|
|
void Messages::appendMessagesLine ( const QString& s ) |
259 |
capela |
94 |
{ |
260 |
capela |
1738 |
// Check for message line limit... |
261 |
|
|
if (m_iMessagesLines > m_iMessagesHigh) { |
262 |
|
|
m_pMessagesTextView->setUpdatesEnabled(false); |
263 |
|
|
QTextCursor textCursor(m_pMessagesTextView->document()->begin()); |
264 |
capela |
1465 |
while (m_iMessagesLines > m_iMessagesLimit) { |
265 |
|
|
// Move cursor extending selection |
266 |
|
|
// from start to next line-block... |
267 |
|
|
textCursor.movePosition( |
268 |
|
|
QTextCursor::NextBlock, QTextCursor::KeepAnchor); |
269 |
|
|
m_iMessagesLines--; |
270 |
|
|
} |
271 |
|
|
// Remove the excessive line-blocks... |
272 |
|
|
textCursor.removeSelectedText(); |
273 |
capela |
1738 |
m_pMessagesTextView->setUpdatesEnabled(true); |
274 |
|
|
} |
275 |
capela |
1465 |
|
276 |
capela |
1738 |
m_pMessagesTextView->append(s); |
277 |
capela |
1465 |
m_iMessagesLines++; |
278 |
capela |
94 |
} |
279 |
|
|
|
280 |
|
|
|
281 |
capela |
1738 |
// The main utility methods. |
282 |
|
|
void Messages::appendMessages ( const QString& s ) |
283 |
|
|
{ |
284 |
capela |
1739 |
appendMessagesColor(s, "#999999"); |
285 |
capela |
1738 |
} |
286 |
|
|
|
287 |
|
|
void Messages::appendMessagesColor ( const QString& s, const QString &c ) |
288 |
|
|
{ |
289 |
|
|
QString sText = QTime::currentTime().toString("hh:mm:ss.zzz") + ' ' + s; |
290 |
|
|
appendMessagesLine("<font color=\"" + c + "\">" + sText + "</font>"); |
291 |
|
|
appendMessagesLog(sText); |
292 |
|
|
} |
293 |
|
|
|
294 |
|
|
void Messages::appendMessagesText ( const QString& s ) |
295 |
|
|
{ |
296 |
|
|
appendMessagesLine(s); |
297 |
|
|
appendMessagesLog(s); |
298 |
|
|
} |
299 |
|
|
|
300 |
|
|
|
301 |
capela |
1465 |
// History reset. |
302 |
capela |
1558 |
void Messages::clear (void) |
303 |
capela |
94 |
{ |
304 |
capela |
1465 |
m_iMessagesLines = 0; |
305 |
capela |
1738 |
m_pMessagesTextView->clear(); |
306 |
capela |
94 |
} |
307 |
|
|
|
308 |
capela |
1558 |
} // namespace QSampler |
309 |
capela |
94 |
|
310 |
capela |
1558 |
|
311 |
capela |
94 |
// end of qsamplerMessages.cpp |