LCOV - code coverage report
Current view: top level - muse/framework/global - logremover.cpp Coverage Total Hit
Test: filtered_coverage.info Lines: 80.9 % 47 38
Test Date: 2026-11-05 04:07:02

            Line data    Source code
       1              : /*
       2              :  * SPDX-License-Identifier: GPL-3.0-only
       3              :  * MuseScore-CLA-applies
       4              :  *
       5              :  * MuseScore Studio
       6              :  * Music Composition & Notation
       7              :  *
       8              :  * Copyright (C) 2021 MuseScore Limited and others
       9              :  *
      10              :  * This program is free software: you can redistribute it and/or modify
      11              :  * it under the terms of the GNU General Public License version 3 as
      12              :  * published by the Free Software Foundation.
      13              :  *
      14              :  * This program is distributed in the hope that it will be useful,
      15              :  * but WITHOUT ANY WARRANTY; without even the implied warranty of
      16              :  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      17              :  * GNU General Public License for more details.
      18              :  *
      19              :  * You should have received a copy of the GNU General Public License
      20              :  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
      21              :  */
      22              : #include "logremover.h"
      23              : 
      24              : #include "io/file.h"
      25              : #include "io/dir.h"
      26              : 
      27              : #include "log.h"
      28              : 
      29              : using namespace muse;
      30              : 
      31           46 : void LogRemover::removeLogs(const io::path_t& logsDir, int olderThanDays, const String& pattern)
      32              : {
      33              :     //! NOTE If the pattern changes,
      34              :     //! then we need to change the implementation of `scanDir` and `parseDate` functions.
      35           46 :     IF_ASSERT_FAILED(pattern.endsWith(u"_yyMMdd.log") || pattern.endsWith(u"_yyMMdd_HHmmss.log")) {
      36            0 :         return;
      37              :     }
      38              : 
      39           46 :     io::paths_t files;
      40           46 :     scanDir(logsDir, files);
      41              : 
      42           46 :     Date currentDate = Date::currentDate();
      43              : 
      44           46 :     io::paths_t toRemoveFiles;
      45           46 :     for (const io::path_t& file : files) {
      46            0 :         Date date = parseDate(io::filename(file).toString());
      47            0 :         if (date.isNull()) {
      48            0 :             continue;
      49              :         }
      50              : 
      51            0 :         int days = date.daysTo(currentDate);
      52            0 :         if (days >= olderThanDays) {
      53            0 :             toRemoveFiles.push_back(file);
      54              :         }
      55              :     }
      56              : 
      57           46 :     removeFiles(toRemoveFiles);
      58           46 : }
      59              : 
      60           11 : muse::Date LogRemover::parseDate(const String& fileName)
      61              : {
      62           11 :     size_t dateStartIdx = fileName.indexOf(u'_');
      63           11 :     if (dateStartIdx == muse::nidx) {
      64            1 :         return Date();
      65              :     }
      66              : 
      67           10 :     String dateStr = fileName.mid(dateStartIdx + 1, fileName.size() - 1);
      68              : 
      69              :     // "yyMMdd"
      70           10 :     String yy = dateStr.mid(0, 2);
      71           10 :     bool ok = false;
      72           10 :     int y = yy.toInt(&ok);
      73           10 :     if (!ok || !(y > 0)) {
      74            4 :         return Date();
      75              :     }
      76              : 
      77            6 :     String mm = dateStr.mid(2, 2);
      78            6 :     int m = mm.toInt(&ok);
      79            6 :     if (!ok || !(m > 0 && m <= 12)) {
      80            1 :         return Date();
      81              :     }
      82              : 
      83            5 :     String dd = dateStr.mid(4, 2);
      84            5 :     int d = dd.toInt(&ok);
      85            5 :     if (!ok || !(d > 0 && d <= 31)) {
      86            1 :         return Date();
      87              :     }
      88              : 
      89            4 :     Date date(2000 + y, m, d);
      90            4 :     return date;
      91           10 : }
      92              : 
      93           46 : void LogRemover::removeFiles(const io::paths_t& files)
      94              : {
      95           46 :     for (const io::path_t& file : files) {
      96            0 :         io::File::remove(file);
      97              :     }
      98           46 : }
      99              : 
     100           46 : void LogRemover::scanDir(const io::path_t& logsDir, io::paths_t& files)
     101              : {
     102           92 :     RetVal<io::paths_t> rv = io::Dir::scanFiles(logsDir, { "*.log" }, io::ScanMode::FilesInCurrentDir);
     103           46 :     if (!rv.ret) {
     104            0 :         LOGE() << "failed scan dir: " << logsDir;
     105              :     }
     106              : 
     107           46 :     files = rv.val;
     108           46 : }
        

Generated by: LCOV version 2.0-1