/[svn]/qsampler/trunk/src/qsamplerMessages.cpp
ViewVC logotype

Contents of /qsampler/trunk/src/qsamplerMessages.cpp

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC