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