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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 2669 by capela, Sun Aug 3 16:56:18 2014 UTC revision 3793 by capela, Mon Jun 15 17:21:04 2020 UTC
# Line 1  Line 1 
1  // qsamplerMessages.cpp  // qsamplerMessages.cpp
2  //  //
3  /****************************************************************************  /****************************************************************************
4     Copyright (C) 2004-2014, rncbc aka Rui Nuno Capela. All rights reserved.     Copyright (C) 2004-2020, rncbc aka Rui Nuno Capela. All rights reserved.
5     Copyright (C) 2007, Christian Schoenebeck     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
# Line 26  Line 26 
26  #include <QSocketNotifier>  #include <QSocketNotifier>
27    
28  #include <QFile>  #include <QFile>
29  #include <QTextEdit>  #include <QTextBrowser>
30  #include <QTextCursor>  #include <QTextCursor>
31  #include <QTextStream>  #include <QTextStream>
32  #include <QTextBlock>  #include <QTextBlock>
 #include <QScrollBar>  
33  #include <QDateTime>  #include <QDateTime>
34  #include <QIcon>  #include <QIcon>
35    
36  #if !defined(WIN32)  #if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
37  #include <unistd.h>  #include <unistd.h>
38  #include <fcntl.h>  #include <fcntl.h>
39  #endif  #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 {  namespace QSampler {
49    
50  // The default maximum number of message lines.  // The default maximum number of message lines.
# Line 63  Messages::Messages ( QWidget *pParent ) Line 68  Messages::Messages ( QWidget *pParent )
68          QDockWidget::setObjectName("qsamplerMessages");          QDockWidget::setObjectName("qsamplerMessages");
69    
70          // Intialize stdout capture stuff.          // Intialize stdout capture stuff.
71          m_pStdoutNotifier = NULL;          m_pStdoutNotifier = nullptr;
72          m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;          m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;
73          m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL;          m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL;
74    
75          // Create local text view widget.          // Create local text view widget.
76          m_pMessagesTextView = new QTextEdit(this);          m_pMessagesTextView = new QTextBrowser(this);
77  //  QFont font(m_pMessagesTextView->font());  //  QFont font(m_pMessagesTextView->font());
78  //  font.setFamily("Fixed");  //  font.setFamily("Fixed");
79  //  m_pMessagesTextView->setFont(font);  //  m_pMessagesTextView->setFont(font);
80          m_pMessagesTextView->setLineWrapMode(QTextEdit::NoWrap);          m_pMessagesTextView->setLineWrapMode(QTextEdit::NoWrap);
81          m_pMessagesTextView->setReadOnly(true);  //      m_pMessagesTextView->setReadOnly(true);
82          m_pMessagesTextView->setUndoRedoEnabled(false);  //      m_pMessagesTextView->setUndoRedoEnabled(false);
83  //      m_pMessagesTextView->setTextFormat(Qt::LogText);  //      m_pMessagesTextView->setTextFormat(Qt::LogText);
84    
85          // Initialize default message limit.          // Initialize default message limit.
86          m_iMessagesLines = 0;          m_iMessagesLines = 0;
87          setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES);          setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES);
88    
89          m_pMessagesLog = NULL;          m_pMessagesLog = nullptr;
90    
91          // Prepare the dockable window stuff.          // Prepare the dockable window stuff.
92          QDockWidget::setWidget(m_pMessagesTextView);          QDockWidget::setWidget(m_pMessagesTextView);
93          QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures);  //      QDockWidget::setFeatures(QDockWidget::AllDockWidgetFeatures);
94          QDockWidget::setAllowedAreas(          QDockWidget::setAllowedAreas(
95                  Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);                  Qt::TopDockWidgetArea | Qt::BottomDockWidgetArea);
96          // Some specialties to this kind of dock window...          // Some specialties to this kind of dock window...
# Line 113  Messages::~Messages (void) Line 118  Messages::~Messages (void)
118  }  }
119    
120    
121    // Set stdout/stderr blocking mode.
122    bool Messages::stdoutBlock ( int fd, bool bBlock ) const
123    {
124    #if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
125            const int iFlags = ::fcntl(fd, F_GETFL, 0);
126            const bool bNonBlock = bool(iFlags & O_NONBLOCK);
127            if (bBlock && bNonBlock)
128                    bBlock = (::fcntl(fd, F_SETFL, iFlags & ~O_NONBLOCK) == 0);
129            else
130            if (!bBlock && !bNonBlock)
131                    bBlock = (::fcntl(fd, F_SETFL, iFlags |  O_NONBLOCK) != 0);
132    #endif
133            return bBlock;
134    }
135    
136    
137  // Own stdout/stderr socket notifier slot.  // Own stdout/stderr socket notifier slot.
138  void Messages::stdoutNotify ( int fd )  void Messages::stdoutNotify ( int fd )
139  {  {
140  #if !defined(WIN32)  #if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
141          // Set non-blocking reads, if not already...          // Set non-blocking reads, if not already...
142          const int iFlags = ::fcntl(fd, F_GETFL, 0);          const bool bBlock = stdoutBlock(fd, false);
         int iBlock = ((iFlags & O_NONBLOCK) == 0);  
         if (iBlock)  
                 iBlock = ::fcntl(fd, F_SETFL, iFlags | O_NONBLOCK);  
143          // Read as much as is available...          // Read as much as is available...
144          QString sTemp;          QString sTemp;
145          char achBuffer[1024];          char achBuffer[1024];
# Line 130  void Messages::stdoutNotify ( int fd ) Line 148  void Messages::stdoutNotify ( int fd )
148          while (cchRead > 0) {          while (cchRead > 0) {
149                  achBuffer[cchRead] = (char) 0;                  achBuffer[cchRead] = (char) 0;
150                  sTemp.append(achBuffer);                  sTemp.append(achBuffer);
151                  cchRead = (iBlock ? 0 : ::read(fd, achBuffer, cchBuffer));                  cchRead = (bBlock ? 0 : ::read(fd, achBuffer, cchBuffer));
152          }          }
153          // Needs to be non-empty...          // Needs to be non-empty...
154          if (!sTemp.isEmpty())          if (!sTemp.isEmpty())
# Line 144  void Messages::appendStdoutBuffer ( cons Line 162  void Messages::appendStdoutBuffer ( cons
162  {  {
163          m_sStdoutBuffer.append(s);          m_sStdoutBuffer.append(s);
164    
165            processStdoutBuffer();
166    }
167    
168    void Messages::processStdoutBuffer (void)
169    {
170          const int iLength = m_sStdoutBuffer.lastIndexOf('\n');          const int iLength = m_sStdoutBuffer.lastIndexOf('\n');
171          if (iLength > 0) {          if (iLength > 0) {
172                  const QString& sTemp = m_sStdoutBuffer.left(iLength);                  const QString& sTemp = m_sStdoutBuffer.left(iLength);
# Line 160  void Messages::appendStdoutBuffer ( cons Line 183  void Messages::appendStdoutBuffer ( cons
183  void Messages::flushStdoutBuffer (void)  void Messages::flushStdoutBuffer (void)
184  {  {
185          if (!m_sStdoutBuffer.isEmpty()) {          if (!m_sStdoutBuffer.isEmpty()) {
186                  appendMessagesText(m_sStdoutBuffer);                  processStdoutBuffer();
187                  m_sStdoutBuffer.clear();                  m_sStdoutBuffer.clear();
188          }          }
189  }  }
# Line 169  void Messages::flushStdoutBuffer (void) Line 192  void Messages::flushStdoutBuffer (void)
192  // Stdout capture accessors.  // Stdout capture accessors.
193  bool Messages::isCaptureEnabled (void)  bool Messages::isCaptureEnabled (void)
194  {  {
195          return (m_pStdoutNotifier != NULL);          return (m_pStdoutNotifier != nullptr);
196  }  }
197    
198  void Messages::setCaptureEnabled ( bool bCapture )  void Messages::setCaptureEnabled ( bool bCapture )
# Line 177  void Messages::setCaptureEnabled ( bool Line 200  void Messages::setCaptureEnabled ( bool
200          // Flush current buffer.          // Flush current buffer.
201          flushStdoutBuffer();          flushStdoutBuffer();
202    
203  #if !defined(WIN32)  #if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
204          // Destroy if already enabled.          // Destroy if already enabled.
205          if (!bCapture && m_pStdoutNotifier) {          if (!bCapture && m_pStdoutNotifier) {
206                  delete m_pStdoutNotifier;                  delete m_pStdoutNotifier;
207                  m_pStdoutNotifier = NULL;                  m_pStdoutNotifier = nullptr;
208                  // Close the notification pipes.                  // Close the notification pipes.
209                  if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {                  if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {
210                          ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);                          ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);
# Line 189  void Messages::setCaptureEnabled ( bool Line 212  void Messages::setCaptureEnabled ( bool
212                  }                  }
213          }          }
214          // Are we going to make up the capture?          // Are we going to make up the capture?
215          if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) {          if (bCapture && m_pStdoutNotifier == nullptr && ::pipe(m_fdStdout) == 0) {
216                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO);                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO);
217                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO);                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO);
218                  m_pStdoutNotifier = new QSocketNotifier(                  m_pStdoutNotifier = new QSocketNotifier(
# Line 229  void Messages::setMessagesLimit ( int iM Line 252  void Messages::setMessagesLimit ( int iM
252  // Messages logging stuff.  // Messages logging stuff.
253  bool Messages::isLogging (void) const  bool Messages::isLogging (void) const
254  {  {
255          return (m_pMessagesLog != NULL);          return (m_pMessagesLog != nullptr);
256  }  }
257    
258  void Messages::setLogging ( bool bEnabled, const QString& sFilename )  void Messages::setLogging ( bool bEnabled, const QString& sFilename )
# Line 239  void Messages::setLogging ( bool bEnable Line 262  void Messages::setLogging ( bool bEnable
262                          .arg(QDateTime::currentDateTime().toString()));                          .arg(QDateTime::currentDateTime().toString()));
263                  m_pMessagesLog->close();                  m_pMessagesLog->close();
264                  delete m_pMessagesLog;                  delete m_pMessagesLog;
265                  m_pMessagesLog = NULL;                  m_pMessagesLog = nullptr;
266          }          }
267    
268          if (bEnabled) {          if (bEnabled) {
# Line 249  void Messages::setLogging ( bool bEnable Line 272  void Messages::setLogging ( bool bEnable
272                                  .arg(QDateTime::currentDateTime().toString()));                                  .arg(QDateTime::currentDateTime().toString()));
273                  } else {                  } else {
274                          delete m_pMessagesLog;                          delete m_pMessagesLog;
275                          m_pMessagesLog = NULL;                          m_pMessagesLog = nullptr;
276                  }                  }
277          }          }
278  }  }
# Line 259  void Messages::setLogging ( bool bEnable Line 282  void Messages::setLogging ( bool bEnable
282  void Messages::appendMessagesLog ( const QString& s )  void Messages::appendMessagesLog ( const QString& s )
283  {  {
284          if (m_pMessagesLog) {          if (m_pMessagesLog) {
285                  QTextStream(m_pMessagesLog) << s << endl;                  QTextStream(m_pMessagesLog)
286                            << QTime::currentTime().toString("hh:mm:ss.zzz")
287                            << ' ' << s << endl;
288                  m_pMessagesLog->flush();                  m_pMessagesLog->flush();
289          }          }
290  }  }
# Line 291  void Messages::appendMessagesLine ( cons Line 316  void Messages::appendMessagesLine ( cons
316  // The main utility methods.  // The main utility methods.
317  void Messages::appendMessages ( const QString& s )  void Messages::appendMessages ( const QString& s )
318  {  {
319          appendMessagesColor(s, "#999999");          appendMessagesColor(s, Qt::gray);
320  }  }
321    
322  void Messages::appendMessagesColor ( const QString& s, const QString &c )  void Messages::appendMessagesColor ( const QString& s, const QColor& rgb )
323  {  {
324          const QString& sText          appendMessagesLine("<font color=\"" + rgb.name() + "\">" + s + "</font>");
325                  = QTime::currentTime().toString("hh:mm:ss.zzz") + ' ' + s;          appendMessagesLog(s);
         appendMessagesLine("<font color=\"" + c + "\">" + sText + "</font>");  
         appendMessagesLog(sText);  
326  }  }
327    
328  void Messages::appendMessagesText ( const QString& s )  void Messages::appendMessagesText ( const QString& s )

Legend:
Removed from v.2669  
changed lines
  Added in v.3793

  ViewVC Help
Powered by ViewVC