acpidAnalyzer.h 4.46 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 ACPID_ANALYZER_H
#define ACPID_ANALYZER_H
24

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

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

#include "localLogFileReader.h"
#include "parsingHelper.h"
#include "acpidLogMode.h"

33
class AcpidAnalyzer : public FileAnalyzer
34 35
{
    Q_OBJECT
36

37
public:
Yuri Chornoivan's avatar
Yuri Chornoivan committed
38
    explicit AcpidAnalyzer(LogMode *logMode)
39
        : FileAnalyzer(logMode)
40 41
    {
    }
42

Laurent Montel's avatar
Laurent Montel committed
43
    ~AcpidAnalyzer() override {}
44

Laurent Montel's avatar
Laurent Montel committed
45
    LogViewColumns initColumns() override
46 47 48 49 50
    {
        LogViewColumns columns;
        columns.addColumn(LogViewColumn(i18n("Date"), true, false));
        columns.addColumn(LogViewColumn(i18n("Type"), true, true));
        columns.addColumn(LogViewColumn(i18n("Message"), true, false));
51

52 53
        return columns;
    }
54

55
protected:
Laurent Montel's avatar
Laurent Montel committed
56
    LogFileReader *createLogFileReader(const LogFile &logFile) override { return new LocalLogFileReader(logFile); }
57

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

Laurent Montel's avatar
Laurent Montel committed
60
    LogLine *parseMessage(const QString &logLine, const LogFile &originalFile) override
61 62
    {
        QString line(logLine);
63

64 65
        int dateBegin = line.indexOf(QLatin1String("["));
        int dateEnd = line.indexOf(QLatin1String("]"));
66

67 68 69 70
        QString type;
        QString message;
        QDate date;
        QTime time;
71

72 73 74 75 76 77 78 79
        // If there is a format problem in the line
        if (dateBegin == -1 || dateEnd == -1) {
            type = QLatin1String(""); // No type
            message = line;
            date = QDate::currentDate();
            time = QTime::currentTime();
        } else {
            QString strDate = line.mid(dateBegin + 1, dateEnd - dateBegin - 1);
80

81
            QString month = strDate.mid(4, 3);
82

83
            QString day = strDate.mid(8, 2);
84

85 86 87
            QString hour = strDate.mid(11, 2);
            QString min = strDate.mid(14, 2);
            QString sec = strDate.mid(17, 2);
88

89
            QString year = strDate.mid(20, 4);
90

91 92
            date = QDate(year.toInt(), ParsingHelper::instance()->parseSyslogMonth(month), day.toInt());
            time = QTime(hour.toInt(), min.toInt(), sec.toInt());
93

94 95
            // logDebug() << "Date=" << date.toString();
            // logDebug() << "Time=" << time.toString();
96

97
            line.remove(0, dateEnd + 2);
98

99
            int endType = line.indexOf(QLatin1String("\""));
100

101 102 103 104 105 106
            // If the " character does not exist, it means that there is no Type category
            if (endType == -1) {
                type = QLatin1String(""); // No type
                message = line;
            } else {
                type = line.left(endType - 1);
107
                line.remove(0, endType + 1);
108

109 110 111
                message = line.left(line.length() - 2);
            }
        }
112

113
        QStringList list;
114

115 116
        list.append(type);
        list.append(message);
117

118
        return new LogLine(logLineInternalIdGenerator++, QDateTime(date, time), list,
119
                           originalFile.url().toLocalFile(), Globals::instance().informationLogLevel(), logMode);
120
    }
121 122
};

123
#endif // _ACPID_ANALYZER_H