1 |
c137512e
|
Oto Šťáva
|
// 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 |
|
|
#ifndef QSLOGDESTFILE_H
|
27 |
|
|
#define QSLOGDESTFILE_H
|
28 |
|
|
|
29 |
|
|
#include "QsLogDest.h"
|
30 |
|
|
#include <QFile>
|
31 |
|
|
#include <QTextStream>
|
32 |
|
|
#include <QtGlobal>
|
33 |
|
|
#include <QSharedPointer>
|
34 |
|
|
|
35 |
|
|
namespace QsLogging
|
36 |
|
|
{
|
37 |
|
|
class RotationStrategy
|
38 |
|
|
{
|
39 |
|
|
public:
|
40 |
|
|
virtual ~RotationStrategy();
|
41 |
|
|
|
42 |
|
|
virtual void setInitialInfo(const QFile &file) = 0;
|
43 |
|
|
virtual void includeMessageInCalculation(const QString &message) = 0;
|
44 |
|
|
virtual bool shouldRotate() = 0;
|
45 |
|
|
virtual void rotate() = 0;
|
46 |
|
|
virtual QIODevice::OpenMode recommendedOpenModeFlag() = 0;
|
47 |
|
|
};
|
48 |
|
|
|
49 |
|
|
// Never rotates file, overwrites existing file.
|
50 |
|
|
class NullRotationStrategy : public RotationStrategy
|
51 |
|
|
{
|
52 |
|
|
public:
|
53 |
|
|
virtual void setInitialInfo(const QFile &) {}
|
54 |
|
|
virtual void includeMessageInCalculation(const QString &) {}
|
55 |
|
|
virtual bool shouldRotate() { return false; }
|
56 |
|
|
virtual void rotate() {}
|
57 |
|
|
virtual QIODevice::OpenMode recommendedOpenModeFlag() { return QIODevice::Truncate; }
|
58 |
|
|
};
|
59 |
|
|
|
60 |
|
|
// Rotates after a size is reached, keeps a number of <= 10 backups, appends to existing file.
|
61 |
|
|
class SizeRotationStrategy : public RotationStrategy
|
62 |
|
|
{
|
63 |
|
|
public:
|
64 |
|
|
SizeRotationStrategy();
|
65 |
|
|
static const int MaxBackupCount;
|
66 |
|
|
|
67 |
|
|
virtual void setInitialInfo(const QFile &file);
|
68 |
|
|
virtual void includeMessageInCalculation(const QString &message);
|
69 |
|
|
virtual bool shouldRotate();
|
70 |
|
|
virtual void rotate();
|
71 |
|
|
virtual QIODevice::OpenMode recommendedOpenModeFlag();
|
72 |
|
|
|
73 |
|
|
void setMaximumSizeInBytes(qint64 size);
|
74 |
|
|
void setBackupCount(int backups);
|
75 |
|
|
|
76 |
|
|
private:
|
77 |
|
|
QString mFileName;
|
78 |
|
|
qint64 mCurrentSizeInBytes;
|
79 |
|
|
qint64 mMaxSizeInBytes;
|
80 |
|
|
int mBackupsCount;
|
81 |
|
|
};
|
82 |
|
|
|
83 |
|
|
typedef QSharedPointer<RotationStrategy> RotationStrategyPtr;
|
84 |
|
|
|
85 |
|
|
// file message sink
|
86 |
|
|
class FileDestination : public Destination
|
87 |
|
|
{
|
88 |
|
|
public:
|
89 |
|
|
FileDestination(const QString& filePath, RotationStrategyPtr rotationStrategy);
|
90 |
|
|
virtual void write(const QString& message, Level level);
|
91 |
|
|
virtual bool isValid();
|
92 |
|
|
|
93 |
|
|
private:
|
94 |
|
|
QFile mFile;
|
95 |
|
|
QTextStream mOutputStream;
|
96 |
|
|
QSharedPointer<RotationStrategy> mRotationStrategy;
|
97 |
|
|
};
|
98 |
|
|
|
99 |
|
|
}
|
100 |
|
|
|
101 |
|
|
#endif // QSLOGDESTFILE_H
|