aboutsummaryrefslogtreecommitdiff
path: root/Logger/cyclelogfile.cpp
blob: cb0266e6edae3e362ddae693374da1d8776eceb4 (plain)
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
#include "logger.h"

// if "now" matches the previous logfile name, no cycling needed. otherwise, determine new logfile name.
bool Logger::cycleLogFile()
{
    // calculate the expected log filename
    QString newFilename = _logLabel;

    // get date to see if we need to cycle
    QDate date(QDate::currentDate());

    // when is it necessary to cycle?
    switch ( _cyclePeriod )
    {
        case 1: // yearly
            newFilename += date.toString("-yyyy");
            break;
        case 2: // monthly
            newFilename += date.toString("-yyyy-MM");
            break;
        case 3: // weekly
            newFilename += date.toString("-yyyy") + "-week" + QString("%1").arg(date.weekNumber(), 2, 10, QChar('0'));
            break;
        case 4: // daily
            newFilename += date.toString("-yyyy-MM-dd");
            break;
        default:; // 0 = never
    }

    // add log file extension
    newFilename += ".log";

    // if current and new filename are not the same, cycle log file
    if (_logFileName != newFilename)
    {
        // cycle log file
        _logFileName = newFilename;

        // close the existing/opened log file
        closeLogFile();

        // open the new log file (pass possible error to caller)
        return openLogFile();
    }

    // else: no cycling necessary
    return true;
}