Menu

[729340]: / src / qsamplerMessages.cpp  Maximize  Restore  History

Download this file

366 lines (299 with data), 9.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// qsamplerMessages.cpp
//
/****************************************************************************
Copyright (C) 2004-2021, rncbc aka Rui Nuno Capela. All rights reserved.
Copyright (C) 2007, Christian Schoenebeck
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*****************************************************************************/
#include "qsamplerAbout.h"
#include "qsamplerMessages.h"
#include <QSocketNotifier>
#include <QFile>
#include <QTextBrowser>
#include <QTextCursor>
#include <QTextStream>
#include <QTextBlock>
#include <QDateTime>
#include <QIcon>
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
#include <unistd.h>
#include <fcntl.h>
#endif
// Deprecated QTextStreamFunctions/Qt namespaces workaround.
#if QT_VERSION >= QT_VERSION_CHECK(5, 15, 0)
#define endl Qt::endl
#endif
namespace QSampler {
// The default maximum number of message lines.
#define QSAMPLER_MESSAGES_MAXLINES 1000
// Notification pipe descriptors
#define QSAMPLER_MESSAGES_FDNIL -1
#define QSAMPLER_MESSAGES_FDREAD 0
#define QSAMPLER_MESSAGES_FDWRITE 1
//-------------------------------------------------------------------------
// QSampler::Messages - Messages log dockable window.
//
// Constructor.
Messages::Messages ( QWidget *pParent )
: QDockWidget(pParent)
{
// Surely a name is crucial (e.g.for storing geometry settings)
QDockWidget::setObjectName("qsamplerMessages");
// Intialize stdout capture stuff.
m_pStdoutNotifier = nullptr;
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL;
m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL;
// Create local text view widget.
m_pMessagesTextView = new QTextBrowser(this);
// QFont font(m_pMessagesTextView->font());
// font.setFamily("Fixed");
// m_pMessagesTextView->setFont(font);
m_pMessagesTextView->setLineWrapMode(QTextEdit::NoWrap);
// m_pMessagesTextView->setReadOnly(true);
// m_pMessagesTextView->setUndoRedoEnabled(false);
// m_pMessagesTextView->setTextFormat(Qt::LogText);
// Initialize default message limit.
m_iMessagesLines = 0;
setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES);
m_pMessagesLog = nullptr;
// Prepare the dockable window stuff.
QDockWidget::setWidget(m_pMessagesTextView);
// QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures);
QDockWidget::setAllowedAreas(
Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
// Some specialties to this kind of dock window...
QDockWidget::setMinimumHeight(120);
// Finally set the default caption and tooltip.
const QString& sCaption = tr("Messages");
QDockWidget::setWindowTitle(sCaption);
// QDockWidget::setWindowIcon(QIcon(":/icons/qsamplerMessages.png"));
QDockWidget::setToolTip(sCaption);
}
// Destructor.
Messages::~Messages (void)
{
// Turn off and close logging.
setLogging(false);
// No more notifications.
if (m_pStdoutNotifier)
delete m_pStdoutNotifier;
// No need to delete child widgets, Qt does it all for us.
}
#if defined(Q_CC_GNU) || defined(Q_CC_MINGW)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wunused-parameter"
#endif
// Set stdout/stderr blocking mode.
bool Messages::stdoutBlock ( int fd, bool bBlock ) const
{
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
const int iFlags = ::fcntl(fd, F_GETFL, 0);
const bool bNonBlock = bool(iFlags & O_NONBLOCK);
if (bBlock && bNonBlock)
bBlock = (::fcntl(fd, F_SETFL, iFlags & ~O_NONBLOCK) == 0);
else
if (!bBlock && !bNonBlock)
bBlock = (::fcntl(fd, F_SETFL, iFlags | O_NONBLOCK) != 0);
#endif
return bBlock;
}
// Own stdout/stderr socket notifier slot.
void Messages::stdoutNotify ( int fd )
{
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
// Set non-blocking reads, if not already...
const bool bBlock = stdoutBlock(fd, false);
// Read as much as is available...
QString sTemp;
char achBuffer[1024];
const int cchBuffer = sizeof(achBuffer) - 1;
int cchRead = ::read(fd, achBuffer, cchBuffer);
while (cchRead > 0) {
achBuffer[cchRead] = (char) 0;
sTemp.append(achBuffer);
cchRead = (bBlock ? 0 : ::read(fd, achBuffer, cchBuffer));
}
// Needs to be non-empty...
if (!sTemp.isEmpty())
appendStdoutBuffer(sTemp);
#endif
}
#if defined(Q_CC_GNU) || defined(Q_CC_MINGW)
#pragma GCC diagnostic pop
#endif
// Stdout buffer handler -- now splitted by complete new-lines...
void Messages::appendStdoutBuffer ( const QString& s )
{
m_sStdoutBuffer.append(s);
processStdoutBuffer();
}
void Messages::processStdoutBuffer (void)
{
const int iLength = m_sStdoutBuffer.lastIndexOf('\n');
if (iLength > 0) {
QStringListIterator iter(m_sStdoutBuffer.left(iLength).split('\n'));
while (iter.hasNext()) {
const QString& sTemp = iter.next();
if (!sTemp.isEmpty())
#if defined(__WIN32__) || defined(_WIN32) || defined(WIN32)
appendMessagesText(sTemp.trimmed());
#else
appendMessagesText(sTemp);
#endif
}
m_sStdoutBuffer.remove(0, iLength + 1);
}
}
// Stdout flusher -- show up any unfinished line...
void Messages::flushStdoutBuffer (void)
{
processStdoutBuffer();
if (!m_sStdoutBuffer.isEmpty()) {
#if defined(__WIN32__) || defined(_WIN32) || defined(WIN32)
appendMessagesText(m_sStdoutBuffer.trimmed());
#else
appendMessagesText(m_sStdoutBuffer);
#endif
m_sStdoutBuffer.clear();
}
}
// Stdout capture accessors.
bool Messages::isCaptureEnabled (void)
{
return (m_pStdoutNotifier != nullptr);
}
void Messages::setCaptureEnabled ( bool bCapture )
{
// Flush current buffer.
flushStdoutBuffer();
#if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
// Destroy if already enabled.
if (!bCapture && m_pStdoutNotifier) {
delete m_pStdoutNotifier;
m_pStdoutNotifier = nullptr;
// Close the notification pipes.
if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {
::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);
m_fdStdout[QSAMPLER_MESSAGES_FDREAD] = QSAMPLER_MESSAGES_FDNIL;
}
}
// Are we going to make up the capture?
if (bCapture && m_pStdoutNotifier == nullptr && ::pipe(m_fdStdout) == 0) {
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO);
::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO);
m_pStdoutNotifier = new QSocketNotifier(
m_fdStdout[QSAMPLER_MESSAGES_FDREAD], QSocketNotifier::Read, this);
QObject::connect(m_pStdoutNotifier,
SIGNAL(activated(int)),
SLOT(stdoutNotify(int)));
}
#endif
}
// Message font accessors.
QFont Messages::messagesFont (void)
{
return m_pMessagesTextView->font();
}
void Messages::setMessagesFont ( const QFont& font )
{
m_pMessagesTextView->setFont(font);
}
// Maximum number of message lines accessors.
int Messages::messagesLimit (void)
{
return m_iMessagesLimit;
}
void Messages::setMessagesLimit ( int iMessagesLimit )
{
m_iMessagesLimit = iMessagesLimit;
m_iMessagesHigh = iMessagesLimit + (iMessagesLimit / 3);
}
// Messages logging stuff.
bool Messages::isLogging (void) const
{
return (m_pMessagesLog != nullptr);
}
void Messages::setLogging ( bool bEnabled, const QString& sFilename )
{
if (m_pMessagesLog) {
appendMessages(tr("Logging stopped --- %1 ---")
.arg(QDateTime::currentDateTime().toString()));
m_pMessagesLog->close();
delete m_pMessagesLog;
m_pMessagesLog = nullptr;
}
if (bEnabled) {
m_pMessagesLog = new QFile(sFilename);
if (m_pMessagesLog->open(QIODevice::Text | QIODevice::Append)) {
appendMessages(tr("Logging started --- %1 ---")
.arg(QDateTime::currentDateTime().toString()));
} else {
delete m_pMessagesLog;
m_pMessagesLog = nullptr;
}
}
}
// Messages log output method.
void Messages::appendMessagesLog ( const QString& s )
{
if (m_pMessagesLog) {
QTextStream(m_pMessagesLog)
<< QTime::currentTime().toString("hh:mm:ss.zzz")
<< ' ' << s << endl;
m_pMessagesLog->flush();
}
}
// Messages widget output method.
void Messages::appendMessagesLine ( const QString& s )
{
// Check for message line limit...
if (m_iMessagesLines > m_iMessagesHigh) {
m_pMessagesTextView->setUpdatesEnabled(false);
QTextCursor textCursor(m_pMessagesTextView->document()->begin());
while (m_iMessagesLines > m_iMessagesLimit) {
// Move cursor extending selection
// from start to next line-block...
textCursor.movePosition(
QTextCursor::NextBlock, QTextCursor::KeepAnchor);
m_iMessagesLines--;
}
// Remove the excessive line-blocks...
textCursor.removeSelectedText();
m_pMessagesTextView->setUpdatesEnabled(true);
}
m_pMessagesTextView->append(s);
m_iMessagesLines++;
}
// The main utility methods.
void Messages::appendMessages ( const QString& s )
{
appendMessagesColor(s, Qt::gray);
}
void Messages::appendMessagesColor ( const QString& s, const QColor& rgb )
{
appendMessagesLine("<font color=\"" + rgb.name() + "\">" + s + "</font>");
appendMessagesLog(s);
}
void Messages::appendMessagesText ( const QString& s )
{
appendMessagesLine(s);
appendMessagesLog(s);
}
// History reset.
void Messages::clear (void)
{
m_iMessagesLines = 0;
m_pMessagesTextView->clear();
}
} // namespace QSampler
// end of qsamplerMessages.cpp
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.