forked from victronenergy/QsLog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
QsLogDestFile.cpp
165 lines (140 loc) · 5.6 KB
/
QsLogDestFile.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
// Copyright (c) 2013, Razvan Petru
// All rights reserved.
// Redistribution and use in source and binary forms, with or without modification,
// are permitted provided that the following conditions are met:
// * Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright notice, this
// list of conditions and the following disclaimer in the documentation and/or other
// materials provided with the distribution.
// * The name of the contributors may not be used to endorse or promote products
// derived from this software without specific prior written permission.
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
// ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
// IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
// INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
// OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
// OF THE POSSIBILITY OF SUCH DAMAGE.
#include "QsLogDestFile.h"
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
#include <QTextCodec>
#endif
#include <QDateTime>
#include <QtGlobal>
#include <iostream>
#if QT_VERSION < QT_VERSION_CHECK(5, 14, 0)
namespace Qt {
static const QTextStreamFunction endl = ::endl;
}
#endif
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;
}
// Algorithm assumes backups will be named filename.X, where 1 <= X <= mBackupsCount.
// All X's will be shifted up.
void QsLogging::SizeRotationStrategy::rotate()
{
if (!mBackupsCount) {
if (!QFile::remove(mFileName))
std::cerr << "QsLog: backup delete failed " << qPrintable(mFileName);
return;
}
// 1. find the last existing backup than can be shifted up
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;
}
// 2. shift up
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);
}
}
// 3. rename current log file
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);
}
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);
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
mOutputStream.setCodec(QTextCodec::codecForName("UTF-8"));
#endif
mRotationStrategy->setInitialInfo(mFile);
}
void QsLogging::FileDestination::write(const QString& message, Level)
{
mRotationStrategy->includeMessageInCalculation(message);
if (mRotationStrategy->shouldRotate()) {
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 << message << Qt::endl;
mOutputStream.flush();
}
bool QsLogging::FileDestination::isValid()
{
return mFile.isOpen();
}