File: logdata.cpp

package info (click to toggle)
glogg 0.9.0-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 832 kB
  • sloc: cpp: 6,649; sh: 66; perl: 32; sed: 25; makefile: 11
file content (429 lines) | stat: -rw-r--r-- 11,916 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/*
 * Copyright (C) 2009, 2010 Nicolas Bonnefon and other contributors
 *
 * This file is part of glogg.
 *
 * glogg 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 3 of the License, or
 * (at your option) any later version.
 *
 * glogg 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 glogg.  If not, see <http://www.gnu.org/licenses/>.
 */

// This file implements LogData, the content of a log file.

#include <iostream>

#include <QFileInfo>

#include "log.h"

#include "logdata.h"
#include "logfiltereddata.h"

// Implementation of the 'start' functions for each operation

void LogData::AttachOperation::doStart(
        LogDataWorkerThread& workerThread ) const
{
    LOG(logDEBUG) << "Attaching " << filename_.toStdString();
    workerThread.attachFile( filename_ );
    workerThread.indexAll();
}

void LogData::FullIndexOperation::doStart(
        LogDataWorkerThread& workerThread ) const
{
    LOG(logDEBUG) << "Reindexing (full)";
    workerThread.indexAll();
}

void LogData::PartialIndexOperation::doStart(
        LogDataWorkerThread& workerThread ) const
{
    LOG(logDEBUG) << "Reindexing (partial)";
    workerThread.indexAdditionalLines( filesize_ );
}


// Constructs an empty log file.
// It must be displayed without error.
LogData::LogData() : AbstractLogData(), fileWatcher_(), linePosition_(),
    fileMutex_(), dataMutex_(), workerThread_()
{
    // Start with an "empty" log
    file_         = NULL;
    fileSize_     = 0;
    nbLines_      = 0;
    maxLength_    = 0;
    currentOperation_ = NULL;
    nextOperation_    = NULL;

    // Initialise the file watcher
    connect( &fileWatcher_, SIGNAL( fileChanged( const QString& ) ),
            this, SLOT( fileChangedOnDisk() ) );
    // Forward the update signal
    connect( &workerThread_, SIGNAL( indexingProgressed( int ) ),
            this, SIGNAL( loadingProgressed( int ) ) );
    connect( &workerThread_, SIGNAL( indexingFinished( bool ) ),
            this, SLOT( indexingFinished( bool ) ) );

    // Starts the worker thread
    workerThread_.start();
}

LogData::~LogData()
{
    if ( file_ )
        delete file_;
}

//
// Public functions
//

void LogData::attachFile( const QString& fileName )
{
    LOG(logDEBUG) << "LogData::attachFile " << fileName.toStdString();

    if ( file_ ) {
        // Remove the current file from the watch list
        fileWatcher_.removeFile( file_->fileName() );
    }

    workerThread_.interrupt();

    LogDataOperation* newOperation = new AttachOperation( fileName );

    // If an attach operation is already in progress, the new one will
    // be delayed untilthe current one is finished (canceled)
    enqueueOperation( newOperation );
}

void LogData::interruptLoading()
{
    workerThread_.interrupt();
}

qint64 LogData::getFileSize() const
{
    return fileSize_;
}

QDateTime LogData::getLastModifiedDate() const
{
    return lastModifiedDate_;
}

// Return an initialised LogFilteredData. The search is not started.
LogFilteredData* LogData::getNewFilteredData() const
{
    LogFilteredData* newFilteredData = new LogFilteredData( this );

    return newFilteredData;
}

// Add an operation to the queue and perform it immediately if
// there is none ongoing.
void LogData::enqueueOperation( const LogDataOperation* newOperation )
{
    if ( currentOperation_ == NULL )
    {
        // We do it immediately
        currentOperation_ = newOperation;
        startOperation();
    }
    else
    {
        // An operation is in progress...
        // ... we schedule the attach op for later
        if ( nextOperation_ != NULL )
            delete nextOperation_;
        nextOperation_ = newOperation;
    }
}

