QsLogDestFile.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. // Copyright (c) 2013, Razvan Petru
  2. // All rights reserved.
  3. // Redistribution and use in source and binary forms, with or without modification,
  4. // are permitted provided that the following conditions are met:
  5. // * Redistributions of source code must retain the above copyright notice, this
  6. // list of conditions and the following disclaimer.
  7. // * Redistributions in binary form must reproduce the above copyright notice, this
  8. // list of conditions and the following disclaimer in the documentation and/or other
  9. // materials provided with the distribution.
  10. // * The name of the contributors may not be used to endorse or promote products
  11. // derived from this software without specific prior written permission.
  12. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  13. // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  14. // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  15. // IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
  16. // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  17. // BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  18. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  19. // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  20. // OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  21. // OF THE POSSIBILITY OF SUCH DAMAGE.
  22. #ifndef QSLOGDESTFILE_H
  23. #define QSLOGDESTFILE_H
  24. #include "QsLogDest.h"
  25. #include <QFile>
  26. #include <QTextStream>
  27. #include <QtGlobal>
  28. #include <QSharedPointer>
  29. namespace QsLogging
  30. {
  31. class RotationStrategy
  32. {
  33. public:
  34. virtual ~RotationStrategy();
  35. virtual void setInitialInfo(const QFile &file) = 0;
  36. virtual void includeMessageInCalculation(const QString &message) = 0;
  37. virtual bool shouldRotate() = 0;
  38. virtual void rotate() = 0;
  39. virtual QIODevice::OpenMode recommendedOpenModeFlag() = 0;
  40. virtual qint64 currentSizeInBytes() {return 0;}
  41. };
  42. // Never rotates file, overwrites existing file.
  43. class NullRotationStrategy : public RotationStrategy
  44. {
  45. public:
  46. virtual void setInitialInfo(const QFile &) {}
  47. virtual void includeMessageInCalculation(const QString &) {}
  48. virtual bool shouldRotate() { return false; }
  49. virtual void rotate() {}
  50. virtual QIODevice::OpenMode recommendedOpenModeFlag() { return QIODevice::Truncate; }
  51. };
  52. // Rotates after a size is reached, keeps a number of <= 10 backups, appends to existing file.
  53. class SizeRotationStrategy : public RotationStrategy
  54. {
  55. public:
  56. SizeRotationStrategy();
  57. static const int MaxBackupCount;
  58. virtual void setInitialInfo(const QFile &file);
  59. virtual void includeMessageInCalculation(const QString &message);
  60. virtual bool shouldRotate();
  61. virtual void rotate();
  62. virtual QIODevice::OpenMode recommendedOpenModeFlag();
  63. virtual qint64 currentSizeInBytes();
  64. void setMaximumSizeInBytes(qint64 size);
  65. void setBackupCount(int backups);
  66. private:
  67. QString mFileName;
  68. qint64 mCurrentSizeInBytes;
  69. qint64 mMaxSizeInBytes;
  70. int mBackupsCount;
  71. };
  72. typedef QSharedPointer<RotationStrategy> RotationStrategyPtr;
  73. // file message sink
  74. class FileDestination : public Destination
  75. {
  76. public:
  77. FileDestination(const QString& filePath, RotationStrategyPtr rotationStrategy);
  78. virtual void write(const QString& message, Level level);
  79. virtual bool isValid();
  80. virtual void rotate();
  81. QSharedPointer<RotationStrategy> rotationStrategy() { return mRotationStrategy; }
  82. private:
  83. QFile mFile;
  84. QTextStream mOutputStream;
  85. QSharedPointer<RotationStrategy> mRotationStrategy;
  86. };
  87. }
  88. #endif // QSLOGDESTFILE_H