00001 /****************************************************************************** 00002 * 00003 * package: Log4Qt 00004 * file: writerappender.h 00005 * created: September 2007 00006 * author: Martin Heinrich 00007 * 00008 * 00009 * Copyright 2007 Martin Heinrich 00010 * 00011 * Licensed under the Apache License, Version 2.0 (the "License"); 00012 * you may not use this file except in compliance with the License. 00013 * You may obtain a copy of the License at 00014 * 00015 * http://www.apache.org/licenses/LICENSE-2.0 00016 * 00017 * Unless required by applicable law or agreed to in writing, software 00018 * distributed under the License is distributed on an "AS IS" BASIS, 00019 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00020 * See the License for the specific language governing permissions and 00021 * limitations under the License. 00022 * 00023 ******************************************************************************/ 00024 00025 #ifndef LOG4QT_WRITERAPPENDER_H 00026 #define LOG4QT_WRITERAPPENDER_H 00027 00028 00029 /****************************************************************************** 00030 * Dependencies 00031 ******************************************************************************/ 00032 00033 #include "log4qt/appenderskeleton.h" 00034 00035 00036 /****************************************************************************** 00037 * Declarations 00038 ******************************************************************************/ 00039 00040 class QTextCodec; 00041 class QTextStream; 00042 00043 namespace Log4Qt 00044 { 00045 00054 class WriterAppender : public AppenderSkeleton 00055 { 00056 Q_OBJECT 00057 00065 Q_PROPERTY(QTextCodec* encoding READ encoding WRITE setEncoding) 00066 00067 00072 Q_PROPERTY(QTextStream* writer READ writer WRITE setWriter) 00073 00081 Q_PROPERTY(bool immediateFlush READ immediateFlush WRITE setImmediateFlush) 00082 00083 public: 00084 WriterAppender(QObject *pParent = 0); 00085 WriterAppender(Layout *pLayout, 00086 QObject *pParent = 0); 00087 WriterAppender(Layout *pLayout, 00088 QTextStream *pTextStream, 00089 QObject *pParent = 0); 00090 virtual ~WriterAppender(); 00091 private: 00092 WriterAppender(const WriterAppender &rOther); // Not implemented 00093 WriterAppender &operator=(const WriterAppender &rOther); // Not implemented 00094 00095 public: 00096 virtual bool requiresLayout() const; 00097 QTextCodec *encoding() const; 00098 bool immediateFlush() const; 00099 QTextStream *writer() const; 00100 00110 void setEncoding(QTextCodec *pTextCodec); 00111 void setImmediateFlush(bool immediateFlush); 00112 void setWriter(QTextStream *pTextStream); 00113 00114 virtual void activateOptions(); 00115 virtual void close(); 00116 00117 protected: 00118 virtual void append(const LoggingEvent &rEvent); 00119 00137 virtual bool checkEntryConditions() const; 00138 00139 void closeWriter(); 00140 00141 #ifndef QT_NO_DEBUG_STREAM 00142 00154 virtual QDebug debug(QDebug &rDebug) const; 00155 #endif // QT_NO_DEBUG_STREAM 00156 00157 virtual bool handleIoErrors() const; 00158 void writeFooter() const; 00159 void writeHeader() const; 00160 00161 private: 00162 QTextCodec *mpEncoding; 00163 QTextStream *mpWriter; 00164 volatile bool mImmediateFlush; 00165 }; 00166 00167 00168 /************************************************************************** 00169 * Operators, Helper 00170 **************************************************************************/ 00171 00172 00173 /************************************************************************** 00174 * Inline 00175 **************************************************************************/ 00176 00177 inline QTextCodec *WriterAppender::encoding() const 00178 { QMutexLocker locker(&mObjectGuard); 00179 return mpEncoding; } 00180 00181 inline bool WriterAppender::immediateFlush() const 00182 { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe 00183 return mImmediateFlush; } 00184 00185 inline QTextStream *WriterAppender::writer() const 00186 { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe 00187 return mpWriter; } 00188 00189 inline void WriterAppender::setImmediateFlush(bool immediateFlush) 00190 { // QMutexLocker locker(&mObjectGuard); // Read/Write of int is safe 00191 mImmediateFlush = immediateFlush; } 00192 00193 00194 } // namespace Log4Qt 00195 00196 00197 // Q_DECLARE_TYPEINFO(Log4Qt::WriterAppender, Q_COMPLEX_TYPE); // Use default 00198 00199 00200 #endif // LOG4QT_WRITERAPPENDER_H