// Performs the current operation asynchronously, a indexingFinished
// signal will be received when it's finished.
void LogData::startOperation()
{
    if ( currentOperation_ )
    {
        LOG(logDEBUG) << "startOperation found something to do.";

        // If it's a full indexing ...
        // ... we invalidate the non indexed data
        if ( currentOperation_->isFull() ) {
            fileSize_     = 0;
            nbLines_      = 0;
            maxLength_    = 0;
        }

        // And let the operation do its stuff
        currentOperation_->start( workerThread_ );
    }
}

//
// Slots
//

void LogData::fileChangedOnDisk()
{
    LOG(logDEBUG) << "signalFileChanged";

    fileWatcher_.removeFile( file_->fileName() );

    const QString name = file_->fileName();
    QFileInfo info( name );

    LogDataOperation* newOperation = NULL;

    LOG(logDEBUG) << "current fileSize=" << fileSize_;
    LOG(logDEBUG) << "info file_->size()=" << info.size();
    if ( info.size() < fileSize_ ) {
        fileChangedOnDisk_ = Truncated;
        LOG(logINFO) << "File truncated";
        newOperation = new FullIndexOperation();
    }
    else if ( fileChangedOnDisk_ != DataAdded ) {
        fileChangedOnDisk_ = DataAdded;
        LOG(logINFO) << "New data on disk";
        newOperation = new PartialIndexOperation( fileSize_ );
    }

    if ( newOperation )
        enqueueOperation( newOperation );

    lastModifiedDate_ = info.lastModified();

    emit fileChanged( fileChangedOnDisk_ );
    // TODO: fileChangedOnDisk_, fileSize_
}

void LogData::indexingFinished( bool success )
{
    LOG(logDEBUG) << "Entering LogData::indexingFinished.";

    // We use the newly created file data or restore the old ones.
    // (Qt implicit copy makes this fast!)
    {
        QMutexLocker locker( &dataMutex_ );
        workerThread_.getIndexingData( &fileSize_, &maxLength_, &linePosition_ );
        nbLines_ = linePosition_.size();
    }

    LOG(logDEBUG) << "indexingFinished: " << success <<
        ", found " << nbLines_ << " lines.";

    if ( success ) {
        // Use the new filename if needed
        if ( !currentOperation_->getFilename().isNull() ) {
            QString newFileName = currentOperation_->getFilename();

            if ( file_ ) {
                QMutexLocker locker( &fileMutex_ );
                file_->setFileName( newFileName );
            }
            else {
                QMutexLocker locker( &fileMutex_ );
                file_ = new QFile( newFileName );
            }
        }

        // Update the modified date/time if the file exists
        lastModifiedDate_ = QDateTime();
        QFileInfo fileInfo( *file_ );
        if ( fileInfo.exists() )
            lastModifiedDate_ = fileInfo.lastModified();
    }

    if ( file_ ) {
        // And we watch the file for updates
        fileChangedOnDisk_ = Unchanged;
        fileWatcher_.addFile( file_->fileName() );
    }

    emit loadingFinished( success );

    // So now the operation is done, let's see if there is something
    // else to do, in which case, do it!
    if ( currentOperation_ ) {
        delete currentOperation_;
        currentOperation_ = nextOperation_;
        nextOperation_ = NULL;
    }
    else {
        LOG(logERROR) << "currentOperation_ is NULL in indexingFinished()";
    }

    if ( currentOperation_ ) {
        LOG(logDEBUG) << "indexingFinished is performing the next operation";
        startOperation();
    }
}

//
// Implementation of virtual functions
//
qint64 LogData::doGetNbLine() const
{
    return nbLines_;
}

int LogData::doGetMaxLength() const
{
    return maxLength_;
}

int LogData::doGetLineLength( qint64 line ) const
{
    if ( line >= nbLines_ ) { return 0; /* exception? */ }

    int length = doGetExpandedLineString( line ).length();

    return length;
}

