1 /* 2 * Copyright (C) 2009, 2010, 2013, 2014, 2015 Nicolas Bonnefon and other contributors 3 * 4 * This file is part of glogg. 5 * 6 * glogg is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * 11 * glogg is distributed in the hope that it will be useful, 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 * GNU General Public License for more details. 15 * 16 * You should have received a copy of the GNU General Public License 17 * along with glogg. If not, see <http://www.gnu.org/licenses/>. 18 */ 19 20 // This file implements LogData, the content of a log file. 21 22 #include <iostream> 23 24 #include <cassert> 25 26 #include <QFileInfo> 27 28 #include "log.h" 29 30 #include "logdata.h" 31 #include "logfiltereddata.h" 32 #if defined(GLOGG_SUPPORTS_INOTIFY) || defined(GLOGG_SUPPORTS_KQUEUE) || defined(WIN32) 33 #include "platformfilewatcher.h" 34 #else 35 #include "qtfilewatcher.h" 36 #endif 37 38 // Implementation of the 'start' functions for each operation 39 40 void LogData::AttachOperation::doStart( 41 LogDataWorkerThread& workerThread ) const 42 { 43 LOG(logDEBUG) << "Attaching " << filename_.toStdString(); 44 workerThread.attachFile( filename_ ); 45 workerThread.indexAll(); 46 } 47 48 void LogData::FullIndexOperation::doStart( 49 LogDataWorkerThread& workerThread ) const 50 { 51 LOG(logDEBUG) << "Reindexing (full)"; 52 workerThread.indexAll(); 53 } 54 55 void LogData::PartialIndexOperation::doStart( 56 LogDataWorkerThread& workerThread ) const 57 { 58 LOG(logDEBUG) << "Reindexing (partial)"; 59 workerThread.indexAdditionalLines(); 60 } 61 62 63 // Constructs an empty log file. 64 // It must be displayed without error. 65 LogData::LogData() : AbstractLogData(), indexing_data_(), 66 fileMutex_(), workerThread_( &indexing_data_ ) 67 { 68 // Start with an "empty" log 69 attached_file_ = nullptr; 70 currentOperation_ = nullptr; 71 nextOperation_ = nullptr; 72 73 codec_ = QTextCodec::codecForName( "ISO-8859-1" ); 74 75 #if defined(GLOGG_SUPPORTS_INOTIFY) || defined(GLOGG_SUPPORTS_KQUEUE) || defined(WIN32) 76 fileWatcher_ = std::make_shared<PlatformFileWatcher>(); 77 #else 78 fileWatcher_ = std::make_shared<QtFileWatcher>(); 79 #endif 80 81 // Initialise the file watcher 82 connect( fileWatcher_.get(), SIGNAL( fileChanged( const QString& ) ), 83 this, SLOT( fileChangedOnDisk() ) ); 84 // Forward the update signal 85 connect( &workerThread_, SIGNAL( indexingProgressed( int ) ), 86 this, SIGNAL( loadingProgressed( int ) ) ); 87 connect( &workerThread_, SIGNAL( indexingFinished( LoadingStatus ) ), 88 this, SLOT( indexingFinished( LoadingStatus ) ) ); 89 90 // Starts the worker thread 91 workerThread_.start(); 92 } 93 94 LogData::~LogData() 95 { 96 // Remove the current file from the watch list 97 if ( attached_file_ ) 98 fileWatcher_->removeFile( attached_file_->fileName() ); 99 100 // FIXME 101 // workerThread_.stop(); 102 } 103 104 // 105 // Public functions 106 // 107 108 void LogData::attachFile( const QString& fileName ) 109 { 110 LOG(logDEBUG) << "LogData::attachFile " << fileName.toStdString(); 111 112 if ( attached_file_ ) { 113 // We cannot reattach 114 throw CantReattachErr(); 115 } 116 117 attached_file_.reset( new QFile( fileName ) ); 118 attached_file_->open( QIODevice::ReadOnly ); 119 120 std::shared_ptr<const LogDataOperation> operation( new AttachOperation( fileName ) ); 121 enqueueOperation( std::move( operation ) ); 122 } 123 124 void LogData::interruptLoading() 125 { 126 workerThread_.interrupt(); 127 } 128 129 qint64 LogData::getFileSize() const 130 { 131 return indexing_data_.getSize(); 132 } 133 134 QDateTime LogData::getLastModifiedDate() const 135 { 136 return lastModifiedDate_; 137 } 138 139 // Return an initialised LogFilteredData. The search is not started. 140 LogFilteredData* LogData::getNewFilteredData() const 141 { 142 LogFilteredData* newFilteredData = new LogFilteredData( this ); 143 144 return newFilteredData; 145 } 146 147 void LogData::reload() 148 { 149 workerThread_.interrupt(); 150 151 enqueueOperation( std::make_shared<FullIndexOperation>() ); 152 } 153 154 void LogData::setPollingInterval( uint32_t interval_ms ) 155 { 156 fileWatcher_->setPollingInterval( interval_ms ); 157 } 158 159 // 160 // Private functions 161 // 162 163 // Add an operation to the queue and perform it immediately if 164 // there is none ongoing. 165 void LogData::enqueueOperation( std::shared_ptr<const LogDataOperation> new_operation ) 166 { 167 if ( currentOperation_ == nullptr ) 168 { 169 // We do it immediately 170 currentOperation_ = new_operation; 171 startOperation(); 172 } 173 else 174 { 175 // An operation is in progress... 176 // ... we schedule the attach op for later 177 nextOperation_ = new_operation; 178 } 179 } 180 181 // Performs the current operation asynchronously, a indexingFinished 182 // signal will be received when it's finished. 183 void LogData::startOperation() 184 { 185 if ( currentOperation_ ) 186 { 187 LOG(logDEBUG) << "startOperation found something to do."; 188 189 // Let the operation do its stuff 190 currentOperation_->start( workerThread_ ); 191 } 192 } 193 194 // 195 // Slots 196 // 197 198 void LogData::fileChangedOnDisk() 199 { 200 LOG(logDEBUG) << "signalFileChanged"; 201 202 const QString name = attached_file_->fileName(); 203 QFileInfo info( name ); 204 205 // Need to open the file in case it was absent 206 attached_file_->open( QIODevice::ReadOnly ); 207 208 std::shared_ptr<LogDataOperation> newOperation; 209 210 qint64 file_size = indexing_data_.getSize(); 211 LOG(logDEBUG) << "current fileSize=" << file_size; 212 LOG(logDEBUG) << "info file_->size()=" << info.size(); 213 if ( info.size() < file_size ) { 214 fileChangedOnDisk_ = Truncated; 215 LOG(logINFO) << "File truncated"; 216 newOperation = std::make_shared<FullIndexOperation>(); 217 } 218 else if ( info.size() == file_size ) { 219 LOG(logINFO) << "No change in file"; 220 } 221 else if ( fileChangedOnDisk_ != DataAdded ) { 222 fileChangedOnDisk_ = DataAdded; 223 LOG(logINFO) << "New data on disk"; 224 newOperation = std::make_shared<PartialIndexOperation>(); 225 } 226 227 if ( newOperation ) { 228 enqueueOperation( newOperation ); 229 lastModifiedDate_ = info.lastModified(); 230 231 emit fileChanged( fileChangedOnDisk_ ); 232 } 233 } 234 235 void LogData::indexingFinished( LoadingStatus status ) 236 { 237 LOG(logDEBUG) << "indexingFinished: " << 238 ( status == LoadingStatus::Successful ) << 239 ", found " << indexing_data_.getNbLines() << " lines."; 240 241 if ( status == LoadingStatus::Successful ) { 242 // Start watching we watch the file for updates 243 fileChangedOnDisk_ = Unchanged; 244 fileWatcher_->addFile( attached_file_->fileName() ); 245 246 // Update the modified date/time if the file exists 247 lastModifiedDate_ = QDateTime(); 248 QFileInfo fileInfo( *attached_file_ ); 249 if ( fileInfo.exists() ) 250 lastModifiedDate_ = fileInfo.lastModified(); 251 } 252 253 // FIXME be cleverer here as a notification might have arrived whilst we 254 // were indexing. 255 fileChangedOnDisk_ = Unchanged; 256 257 LOG(logDEBUG) << "Sending indexingFinished."; 258 emit loadingFinished( status ); 259 260 // So now the operation is done, let's see if there is something 261 // else to do, in which case, do it! 262 assert( currentOperation_ ); 263 264 currentOperation_ = std::move( nextOperation_ ); 265 nextOperation_.reset(); 266 267 if ( currentOperation_ ) { 268 LOG(logDEBUG) << "indexingFinished is performing the next operation"; 269 startOperation(); 270 } 271 } 272 273 // 274 // Implementation of virtual functions 275 // 276 qint64 LogData::doGetNbLine() const 277 { 278 return indexing_data_.getNbLines(); 279 } 280 281 int LogData::doGetMaxLength() const 282 { 283 return indexing_data_.getMaxLength(); 284 } 285 286 int LogData::doGetLineLength( qint64 line ) const 287 { 288 if ( line >= indexing_data_.getNbLines() ) { return 0; /* exception? */ } 289 290 int length = doGetExpandedLineString( line ).length(); 291 292 return length; 293 } 294 295 void LogData::doSetDisplayEncoding( Encoding encoding ) 296 { 297 LOG(logDEBUG) << "AbstractLogData::setDisplayEncoding: " << static_cast<int>( encoding ); 298 299 static const char* latin1_encoding = "iso-8859-1"; 300 static const char* utf8_encoding = "utf-8"; 301 static const char* utf16le_encoding = "utf-16le"; 302 static const char* utf16be_encoding = "utf-16be"; 303 static const char* cp1251_encoding = "CP1251"; 304 static const char* cp1252_encoding = "CP1252"; 305 static const char* big5_encoding = "Big5"; 306 static const char* gb18030_encoding = "GB18030"; 307 static const char* shiftJIS_encoding = "Shift-JIS"; 308 static const char* koi8r_encoding = "KOI8-R"; 309 310 const char* qt_encoding = latin1_encoding; 311 312 // Default to 0, for 8bit encodings 313 int before_cr = 0; 314 int after_cr = 0; 315 316 switch ( encoding ) { 317 case Encoding::ENCODING_UTF8: 318 qt_encoding = utf8_encoding; 319 break; 320 case Encoding::ENCODING_UTF16LE: 321 qt_encoding = utf16le_encoding; 322 before_cr = 0; 323 after_cr = 1; 324 break; 325 case Encoding::ENCODING_UTF16BE: 326 qt_encoding = utf16be_encoding; 327 before_cr = 1; 328 after_cr = 0; 329 break; 330 case Encoding::ENCODING_CP1251: 331 qt_encoding = cp1251_encoding; 332 break; 333 case Encoding::ENCODING_CP1252: 334 qt_encoding = cp1252_encoding; 335 break; 336 case Encoding::ENCODING_BIG5: 337 qt_encoding = big5_encoding; 338 break; 339 case Encoding::ENCODING_GB18030: 340 qt_encoding = gb18030_encoding; 341 break; 342 case Encoding::ENCODING_SHIFT_JIS: 343 qt_encoding = shiftJIS_encoding; 344 break; 345 case Encoding::ENCODING_KOI8R: 346 qt_encoding = koi8r_encoding; 347 break; 348 case Encoding::ENCODING_ISO_8859_1: 349 qt_encoding = latin1_encoding; 350 break; 351 default: 352 LOG( logERROR ) << "Unknown encoding set!"; 353 assert( false ); 354 break; 355 } 356 357 doSetMultibyteEncodingOffsets( before_cr, after_cr ); 358 codec_ = QTextCodec::codecForName( qt_encoding ); 359 } 360 361 void LogData::doSetMultibyteEncodingOffsets( int before_cr, int after_cr ) 362 { 363 before_cr_offset_ = before_cr; 364 after_cr_offset_ = after_cr; 365 } 366 367 QString LogData::doGetLineString( qint64 line ) const 368 { 369 if ( line >= indexing_data_.getNbLines() ) { return 0; /* exception? */ } 370 371 fileMutex_.lock(); 372 373 // end_byte is non-inclusive.(is not read) 374 const qint64 first_byte = (line == 0) ? 375 0 : ( indexing_data_.getPosForLine( line-1 ) + after_cr_offset_ ); 376 const qint64 end_byte = endOfLinePosition( line ); 377 378 attached_file_->seek( first_byte ); 379 380 QString string = codec_->toUnicode( attached_file_->read( end_byte - first_byte ) ); 381 382 fileMutex_.unlock(); 383 384 return string; 385 } 386 387 QString LogData::doGetExpandedLineString( qint64 line ) const 388 { 389 if ( line >= indexing_data_.getNbLines() ) { return 0; /* exception? */ } 390 391 fileMutex_.lock(); 392 393 // end_byte is non-inclusive.(is not read) We also exclude the final \r. 394 const qint64 first_byte = (line == 0) ? 395 0 : ( indexing_data_.getPosForLine( line-1 ) + after_cr_offset_ ); 396 const qint64 end_byte = endOfLinePosition( line ); 397 398 attached_file_->seek( first_byte ); 399 400 // LOG(logDEBUG) << "LogData::doGetExpandedLineString first_byte:" << first_byte << " end_byte:" << end_byte; 401 QByteArray rawString = attached_file_->read( end_byte - first_byte ); 402 403 fileMutex_.unlock(); 404 405 QString string = untabify( codec_->toUnicode( rawString ) ); 406 407 // LOG(logDEBUG) << "doGetExpandedLineString Line is: " << string.toStdString(); 408 409 return string; 410 } 411 412 // Note this function is also called from the LogFilteredDataWorker thread, so 413 // data must be protected because they are changed in the main thread (by 414 // indexingFinished). 415 QStringList LogData::doGetLines( qint64 first_line, int number ) const 416 { 417 QStringList list; 418 const qint64 last_line = first_line + number - 1; 419 420 // LOG(logDEBUG) << "LogData::doGetLines first_line:" << first_line << " nb:" << number; 421 422 if ( number == 0 ) { 423 return QStringList(); 424 } 425 426 if ( last_line >= indexing_data_.getNbLines() ) { 427 LOG(logWARNING) << "LogData::doGetLines Lines out of bound asked for"; 428 return QStringList(); /* exception? */ 429 } 430 431 fileMutex_.lock(); 432 433 const qint64 first_byte = (first_line == 0) ? 434 0 : ( indexing_data_.getPosForLine( first_line-1 ) + after_cr_offset_ ); 435 const qint64 end_byte = endOfLinePosition( last_line ); 436 // LOG(logDEBUG) << "LogData::doGetLines first_byte:" << first_byte << " end_byte:" << end_byte; 437 attached_file_->seek( first_byte ); 438 QByteArray blob = attached_file_->read( end_byte - first_byte ); 439 440 fileMutex_.unlock(); 441 442 qint64 beginning = 0; 443 qint64 end = 0; 444 for ( qint64 line = first_line; (line <= last_line); line++ ) { 445 end = endOfLinePosition( line ) - first_byte; 446 // LOG(logDEBUG) << "Getting line " << line << " beginning " << beginning << " end " << end; 447 QByteArray this_line = blob.mid( beginning, end - beginning ); 448 // LOG(logDEBUG) << "Line is: " << QString( this_line ).toStdString(); 449 list.append( codec_->toUnicode( this_line ) ); 450 beginning = beginningOfNextLine( end ); 451 } 452 453 return list; 454 } 455 456 QStringList LogData::doGetExpandedLines( qint64 first_line, int number ) const 457 { 458 QStringList list; 459 const qint64 last_line = first_line + number - 1; 460 461 if ( number == 0 ) { 462 return QStringList(); 463 } 464 465 if ( last_line >= indexing_data_.getNbLines() ) { 466 LOG(logWARNING) << "LogData::doGetExpandedLines Lines out of bound asked for"; 467 return QStringList(); /* exception? */ 468 } 469 470 fileMutex_.lock(); 471 472 // end_byte is non-inclusive.(is not read) 473 const qint64 first_byte = (first_line == 0) ? 474 0 : ( indexing_data_.getPosForLine( first_line-1 ) + after_cr_offset_ ); 475 const qint64 end_byte = endOfLinePosition( last_line ); 476 LOG(logDEBUG) << "LogData::doGetExpandedLines first_byte:" << first_byte << " end_byte:" << end_byte; 477 478 attached_file_->seek( first_byte ); 479 QByteArray blob = attached_file_->read( end_byte - first_byte ); 480 481 fileMutex_.unlock(); 482 483 qint64 beginning = 0; 484 qint64 end = 0; 485 for ( qint64 line = first_line; (line <= last_line); line++ ) { 486 // end is non-inclusive 487 // LOG(logDEBUG) << "EoL " << line << ": " << indexing_data_.getPosForLine( line ); 488 end = endOfLinePosition( line ) - first_byte; 489 // LOG(logDEBUG) << "Getting line " << line << " beginning " << beginning << " end " << end; 490 QByteArray this_line = blob.mid( beginning, end - beginning ); 491 QString conv_line = codec_->toUnicode( this_line ); 492 // LOG(logDEBUG) << "Line is: " << conv_line.toStdString(); 493 list.append( untabify( conv_line ) ); 494 beginning = beginningOfNextLine( end ); 495 } 496 497 return list; 498 } 499 500 EncodingSpeculator::Encoding LogData::getDetectedEncoding() const 501 { 502 return indexing_data_.getEncodingGuess(); 503 } 504 505 // Given a line number, returns the position (offset in file) of 506 // the byte immediately past its end. 507 // e.g. in utf-16: T e s t \n2 n d l i n e \n 508 // -------------------------- 509 // ^ 510 // endOfLinePosition( 0 ) 511 qint64 LogData::endOfLinePosition( qint64 line ) const 512 { 513 return indexing_data_.getPosForLine( line ) - 1 - before_cr_offset_; 514 } 515 516 // Given the position (offset in file) of the end of a line, returns 517 // the position of the beginning of the following, taking into account 518 // encoding and newline signalling. 519 qint64 LogData::beginningOfNextLine( qint64 end_pos ) const 520 { 521 return end_pos + 1 + before_cr_offset_ + after_cr_offset_; 522 } 523