123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- #ifndef QSLOGDESTFILE_H
- #define QSLOGDESTFILE_H
- #include "QsLogDest.h"
- #include <QFile>
- #include <QTextStream>
- #include <QtGlobal>
- #include <QSharedPointer>
- namespace QsLogging
- {
- class RotationStrategy
- {
- public:
- virtual ~RotationStrategy();
- virtual void setInitialInfo(const QFile &file) = 0;
- virtual void includeMessageInCalculation(const QString &message) = 0;
- virtual bool shouldRotate() = 0;
- virtual void rotate() = 0;
- virtual QIODevice::OpenMode recommendedOpenModeFlag() = 0;
- virtual qint64 currentSizeInBytes() {return 0;}
- };
- class NullRotationStrategy : public RotationStrategy
- {
- public:
- virtual void setInitialInfo(const QFile &) {}
- virtual void includeMessageInCalculation(const QString &) {}
- virtual bool shouldRotate() { return false; }
- virtual void rotate() {}
- virtual QIODevice::OpenMode recommendedOpenModeFlag() { return QIODevice::Truncate; }
- };
- class SizeRotationStrategy : public RotationStrategy
- {
- public:
- SizeRotationStrategy();
- static const int MaxBackupCount;
- virtual void setInitialInfo(const QFile &file);
- virtual void includeMessageInCalculation(const QString &message);
- virtual bool shouldRotate();
- virtual void rotate();
- virtual QIODevice::OpenMode recommendedOpenModeFlag();
- virtual qint64 currentSizeInBytes();
- void setMaximumSizeInBytes(qint64 size);
- void setBackupCount(int backups);
- private:
- QString mFileName;
- qint64 mCurrentSizeInBytes;
- qint64 mMaxSizeInBytes;
- int mBackupsCount;
- };
- typedef QSharedPointer<RotationStrategy> RotationStrategyPtr;
- class FileDestination : public Destination
- {
- public:
- FileDestination(const QString& filePath, RotationStrategyPtr rotationStrategy);
- virtual void write(const QString& message, Level level);
- virtual bool isValid();
- virtual void rotate();
- QSharedPointer<RotationStrategy> rotationStrategy() { return mRotationStrategy; }
- private:
- QFile mFile;
- QTextStream mOutputStream;
- QSharedPointer<RotationStrategy> mRotationStrategy;
- };
- }
- #endif
|