| 1 | /* |
| 2 | * File: LogRunner.cpp |
| 3 | * |
| 4 | * Author: Jacob Dekel |
| 5 | * Created on: Aug 7, 2009 |
| 6 | * |
| 7 | * Copyright (c) 2009-2013 Jacob Dekel |
| 8 | * $Id$ |
| 9 | * |
| 10 | * This program is free software: you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License as published by |
| 12 | * the Free Software Foundation, either version 3 of the License, or |
| 13 | * (at your option) any later version. |
| 14 | * |
| 15 | * This program is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | * GNU General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU General Public License |
| 21 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 22 | * |
| 23 | */ |
| 24 | |
| 25 | #include "LogRunner.h" |
| 26 | #include "HerculesStudio.h" |
| 27 | #include "NamedPipe.h" |
| 28 | #include "Preferences.h" |
| 29 | |
| 30 | LogRunner::LogRunner(SynchronizedQueue& logQueue, HerculesExecutor * herculesExecutor, int maxQueueSize) |
| 31 | : Runner(logQueue), mHerculesExecutor(herculesExecutor), mMaxQueueSize(maxQueueSize) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | LogRunner::~LogRunner() |
| 36 | { |
| 37 | |
| 38 | } |
| 39 | |
| 40 | void LogRunner::run() |
| 41 | { |
| 42 | mQueue.push_back("logger started"); |
| 43 | emit newData(); |
| 44 | |
| 45 | #ifdef hFramework |
| 46 | mProcess = mHerculesExecutor->getQProcess(); |
| 47 | connect(mProcess, |
| 48 | SIGNAL(readyReadStandardOutput()), |
| 49 | this, |
| 50 | SLOT(readStandardOutput())); |
| 51 | return; |
| 52 | #else |
| 53 | QFile& logFile = NamedPipe::getInstance().getHerculesLogfile(); |
| 54 | QByteArray buff; |
| 55 | buff.resize(512); |
| 56 | mRunning = true; |
| 57 | bool lineRead = false; |
| 58 | int startupCounter = 0; |
| 59 | |
| 60 | while(mRunning) |
| 61 | { |
| 62 | int size; |
| 63 | if ((size = logFile.readLine(buff.data(),512)) <= 0) |
| 64 | { |
| 65 | // we wait up to 2 seconds for data to appear in the log |
| 66 | if (!lineRead) |
| 67 | { |
| 68 | if (startupCounter++ < 200) |
| 69 | { |
| 70 | QThread::msleep(10); |
| 71 | continue; // retry reading by returning to main loop |
| 72 | } |
| 73 | } |
| 74 | mQueue.push_back("logger ended"); |
| 75 | emit newData(); |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | lineRead = true; |
| 80 | buff[size-1] = '\0'; |
| 81 | mQueue.push_back(buff); |
| 82 | emit newData(); |
| 83 | while (mQueue.size() > mMaxQueueSize) // do not flood the queue |
| 84 | QThread::msleep(100); // yield |
| 85 | } |
| 86 | #endif |
| 87 | } |
| 88 | |
| 89 | #ifdef hFramework |
| 90 | void LogRunner::readStandardOutput() |
| 91 | { |
| 92 | mProcess->setReadChannel(QProcess::StandardOutput); |
| 93 | while (true) |
| 94 | { |
| 95 | QByteArray output = mProcess->readLine(); |
| 96 | if (output.length() == 0) break; |
| 97 | #ifndef hCRLF |
| 98 | output[output.length()-1]='\0'; |
| 99 | #else |
| 100 | output[output.length()-2]='\0'; |
| 101 | #endif |
| 102 | mQueue.push_back(output.data()); |
| 103 | emit newData(); |
| 104 | } |
| 105 | } |
| 106 | #endif |