| 1 | /* ---------------------------------- readLogFile.cpp --------------------------------------------------------------------------- |
| 2 | Class to read a logfile from disk in a seperate thread |
| 3 | |
| 4 | =============================================================================================================================== |
| 5 | =============================================================================================================================== |
| 6 | This file is part of "luckyBackup" project |
| 7 | Copyright, Loukas Avgeriou |
| 8 | luckyBackup is distributed under the terms of the GNU General Public License |
| 9 | luckyBackup is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 3 of the License, or |
| 12 | (at your option) any later version. |
| 13 | |
| 14 | luckyBackup is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with luckyBackup. If not, see <http://www.gnu.org/licenses/>. |
| 21 | |
| 22 | |
| 23 | project version : Please see "main.cpp" for project version |
| 24 | |
| 25 | developer : luckyb |
| 26 | last modified : 22 May 2016 |
| 27 | =============================================================================================================================== |
| 28 | =============================================================================================================================== |
| 29 | */ |
| 30 | |
| 31 | |
| 32 | #include "readLogFile.h" |
| 33 | |
| 34 | #include <QTextStream> |
| 35 | |
| 36 | #include "global.h" |
| 37 | |
| 38 | // class ReadLogFile Constructor================================================================================================= |
| 39 | ReadLogFile::ReadLogFile(QObject *parent) : QThread(parent) |
| 40 | { |
| 41 | text = ""; |
| 42 | errorsTotal = 0; |
| 43 | } |
| 44 | |
| 45 | // class ReadLogFile Destructor================================================================================================= |
| 46 | ReadLogFile::~ReadLogFile () |
| 47 | { |
| 48 | logfile.close(); |
| 49 | } |
| 50 | |
| 51 | // run the QThread================================================================================================= |
| 52 | void ReadLogFile::run () |
| 53 | { |
| 54 | if (logUrl == "file:Does_not_exist") // if the logfile does not exist for a reason display a message |
| 55 | { |
| 56 | text = tr("Could not locate the logfile") + "!!"; |
| 57 | emit error(text); |
| 58 | return; |
| 59 | } |
| 60 | else |
| 61 | { |
| 62 | if (!logfile.open(QIODevice::ReadOnly | QIODevice::Text)) //if it exists try to open it |
| 63 | { |
| 64 | text = tr("Could not open the logfile") + "!!<br><font color=red>"+ logfile.errorString() + "</font>"; |
| 65 | emit error(text); |
| 66 | return; |
| 67 | } |
| 68 | else |
| 69 | readFile(); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | // set the log url================================================================================================= |
| 74 | void ReadLogFile::setLogUrl(QString logurl) |
| 75 | { |
| 76 | this -> logUrl = logurl; |
| 77 | } |
| 78 | |
| 79 | // Return the total errors found ================================================================================================= |
| 80 | int ReadLogFile::TotalErrors() |
| 81 | { |
| 82 | return errorsTotal; |
| 83 | } |
| 84 | |
| 85 | // Read the logfile from disk =============================================================================================== |
| 86 | void ReadLogFile::readFile() |
| 87 | { |
| 88 | QTextStream in(&logfile); |
| 89 | |
| 90 | QString readLine = ""; |
| 91 | int countLines = 0; |
| 92 | while (!in.atEnd()) // read the logfile line by line |
| 93 | { |
| 94 | readLine = in.readLine(); |
| 95 | countLines++; |
| 96 | |
| 97 | if (readLine.startsWith("<a name=\"error")) // if the line is an error increase the total errors |
| 98 | errorsTotal++; |
| 99 | |
| 100 | text.append(readLine + "<br>"); |
| 101 | |
| 102 | if ((countLines > 2000) || (in.atEnd())) //when 5000 lines are read or file is at end, append text to the logviewer |
| 103 | { |
| 104 | emit newText(text); |
| 105 | text = ""; |
| 106 | countLines = 0; |
| 107 | } |
| 108 | } |
| 109 | } |
| 110 | |
| 111 | // end of readLogFile.cpp --------------------------------------------------------------------------- |
| 112 |