1 /*
2     SPDX-FileCopyrightText: 2007 Nicolas Ternisien <nicolas.ternisien@gmail.com>
3 
4     SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "cupsAnalyzer.h"
8 #include <KLocalizedString>
9 
CupsAnalyzer(LogMode * logMode)10 CupsAnalyzer::CupsAnalyzer(LogMode *logMode)
11     : FileAnalyzer(logMode)
12 {
13     initializeTypeLevels();
14 }
15 
~CupsAnalyzer()16 CupsAnalyzer::~CupsAnalyzer()
17 {
18 }
19 
initColumns()20 LogViewColumns CupsAnalyzer::initColumns()
21 {
22     LogViewColumns columns;
23     columns.addColumn(LogViewColumn(i18n("Date"), true, false));
24     columns.addColumn(LogViewColumn(i18n("Message"), true, false));
25 
26     return columns;
27 }
28 
createLogFileReader(const LogFile & logFile)29 LogFileReader *CupsAnalyzer::createLogFileReader(const LogFile &logFile)
30 {
31     return new LocalLogFileReader(logFile);
32 }
33 
logFileSortMode()34 Analyzer::LogFileSortMode CupsAnalyzer::logFileSortMode()
35 {
36     return Analyzer::AscendingSortedLogFile;
37 }
38 
parseMessage(const QString & logLine,const LogFile & originalLogFile)39 LogLine *CupsAnalyzer::parseMessage(const QString &logLine, const LogFile &originalLogFile)
40 {
41     QString line(logLine);
42 
43     const QChar level = logLine[0];
44 
45     const QDateTime dateTime = ParsingHelper::instance()->parseHttpDateTime(logLine.mid(3, 26));
46 
47     const QString message = line.remove(0, 31);
48 
49     const QStringList list{message};
50 
51     return new LogLine(mLogLineInternalIdGenerator++, dateTime, list, originalLogFile.url().toLocalFile(), findLogLevel(level), mLogMode);
52 }
53 
initializeTypeLevels()54 void CupsAnalyzer::initializeTypeLevels()
55 {
56     mMapTypeLevels[QLatin1Char('d')] = new LogLevel(20, i18n("debug 2"), QStringLiteral(DEBUG2_LOG_LEVEL_ICON), QColor(169, 189, 165), this);
57     mMapTypeLevels[QLatin1Char('D')] = Globals::instance().debugLogLevel();
58     mMapTypeLevels[QLatin1Char('I')] = Globals::instance().informationLogLevel();
59     mMapTypeLevels[QLatin1Char('N')] = Globals::instance().noticeLogLevel();
60     mMapTypeLevels[QLatin1Char('W')] = Globals::instance().warningLogLevel();
61     mMapTypeLevels[QLatin1Char('E')] = Globals::instance().errorLogLevel();
62     mMapTypeLevels[QLatin1Char('C')] = Globals::instance().criticalLogLevel();
63     mMapTypeLevels[QLatin1Char('A')] = Globals::instance().alertLogLevel();
64     mMapTypeLevels[QLatin1Char('X')] = Globals::instance().emergencyLogLevel();
65     mMapTypeLevels[QLatin1Char(' ')] = Globals::instance().noLogLevel();
66 }
67 
findLogLevel(QChar type)68 LogLevel *CupsAnalyzer::findLogLevel(QChar type)
69 {
70     QMap<QChar, LogLevel *>::iterator it;
71 
72     it = mMapTypeLevels.find(type);
73     if (it != mMapTypeLevels.end()) {
74         return *it;
75     } else {
76         qCCritical(KSYSTEMLOG) << i18n("New Log Level detected: Please send this log file to the KSystemLog developer to add it.");
77         return Globals::instance().noLogLevel();
78     }
79 }
80