cupsPageAnalyzer.h 3.89 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/***************************************************************************
 *   KSystemLog, a system log viewer tool                                  *
 *   Copyright (C) 2007 by Nicolas Ternisien                               *
 *   nicolas.ternisien@gmail.com                                           *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   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, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.          *
 ***************************************************************************/

22 23
#ifndef CUPS_PAGE_ANALYZER_H
#define CUPS_PAGE_ANALYZER_H
24

Laurent Montel's avatar
Laurent Montel committed
25
#include <KLocalizedString>
26

27
#include "fileAnalyzer.h"
28 29 30 31 32 33 34

#include "localLogFileReader.h"
#include "logging.h"
#include "parsingHelper.h"

#include "cupsPageLogMode.h"

35
class CupsPageAnalyzer : public FileAnalyzer
36 37
{
    Q_OBJECT
38

39 40
public:
    explicit CupsPageAnalyzer(LogMode *logMode)
41
        : FileAnalyzer(logMode)
42
        , cupsPageRegex(QStringLiteral("(\\S*) (\\S*) (\\S*) \\[(.*)\\] (\\S*) (\\S*) (\\S*)"))
43 44
    {
    }
45

46
    virtual ~CupsPageAnalyzer() {}
47

Laurent Montel's avatar
Laurent Montel committed
48
    LogViewColumns initColumns() override
49 50
    {
        LogViewColumns columns;
51

52 53 54 55 56 57 58
        columns.addColumn(LogViewColumn(i18n("Date"), true, false));
        columns.addColumn(LogViewColumn(i18n("Printer"), true, true));
        columns.addColumn(LogViewColumn(i18n("User"), true, true));
        columns.addColumn(LogViewColumn(i18n("Job Id"), true, true));
        columns.addColumn(LogViewColumn(i18n("Page Number"), true, false));
        columns.addColumn(LogViewColumn(i18n("Num Copies"), true, true));
        columns.addColumn(LogViewColumn(i18n("Job Billing"), true, false));
59

60 61
        return columns;
    }
62

63 64
protected:
    QRegExp cupsPageRegex;
65

Laurent Montel's avatar
Laurent Montel committed
66
    LogFileReader *createLogFileReader(const LogFile &logFile) override { return new LocalLogFileReader(logFile); }
67

Laurent Montel's avatar
Laurent Montel committed
68
    Analyzer::LogFileSortMode logFileSortMode() override { return Analyzer::AscendingSortedLogFile; }
69

70
    /*
71
     * https://www.cups.org/doc/man-cupsd-logs.html
72 73 74 75 76
     * Format : printer user job-id date-time page-number num-copies job-billing
     *
     * DeskJet root 2 [20/May/1999:19:21:05 +0000] 1 1 acme-123
     * DeskJet root 2 [20/May/1999:19:21:05 +0000] 2 1 acme-123
     */
Laurent Montel's avatar
Laurent Montel committed
77
    LogLine *parseMessage(const QString &logLine, const LogFile &originalLogFile) override
78 79 80 81
    {
        int firstPosition = cupsPageRegex.indexIn(logLine);
        if (firstPosition == -1) {
            logDebug() << "Unable to parse line " << logLine;
Laurent Montel's avatar
Laurent Montel committed
82
            return nullptr;
83
        }
84

85
        QStringList capturedTexts = cupsPageRegex.capturedTexts();
86

87 88
        // Remove full line
        capturedTexts.removeAt(0);
89

90
        QDateTime dateTime = ParsingHelper::instance()->parseHttpDateTime(capturedTexts.takeAt(3));
91

92
        return new LogLine(logLineInternalIdGenerator++, dateTime, capturedTexts,
93
                           originalLogFile.url().toLocalFile(), Globals::instance().informationLogLevel(), logMode);
94
    }
95 96
};

97
#endif // _CUPS_PAGE_ANALYZER_H