File indexing completed on 2024-05-12 05:48:34

0001 /*
0002     SPDX-FileCopyrightText: 2007 Nicolas Ternisien <nicolas.ternisien@gmail.com>
0003 
0004     SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "apacheAnalyzer.h"
0008 
0009 ApacheAnalyzer::ApacheAnalyzer(LogMode *logMode)
0010     : FileAnalyzer(logMode)
0011 {
0012     initializeTypeLevels();
0013 }
0014 
0015 LogViewColumns ApacheAnalyzer::initColumns()
0016 {
0017     LogViewColumns columns;
0018     columns.addColumn(LogViewColumn(i18n("Date"), true, false));
0019     columns.addColumn(LogViewColumn(i18n("Client"), true, false));
0020     columns.addColumn(LogViewColumn(i18n("Message"), true, false));
0021 
0022     return columns;
0023 }
0024 
0025 LogFileReader *ApacheAnalyzer::createLogFileReader(const LogFile &logFile)
0026 {
0027     return new LocalLogFileReader(logFile);
0028 }
0029 
0030 Analyzer::LogFileSortMode ApacheAnalyzer::logFileSortMode()
0031 {
0032     return Analyzer::AscendingSortedLogFile;
0033 }
0034 
0035 LogLine *ApacheAnalyzer::parseMessage(const QString &logLine, const LogFile &originalLogFile)
0036 {
0037     QString line(logLine);
0038 
0039     QDate date;
0040     QTime time;
0041 
0042     QString level;
0043 
0044     // Temporary variable
0045     int squareBracket;
0046 
0047     // Special case which sometimes happens
0048     if (line.indexOf(QLatin1String("[client")) == 0) {
0049         date = QDate::currentDate();
0050         time = QTime::currentTime();
0051         level = QStringLiteral("notice");
0052     } else {
0053         // The Date
0054         int const dateBegin = line.indexOf(QLatin1String("["));
0055         int const dateEnd = line.indexOf(QLatin1String("]"));
0056 
0057         const QString strDate = line.mid(dateBegin + 1, dateEnd - dateBegin - 1);
0058 
0059         const QString month = strDate.mid(4, 3);
0060 
0061         const QString day = strDate.mid(8, 2);
0062 
0063         const QString hour = strDate.mid(11, 2);
0064         const QString min = strDate.mid(14, 2);
0065         const QString sec = strDate.mid(17, 2);
0066 
0067         const QString year = strDate.mid(20, 4);
0068 
0069         date = QDate(year.toInt(), ParsingHelper::instance()->parseSyslogMonth(month), day.toInt());
0070         time = QTime(hour.toInt(), min.toInt(), sec.toInt());
0071 
0072         line.remove(0, dateEnd + 3);
0073 
0074         // The log level
0075         squareBracket = line.indexOf(QLatin1String("]"));
0076         level = line.left(squareBracket);
0077         line.remove(0, squareBracket + 2);
0078     }
0079 
0080     // The client
0081     int const beginSquareBracket = line.indexOf(QLatin1String("[client"));
0082     squareBracket = line.indexOf(QLatin1String("]"));
0083     QString client;
0084     if (beginSquareBracket == -1 || squareBracket == -1) {
0085         client = QLatin1String("");
0086     } else {
0087         client = line.mid(8, squareBracket - 8); // 8=strlen("[client ")
0088         line.remove(0, squareBracket + 2);
0089     }
0090 
0091     QStringList list;
0092     list.append(client);
0093     list.append(line);
0094 
0095     return new LogLine(mLogLineInternalIdGenerator++, QDateTime(date, time), list, originalLogFile.url().toLocalFile(), findLogLevel(level), mLogMode);
0096 }
0097 
0098 void ApacheAnalyzer::initializeTypeLevels()
0099 {
0100     mMapTypeLevels[QStringLiteral("notice")] = Globals::instance().informationLogLevel();
0101     mMapTypeLevels[QStringLiteral("warn")] = Globals::instance().warningLogLevel();
0102     mMapTypeLevels[QStringLiteral("error")] = Globals::instance().errorLogLevel();
0103 }
0104 
0105 LogLevel *ApacheAnalyzer::findLogLevel(const QString &type)
0106 {
0107     QMap<QString, LogLevel *>::iterator it;
0108 
0109     it = mMapTypeLevels.find(type);
0110     if (it != mMapTypeLevels.end()) {
0111         return *it;
0112     } else {
0113         qCCritical(KSYSTEMLOG) << "New Log Level detected: Please send this log file to the KSystemLog developer to add it (" << type << ")";
0114         return Globals::instance().noLogLevel();
0115     }
0116 }
0117 
0118 #include "moc_apacheAnalyzer.cpp"