/[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 2583 by capela, Fri May 30 21:03:25 2014 UTC revision 3555 by capela, Tue Aug 13 10:19:32 2019 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-2019, 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 34  Line 34 
34  #include <QDateTime>  #include <QDateTime>
35  #include <QIcon>  #include <QIcon>
36    
37  #if !defined(WIN32)  #if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
38  #include <unistd.h>  #include <unistd.h>
39    #include <fcntl.h>
40  #endif  #endif
41    
42    
# Line 62  Messages::Messages ( QWidget *pParent ) Line 63  Messages::Messages ( QWidget *pParent )
63          QDockWidget::setObjectName("qsamplerMessages");          QDockWidget::setObjectName("qsamplerMessages");
64    
65          // Intialize stdout capture stuff.          // Intialize stdout capture stuff.
66          m_pStdoutNotifier = NULL;          m_pStdoutNotifier = nullptr;
67          m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;          m_fdStdout[QSAMPLER_MESSAGES_FDREAD]  = QSAMPLER_MESSAGES_FDNIL;
68          m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL;          m_fdStdout[QSAMPLER_MESSAGES_FDWRITE] = QSAMPLER_MESSAGES_FDNIL;
69    
# Line 80  Messages::Messages ( QWidget *pParent ) Line 81  Messages::Messages ( QWidget *pParent )
81          m_iMessagesLines = 0;          m_iMessagesLines = 0;
82          setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES);          setMessagesLimit(QSAMPLER_MESSAGES_MAXLINES);
83    
84          m_pMessagesLog = NULL;          m_pMessagesLog = nullptr;
85    
86          // Prepare the dockable window stuff.          // Prepare the dockable window stuff.
87          QDockWidget::setWidget(m_pMessagesTextView);          QDockWidget::setWidget(m_pMessagesTextView);
# Line 112  Messages::~Messages (void) Line 113  Messages::~Messages (void)
113  }  }
114    
115    
116    // Set stdout/stderr blocking mode.
117    bool Messages::stdoutBlock ( int fd, bool bBlock ) const
118    {
119    #if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
120            const int iFlags = ::fcntl(fd, F_GETFL, 0);
121            const bool bNonBlock = bool(iFlags & O_NONBLOCK);
122            if (bBlock && bNonBlock)
123                    bBlock = (::fcntl(fd, F_SETFL, iFlags & ~O_NONBLOCK) == 0);
124            else
125            if (!bBlock && !bNonBlock)
126                    bBlock = (::fcntl(fd, F_SETFL, iFlags |  O_NONBLOCK) != 0);
127    #endif
128            return bBlock;
129    }
130    
131    
132  // Own stdout/stderr socket notifier slot.  // Own stdout/stderr socket notifier slot.
133  void Messages::stdoutNotify ( int fd )  void Messages::stdoutNotify ( int fd )
134  {  {
135  #if !defined(WIN32)  #if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
136            // Set non-blocking reads, if not already...
137            const bool bBlock = stdoutBlock(fd, false);
138            // Read as much as is available...
139            QString sTemp;
140          char achBuffer[1024];          char achBuffer[1024];
141          const int cchBuffer = ::read(fd, achBuffer, sizeof(achBuffer) - 1);          const int cchBuffer = sizeof(achBuffer) - 1;
142          if (cchBuffer > 0) {          int cchRead = ::read(fd, achBuffer, cchBuffer);
143                  achBuffer[cchBuffer] = (char) 0;          while (cchRead > 0) {
144                  appendStdoutBuffer(achBuffer);                  achBuffer[cchRead] = (char) 0;
145                    sTemp.append(achBuffer);
146                    cchRead = (bBlock ? 0 : ::read(fd, achBuffer, cchBuffer));
147          }          }
148            // Needs to be non-empty...
149            if (!sTemp.isEmpty())
150                    appendStdoutBuffer(sTemp);
151  #endif  #endif
152  }  }
153    
# Line 148  void Messages::flushStdoutBuffer (void) Line 174  void Messages::flushStdoutBuffer (void)
174  {  {
175          if (!m_sStdoutBuffer.isEmpty()) {          if (!m_sStdoutBuffer.isEmpty()) {
176                  appendMessagesText(m_sStdoutBuffer);                  appendMessagesText(m_sStdoutBuffer);
177                  m_sStdoutBuffer.truncate(0);                  m_sStdoutBuffer.clear();
178          }          }
179  }  }
180    
# Line 156  void Messages::flushStdoutBuffer (void) Line 182  void Messages::flushStdoutBuffer (void)
182  // Stdout capture accessors.  // Stdout capture accessors.
183  bool Messages::isCaptureEnabled (void)  bool Messages::isCaptureEnabled (void)
184  {  {
185          return (m_pStdoutNotifier != NULL);          return (m_pStdoutNotifier != nullptr);
186  }  }
187    
188  void Messages::setCaptureEnabled ( bool bCapture )  void Messages::setCaptureEnabled ( bool bCapture )
# Line 164  void Messages::setCaptureEnabled ( bool Line 190  void Messages::setCaptureEnabled ( bool
190          // Flush current buffer.          // Flush current buffer.
191          flushStdoutBuffer();          flushStdoutBuffer();
192    
193  #if !defined(WIN32)  #if !defined(__WIN32__) && !defined(_WIN32) && !defined(WIN32)
194          // Destroy if already enabled.          // Destroy if already enabled.
195          if (!bCapture && m_pStdoutNotifier) {          if (!bCapture && m_pStdoutNotifier) {
196                  delete m_pStdoutNotifier;                  delete m_pStdoutNotifier;
197                  m_pStdoutNotifier = NULL;                  m_pStdoutNotifier = nullptr;
198                  // Close the notification pipes.                  // Close the notification pipes.
199                  if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {                  if (m_fdStdout[QSAMPLER_MESSAGES_FDREAD] != QSAMPLER_MESSAGES_FDNIL) {
200                          ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);                          ::close(m_fdStdout[QSAMPLER_MESSAGES_FDREAD]);
# Line 176  void Messages::setCaptureEnabled ( bool Line 202  void Messages::setCaptureEnabled ( bool
202                  }                  }
203          }          }
204          // Are we going to make up the capture?          // Are we going to make up the capture?
205          if (bCapture && m_pStdoutNotifier == NULL && ::pipe(m_fdStdout) == 0) {          if (bCapture && m_pStdoutNotifier == nullptr && ::pipe(m_fdStdout) == 0) {
206                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO);                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDOUT_FILENO);
207                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO);                  ::dup2(m_fdStdout[QSAMPLER_MESSAGES_FDWRITE], STDERR_FILENO);
208                  m_pStdoutNotifier = new QSocketNotifier(                  m_pStdoutNotifier = new QSocketNotifier(
# Line 216  void Messages::setMessagesLimit ( int iM Line 242  void Messages::setMessagesLimit ( int iM
242  // Messages logging stuff.  // Messages logging stuff.
243  bool Messages::isLogging (void) const  bool Messages::isLogging (void) const
244  {  {
245          return (m_pMessagesLog != NULL);          return (m_pMessagesLog != nullptr);
246  }  }
247    
248  void Messages::setLogging ( bool bEnabled, const QString& sFilename )  void Messages::setLogging ( bool bEnabled, const QString& sFilename )
# Line 226  void Messages::setLogging ( bool bEnable Line 252  void Messages::setLogging ( bool bEnable
252                          .arg(QDateTime::currentDateTime().toString()));                          .arg(QDateTime::currentDateTime().toString()));
253                  m_pMessagesLog->close();                  m_pMessagesLog->close();
254                  delete m_pMessagesLog;                  delete m_pMessagesLog;
255                  m_pMessagesLog = NULL;                  m_pMessagesLog = nullptr;
256          }          }
257    
258          if (bEnabled) {          if (bEnabled) {
# Line 236  void Messages::setLogging ( bool bEnable Line 262  void Messages::setLogging ( bool bEnable
262                                  .arg(QDateTime::currentDateTime().toString()));                                  .arg(QDateTime::currentDateTime().toString()));
263                  } else {                  } else {
264                          delete m_pMessagesLog;                          delete m_pMessagesLog;
265                          m_pMessagesLog = NULL;                          m_pMessagesLog = nullptr;
266                  }                  }
267          }          }
268  }  }

Legend:
Removed from v.2583  
changed lines
  Added in v.3555

  ViewVC Help
Powered by ViewVC