123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163 |
- #include "QsLogDestFile.h"
- #include <QTextCodec>
- #include <QDateTime>
- #include <QtGlobal>
- #include <iostream>
- const int QsLogging::SizeRotationStrategy::MaxBackupCount = 10;
- QsLogging::RotationStrategy::~RotationStrategy()
- {
- }
- QsLogging::SizeRotationStrategy::SizeRotationStrategy() : mCurrentSizeInBytes(0), mMaxSizeInBytes(0), mBackupsCount(0)
- {
- }
- void QsLogging::SizeRotationStrategy::setInitialInfo(const QFile& file)
- {
- mFileName = file.fileName();
- mCurrentSizeInBytes = file.size();
- }
- void QsLogging::SizeRotationStrategy::includeMessageInCalculation(const QString& message)
- {
- mCurrentSizeInBytes += message.toUtf8().size();
- }
- bool QsLogging::SizeRotationStrategy::shouldRotate()
- {
- return mCurrentSizeInBytes > mMaxSizeInBytes;
- }
- void QsLogging::SizeRotationStrategy::rotate()
- {
- if (!mBackupsCount)
- {
- if (!QFile::remove(mFileName))
- std::cerr << "QsLog: backup delete failed " << qPrintable(mFileName);
- return;
- }
-
- const QString logNamePattern = mFileName + QString::fromUtf8(".%1");
- int lastExistingBackupIndex = 0;
- for (int i = 1; i <= mBackupsCount; ++i)
- {
- const QString backupFileName = logNamePattern.arg(i);
- if (QFile::exists(backupFileName))
- lastExistingBackupIndex = qMin(i, mBackupsCount - 1);
- else
- break;
- }
-
- for (int i = lastExistingBackupIndex; i >= 1; --i)
- {
- const QString oldName = logNamePattern.arg(i);
- const QString newName = logNamePattern.arg(i + 1);
- QFile::remove(newName);
- const bool renamed = QFile::rename(oldName, newName);
- if (!renamed)
- {
- std::cerr << "QsLog: could not rename backup " << qPrintable(oldName) << " to " << qPrintable(newName);
- }
- }
-
- const QString newName = logNamePattern.arg(1);
- if (QFile::exists(newName))
- QFile::remove(newName);
- if (!QFile::rename(mFileName, newName))
- {
- std::cerr << "QsLog: could not rename log " << qPrintable(mFileName) << " to " << qPrintable(newName);
- }
- }
- QIODevice::OpenMode QsLogging::SizeRotationStrategy::recommendedOpenModeFlag()
- {
- return QIODevice::Append;
- }
- void QsLogging::SizeRotationStrategy::setMaximumSizeInBytes(qint64 size)
- {
- Q_ASSERT(size >= 0);
- mMaxSizeInBytes = size;
- }
- void QsLogging::SizeRotationStrategy::setBackupCount(int backups)
- {
- Q_ASSERT(backups >= 0);
- mBackupsCount = qMin(backups, SizeRotationStrategy::MaxBackupCount);
- }
- qint64 QsLogging::SizeRotationStrategy::currentSizeInBytes()
- {
- return mCurrentSizeInBytes;
- }
- QsLogging::FileDestination::FileDestination(const QString& filePath, RotationStrategyPtr rotationStrategy)
- : mRotationStrategy(rotationStrategy)
- {
- mFile.setFileName(filePath);
- if (!mFile.open(QFile::WriteOnly | QFile::Text | mRotationStrategy->recommendedOpenModeFlag()))
- std::cerr << "QsLog: could not open log file " << qPrintable(filePath);
- mOutputStream.setDevice(&mFile);
- mOutputStream.setCodec("UTF-8");
- mRotationStrategy->setInitialInfo(mFile);
- }
- void QsLogging::FileDestination::write(const QString& message, Level)
- {
- mRotationStrategy->includeMessageInCalculation(message);
- if (mRotationStrategy->shouldRotate())
- rotate();
- mOutputStream << message << "\n";
- mOutputStream.flush();
- }
- bool QsLogging::FileDestination::isValid()
- {
- return mFile.isOpen();
- }
- void QsLogging::FileDestination::rotate()
- {
- mOutputStream.setDevice(NULL);
- mFile.close();
- mRotationStrategy->rotate();
- if (!mFile.open(QFile::WriteOnly | QFile::Text | mRotationStrategy->recommendedOpenModeFlag()))
- std::cerr << "QsLog: could not reopen log file " << qPrintable(mFile.fileName());
- mRotationStrategy->setInitialInfo(mFile);
- mOutputStream.setDevice(&mFile);
- mOutputStream.setCodec("UTF-8");
- }
|