123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- /*=========================================================================
- Library: CTK
- Copyright (c) Kitware Inc.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0.txt
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- =========================================================================*/
- // Qt includes
- #include <QMutexLocker>
- #include <QThread>
- #include <QHash>
- // CTK includes
- #include "ctkErrorLogContext.h"
- #include "ctkErrorLogStreamMessageHandler.h"
- #include "ctkUtils.h"
- // STD includes
- #include <iostream>
- #include <streambuf>
- #include <string>
- namespace
- {
- // --------------------------------------------------------------------------
- // ctkStreamHandler
- //
- // See http://lists.trolltech.com/qt-interest/2005-06/thread00166-0.html
- //
- // --------------------------------------------------------------------------
- class ctkStreamHandler : public std::streambuf
- {
- public:
- ctkStreamHandler(ctkErrorLogStreamMessageHandler* messageHandler,
- ctkErrorLogLevel::LogLevel logLevel,
- std::ostream& stream);
- void setEnabled(bool value);
- protected:
- virtual int_type overflow(int_type v);
- virtual std::streamsize xsputn(const char *p, std::streamsize n);
- private:
- void initBuffer();
- std::string* currentBuffer();
- ctkErrorLogStreamMessageHandler * MessageHandler;
- ctkErrorLogLevel::LogLevel LogLevel;
- ctkErrorLogContext LogContext;
- bool Enabled;
- std::ostream& Stream;
- std::streambuf* SavedBuffer;
- QHash<Qt::HANDLE, std::string*> StringBuffers;
- QMutex Mutex;
- };
- // --------------------------------------------------------------------------
- // ctkStreamHandler methods
- // --------------------------------------------------------------------------
- ctkStreamHandler::ctkStreamHandler(ctkErrorLogStreamMessageHandler* messageHandler,
- ctkErrorLogLevel::LogLevel logLevel,
- std::ostream& stream) :
- MessageHandler(messageHandler), LogLevel(logLevel), Stream(stream)
- {
- this->Enabled = false;
- }
- // --------------------------------------------------------------------------
- void ctkStreamHandler::setEnabled(bool value)
- {
- if (this->Enabled == value)
- {
- return;
- }
- if (value)
- {
- this->SavedBuffer = this->Stream.rdbuf();
- this->Stream.rdbuf(this);
- }
- else
- {
- // Output anything that is left
- // if (!this->StringBuffer.empty())
- // {
- // Q_ASSERT(this->MessageHandler);
- // this->MessageHandler->handleMessage(this->LogLevel,
- // this->MessageHandler->handlerPrettyName(), this->StringBuffer.c_str());
- // }
- this->Stream.rdbuf(this->SavedBuffer);
- }
- this->Enabled = value;
- }
- // --------------------------------------------------------------------------
- void ctkStreamHandler::initBuffer()
- {
- Qt::HANDLE threadId = QThread::currentThreadId();
- if (!this->StringBuffers.contains(threadId))
- {
- this->StringBuffers.insert(threadId, new std::string);
- }
- }
- // --------------------------------------------------------------------------
- std::string* ctkStreamHandler::currentBuffer()
- {
- return this->StringBuffers.value(QThread::currentThreadId());
- }
- // --------------------------------------------------------------------------
- std::streambuf::int_type ctkStreamHandler::overflow(std::streambuf::int_type v)
- {
- QMutexLocker locker(&this->Mutex);
- this->initBuffer();
- if (v == '\n')
- {
- Q_ASSERT(this->MessageHandler);
- this->MessageHandler->handleMessage(
- ctk::qtHandleToString(QThread::currentThreadId()), this->LogLevel,
- this->MessageHandler->handlerPrettyName(),
- ctkErrorLogContext(this->currentBuffer()->c_str()), this->currentBuffer()->c_str());
- this->currentBuffer()->erase(this->currentBuffer()->begin(), this->currentBuffer()->end());
- }
- else
- {
- *this->currentBuffer() += v;
- }
- return v;
- }
- // --------------------------------------------------------------------------
- std::streamsize ctkStreamHandler::xsputn(const char *p, std::streamsize n)
- {
- QMutexLocker locker(&this->Mutex);
- this->initBuffer();
- this->currentBuffer()->append(p, p + n);
- std::string::size_type pos = 0;
- while (pos != std::string::npos)
- {
- pos = this->currentBuffer()->find('\n');
- if (pos != std::string::npos)
- {
- std::string tmp(this->currentBuffer()->begin(), this->currentBuffer()->begin() + pos);
- Q_ASSERT(this->MessageHandler);
- this->MessageHandler->handleMessage(
- ctk::qtHandleToString(QThread::currentThreadId()), this->LogLevel,
- this->MessageHandler->handlerPrettyName(),
- ctkErrorLogContext(tmp.c_str()), tmp.c_str());
- this->currentBuffer()->erase(this->currentBuffer()->begin(), this->currentBuffer()->begin() + pos + 1);
- }
- }
- return n;
- }
- }
- // --------------------------------------------------------------------------
- // ctkErrorLogStreamMessageHandlerPrivate
- // --------------------------------------------------------------------------
- class ctkErrorLogStreamMessageHandlerPrivate
- {
- public:
- ctkErrorLogStreamMessageHandlerPrivate();
- ~ctkErrorLogStreamMessageHandlerPrivate();
- ctkStreamHandler * CoutStreamHandler;
- ctkStreamHandler * CerrStreamHandler;
- };
- // --------------------------------------------------------------------------
- // ctkErrorLogStreamMessageHandlerPrivate methods
- //------------------------------------------------------------------------------
- ctkErrorLogStreamMessageHandlerPrivate::ctkErrorLogStreamMessageHandlerPrivate()
- {
- }
- //------------------------------------------------------------------------------
- ctkErrorLogStreamMessageHandlerPrivate::~ctkErrorLogStreamMessageHandlerPrivate()
- {
- delete this->CoutStreamHandler;
- delete this->CerrStreamHandler;
- }
- //------------------------------------------------------------------------------
- // ctkErrorLogStreamMessageHandler methods
- //------------------------------------------------------------------------------
- QString ctkErrorLogStreamMessageHandler::HandlerName = QLatin1String("Stream");
- // --------------------------------------------------------------------------
- ctkErrorLogStreamMessageHandler::ctkErrorLogStreamMessageHandler() :
- Superclass(), d_ptr(new ctkErrorLogStreamMessageHandlerPrivate())
- {
- Q_D(ctkErrorLogStreamMessageHandler);
- d->CoutStreamHandler = new ctkStreamHandler(this, ctkErrorLogLevel::Info, std::cout);
- d->CerrStreamHandler = new ctkStreamHandler(this, ctkErrorLogLevel::Critical, std::cerr);
- }
- // --------------------------------------------------------------------------
- ctkErrorLogStreamMessageHandler::~ctkErrorLogStreamMessageHandler()
- {
- }
- // --------------------------------------------------------------------------
- QString ctkErrorLogStreamMessageHandler::handlerName()const
- {
- return ctkErrorLogStreamMessageHandler::HandlerName;
- }
- // --------------------------------------------------------------------------
- void ctkErrorLogStreamMessageHandler::setEnabledInternal(bool value)
- {
- Q_D(ctkErrorLogStreamMessageHandler);
- d->CoutStreamHandler->setEnabled(value);
- d->CerrStreamHandler->setEnabled(value);
- }
|