QString LogData::doGetLineString( qint64 line ) const
{
    if ( line >= nbLines_ ) { return QString(); /* exception? */ }

    dataMutex_.lock();
    fileMutex_.lock();
    file_->open( QIODevice::ReadOnly );

    file_->seek( (line == 0) ? 0 : linePosition_[line-1] );

    QString string = QString( file_->readLine() );

    file_->close();
    fileMutex_.unlock();
    dataMutex_.unlock();

    string.chop( 1 );

    return string;
}

QString LogData::doGetExpandedLineString( qint64 line ) const
{
    if ( line >= nbLines_ ) { return QString(); /* exception? */ }

    dataMutex_.lock();
    fileMutex_.lock();
    file_->open( QIODevice::ReadOnly );

    file_->seek( (line == 0) ? 0 : linePosition_[line-1] );

    QByteArray rawString = file_->readLine();

    file_->close();
    fileMutex_.unlock();
    dataMutex_.unlock();

    QString string = QString( untabify( rawString.constData() ) );
    string.chop( 1 );

    return string;
}

// Note this function is also called from the LogFilteredDataWorker thread, so
// data must be protected because they are changed in the main thread (by
// indexingFinished).
QStringList LogData::doGetLines( qint64 first_line, int number ) const
{
    QStringList list;
    const qint64 last_line = first_line + number - 1;

    // LOG(logDEBUG) << "LogData::doGetLines first_line:" << first_line << " nb:" << number;

    if ( number == 0 ) {
        return QStringList();
    }

    if ( last_line >= nbLines_ ) {
        LOG(logWARNING) << "LogData::doGetLines Lines out of bound asked for";
        return QStringList(); /* exception? */
    }

    dataMutex_.lock();

    fileMutex_.lock();
    file_->open( QIODevice::ReadOnly );

    const qint64 first_byte = (first_line == 0) ? 0 : linePosition_[first_line-1];
    const qint64 last_byte  = linePosition_[last_line];
    // LOG(logDEBUG) << "LogData::doGetLines first_byte:" << first_byte << " last_byte:" << last_byte;
    file_->seek( first_byte );
    QByteArray blob = file_->read( last_byte - first_byte );

    file_->close();
    fileMutex_.unlock();

    qint64 beginning = 0;
    qint64 end = 0;
    for ( qint64 line = first_line; (line <= last_line); line++ ) {
        end = linePosition_[line] - first_byte;
        // LOG(logDEBUG) << "Getting line " << line << " beginning " << beginning << " end " << end;
        QByteArray this_line = blob.mid( beginning, end - beginning - 1 );
        // LOG(logDEBUG) << "Line is: " << QString( this_line ).toStdString();
        list.append( QString( this_line ) );
        beginning = end;
    }

    dataMutex_.unlock();

    return list;
}

QStringList LogData::doGetExpandedLines( qint64 first_line, int number ) const
{
    QStringList list;
    const qint64 last_line = first_line + number - 1;

    if ( number == 0 ) {
        return QStringList();
    }

    if ( last_line >= nbLines_ ) {
        LOG(logWARNING) << "LogData::doGetExpandedLines Lines out of bound asked for";
        return QStringList(); /* exception? */
    }

    dataMutex_.lock();

    fileMutex_.lock();
    file_->open( QIODevice::ReadOnly );

    const qint64 first_byte = (first_line == 0) ? 0 : linePosition_[first_line-1];
    const qint64 last_byte  = linePosition_[last_line];
    // LOG(logDEBUG) << "LogData::doGetExpandedLines first_byte:" << first_byte << " last_byte:" << last_byte;
    file_->seek( first_byte );
    QByteArray blob = file_->read( last_byte - first_byte );

    file_->close();
    fileMutex_.unlock();

    qint64 beginning = 0;
    qint64 end = 0;
    for ( qint64 line = first_line; (line <= last_line); line++ ) {
        end = linePosition_[line] - first_byte;
        // LOG(logDEBUG) << "Getting line " << line << " beginning " << beginning << " end " << end;
        QByteArray this_line = blob.mid( beginning, end - beginning - 1 );
        // LOG(logDEBUG) << "Line is: " << QString( this_line ).toStdString();
        list.append( untabify( this_line.constData() ) );
        beginning = end;
    }

    dataMutex_.unlock();

    return list;
}