-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added logs remover and LOGGER_DEBUGLEVEL_ENABLED cmake option
- Loading branch information
1 parent
f888b0e
commit 6d4755b
Showing
13 changed files
with
223 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
/* | ||
* SPDX-License-Identifier: GPL-3.0-only | ||
* MuseScore-CLA-applies | ||
* | ||
* MuseScore | ||
* Music Composition & Notation | ||
* | ||
* Copyright (C) 2021 MuseScore BVBA and others | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
#include "logremover.h" | ||
|
||
#include <QDirIterator> | ||
#include <QFile> | ||
|
||
using namespace mu; | ||
|
||
void LogRemover::removeLogs(const io::path& logsDir, int olderThanDays, const QString& pattern) | ||
{ | ||
//! NOTE If the pattern changes, | ||
//! then we need to change the implementation of `scanDir` and `parseDate` functions. | ||
Q_ASSERT(pattern == "MuseScore_yyMMdd_HHmmss.log"); | ||
if (pattern != "MuseScore_yyMMdd_HHmmss.log") { | ||
return; | ||
} | ||
|
||
QStringList files; | ||
scanDir(logsDir, files); | ||
|
||
QDate currentDate = QDate::currentDate(); | ||
|
||
QStringList toRemoveFiles; | ||
for (const QString& file : files) { | ||
QDate date = parseDate(file); | ||
if (!date.isValid()) { | ||
continue; | ||
} | ||
|
||
int days = date.daysTo(currentDate); | ||
if (days >= olderThanDays) { | ||
toRemoveFiles << file; | ||
} | ||
} | ||
|
||
removeFiles(toRemoveFiles); | ||
} | ||
|
||
QDate LogRemover::parseDate(const QString& fileName) | ||
{ | ||
int endIdx = fileName.lastIndexOf('_'); | ||
if (endIdx == -1) { | ||
return QDate(); | ||
} | ||
|
||
int startIdx = fileName.lastIndexOf('_', endIdx - 1); | ||
if (startIdx == -1) { | ||
return QDate(); | ||
} | ||
|
||
QString dateStr = fileName.mid(startIdx + 1, (endIdx - startIdx - 1)); | ||
QDate date = QDate::fromString(dateStr, "yyMMdd"); | ||
|
||
//! NOTE We get `1921`, but it should be `2021` | ||
if (date.year() < 2000) { | ||
date = date.addYears(100); | ||
} | ||
return date; | ||
} | ||
|
||
void LogRemover::removeFiles(const QStringList& files) | ||
{ | ||
for (const QString& file : files) { | ||
QFile::remove(file); | ||
} | ||
} | ||
|
||
void LogRemover::scanDir(const io::path& logsDir, QStringList& files) | ||
{ | ||
QDirIterator it(logsDir.toQString(), { "*.log" }, QDir::NoDotAndDotDot | QDir::NoSymLinks | QDir::Readable | QDir::Files); | ||
while (it.hasNext()) { | ||
files.push_back(it.next()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
* SPDX-License-Identifier: GPL-3.0-only | ||
* MuseScore-CLA-applies | ||
* | ||
* MuseScore | ||
* Music Composition & Notation | ||
* | ||
* Copyright (C) 2021 MuseScore BVBA and others | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU General Public License version 3 as | ||
* published by the Free Software Foundation. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU General Public License | ||
* along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef MU_LOGREMOVER_H | ||
#define MU_LOGREMOVER_H | ||
|
||
#include <QStringList> | ||
#include <QDate> | ||
|
||
#include <gtest/gtest_prod.h> | ||
|
||
#include "io/path.h" | ||
|
||
namespace mu { | ||
class LogRemover | ||
{ | ||
public: | ||
|
||
static void removeLogs(const io::path& logsDir, int olderThanDays, const QString& pattern); | ||
|
||
private: | ||
|
||
FRIEND_TEST(LogRemoverTests, ParseDate); | ||
|
||
static void scanDir(const io::path& logsDir, QStringList& files); | ||
static QDate parseDate(const QString& fileName); | ||
static void removeFiles(const QStringList& files); | ||
}; | ||
} | ||
|
||
#endif // MU_LOGREMOVER_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.