diff options
| author | Tomasz Sterna <tomek@xiaoka.com> | 2014-07-11 21:06:39 +0200 |
|---|---|---|
| committer | Tomasz Sterna <tomek@xiaoka.com> | 2014-07-11 21:06:39 +0200 |
| commit | efb33d6494d88c27c8766553b6a963ddf2654458 (patch) | |
| tree | 76dd5e56b79191074998c0fb6bf7b81276116a4b /ext/Log4Qt/src/asyncappender.cpp | |
| parent | 072da88eee57e5d16f0b75c7b90c8a0bc6a60cb3 (diff) | |
Included Log4Qt in project
Diffstat (limited to 'ext/Log4Qt/src/asyncappender.cpp')
| -rw-r--r-- | ext/Log4Qt/src/asyncappender.cpp | 157 |
1 files changed, 157 insertions, 0 deletions
diff --git a/ext/Log4Qt/src/asyncappender.cpp b/ext/Log4Qt/src/asyncappender.cpp new file mode 100644 index 0000000..251f87f --- /dev/null +++ b/ext/Log4Qt/src/asyncappender.cpp @@ -0,0 +1,157 @@ +/****************************************************************************** + * + * package: Log4Qt + * file: asyncappender.cpp + * created: February 2011 + * author: Andreas Bacher + * + * + * Copyright 2011 Andreas Bacher + * + * 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 + * + * 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. + * + ******************************************************************************/ + + +/****************************************************************************** + * Dependencies + ******************************************************************************/ + +#include "asyncappender.h" +#include "loggingevent.h" +#include "helpers/dispatcher.h" + +#include <QtCore/QDebug> +#include <QtCore/QCoreApplication> +#include <QtCore/QReadLocker> + +namespace Log4Qt +{ + +/************************************************************************** + * Declarations + **************************************************************************/ + + +/************************************************************************** + * C helper functions + **************************************************************************/ + + +/************************************************************************** + * Class implementation: AsyncAppender + **************************************************************************/ + + +AsyncAppender::AsyncAppender(QObject *parent) : AppenderSkeleton(parent) + ,mpThread(0) + ,mpDispatcher(0) +{ +} + +AsyncAppender::~AsyncAppender() +{ + close(); +} + +bool AsyncAppender::requiresLayout() const +{ + return false; +} + +void AsyncAppender::activateOptions() +{ + if (mpThread) + return; + + mpThread = new QThread(); + mpDispatcher = new Dispatcher(); + mpDispatcher->setAsyncAppender(this); + + mpDispatcher->moveToThread(mpThread); + mpThread->start(); +} + +void AsyncAppender::close() +{ + if (mpThread) + { + mpDispatcher->setAsyncAppender(0); + mpThread->quit(); + mpThread->wait(); + delete mpThread; + mpThread = 0; + delete mpDispatcher; + mpDispatcher = 0; + } +} + +void AsyncAppender::callAppenders(const LoggingEvent &rEvent) const +{ + QReadLocker locker(&mAppenderGuard); + + Appender *pAppender; + Q_FOREACH(pAppender, mAppenders) + pAppender->doAppend(rEvent); +} + +void AsyncAppender::append(const LoggingEvent &rEvent) +{ + // Post to the event loop of the dispatcher + LoggingEvent *event = new LoggingEvent(rEvent); + qApp->postEvent(mpDispatcher, event); +} + +bool AsyncAppender::checkEntryConditions() const +{ + if (mpThread && !mpThread->isRunning()) + { + LogError + e = + LOG4QT_QCLASS_ERROR(QT_TR_NOOP("Use of appender '%1' without a running dispatcher thread"), + APPENDER_ASNC_DISPATCHER_NOT_RUNNING); + e << name(); + logger()->error(e); + return false; + } + + return AppenderSkeleton::checkEntryConditions(); +} + +#ifndef QT_NO_DEBUG_STREAM +/*! + * Writes all object member variables to the given debug stream + * \a rDebug and returns the stream. + * + * <tt> + * %AsyncAppender(name:"WA" ) + * </tt> + * \sa QDebug, operator<<(QDebug debug, const LogObject &rLogObject ) + */ +QDebug AsyncAppender::debug(QDebug &rDebug) const +{ + rDebug.nospace() << "AsyncAppender(" + << "name:" << name() << " " + << ")"; + + return rDebug.space(); +} +#endif // QT_NO_DEBUG_STREAM + + +/************************************************************************** + * Implementation: Operators, Helper + **************************************************************************/ + +} // namespace Log4Qt + |
