kernelAnalyzer.h 5.41 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 KERNEL_ANALYZER_H
#define KERNEL_ANALYZER_H
24 25 26 27

#include <QRegExp>
#include <QFile>

Laurent Montel's avatar
Laurent Montel committed
28
#include <KLocalizedString>
29 30 31

#include "logging.h"

32
#include "fileAnalyzer.h"
33 34 35 36 37 38 39

#include "localLogFileReader.h"
#include "processOutputLogFileReader.h"
#include "kernelLogMode.h"

class LogMode;

40
class KernelAnalyzer : public FileAnalyzer
41 42 43 44
{
    Q_OBJECT

public:
Yuri Chornoivan's avatar
Yuri Chornoivan committed
45
    explicit KernelAnalyzer(LogMode *logMode)
46
        : FileAnalyzer(logMode)
47 48 49 50 51 52
    {
        startupTime();
    }

    virtual ~KernelAnalyzer() {}

Laurent Montel's avatar
Laurent Montel committed
53
    LogViewColumns initColumns() override
54 55 56 57 58 59 60 61 62 63
    {
        LogViewColumns columns;
        columns.addColumn(LogViewColumn(i18n("Date"), true, false));
        columns.addColumn(LogViewColumn(i18n("Component"), true, false));
        columns.addColumn(LogViewColumn(i18n("Message"), true, false));

        return columns;
    }

protected:
Laurent Montel's avatar
Laurent Montel committed
64
    LogFileReader *createLogFileReader(const LogFile &logFile) override
65 66 67 68
    {
        return new ProcessOutputLogFileReader(logFile);
    }

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

    void startupTime()
    {
73
        QFile file(QStringLiteral(UPTIME_FILE));
74 75 76 77 78 79 80

        file.open(QIODevice::ReadOnly | QIODevice::Text);

        QTextStream in(&file);
        QString line = in.readLine();

        // Format : 1618.72 1382.98 (uptime / something)
81
        QStringList times = line.split(QLatin1Char(' '));
82 83

        QString secondsString = times.at(0);
84
        QString pureSecondsString = secondsString.left(secondsString.indexOf(QLatin1Char('.')));
85 86 87 88 89 90
        long updateSeconds = pureSecondsString.toLong();

        startupDateTime = QDateTime::currentDateTime().addSecs(-updateSeconds);
        logDebug() << "Startup time : " << startupDateTime;
    }

Laurent Montel's avatar
Laurent Montel committed
91
    LogLine *parseMessage(const QString &logLine, const LogFile &originalLogFile) override
92
    {
93
        QRegExp timeRegex(QStringLiteral("\\[\\ *(\\d*)\\.(\\d*)\\]\\s+(.*)"));
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

        //			QRegExp componentRegexp(timeRegex + "([^\\s:]{,20})[:\\s\\t]+(.*)");
        //			QRegExp messageRegexp(timeRegex + "(.*)");

        QDateTime dateTime(startupDateTime);
        QStringList messages;

        int timeExists = timeRegex.indexIn(logLine);

        // If we have the date, we are able to update the start date
        if (timeExists != -1) {
            // logDebug() << componentRegexp.cap(1).toInt() << "and" << componentRegexp.cap(2).toInt();
            dateTime = dateTime.addSecs(timeRegex.cap(1).toInt());
            dateTime = dateTime.addMSecs(timeRegex.cap(2).toInt() / 1000);

            parseComponentMessage(timeRegex.cap(3), messages);

        }
        // Else, the date will never change
        else {
            parseComponentMessage(logLine, messages);
        }

        /*
  logDebug() << "--------------------------------";
  logDebug() << logLine;
  logDebug() << "Secs : " << dateTime.time().second();
  logDebug() << "MSec : " << dateTime.time().msec();
  logDebug() << "Comp : " << messages.at(0);
  logDebug() << "Msg  : " << messages.at(1);
  logDebug() << "--------------------------------";
        */

        LogLine *line
128
            = new LogLine(logLineInternalIdGenerator++, dateTime, messages, originalLogFile.url().toLocalFile(),
129
                          Globals::instance().informationLogLevel(), logMode);
130 131 132 133 134 135 136 137 138

        return line;
    }

    inline void parseComponentMessage(const QString &logLine, QStringList &messages)
    {
        QString message(logLine);
        QString component;

139
        int doublePointPosition = message.indexOf(QLatin1Char(':'));
140 141 142 143 144

        // Estimate the max size of a component
        if (doublePointPosition != -1 && doublePointPosition < 20) {
            component = message.left(doublePointPosition);
            // Remove component length + ": "
145
            message.remove(0, doublePointPosition + 2);
146
        }
147

148 149 150
        messages.append(component);
        messages.append(message.simplified());
    }
151

152 153
protected:
    QDateTime startupDateTime;
154 155 156
};

#endif