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