summaryrefslogtreecommitdiffstats
path: root/ksystemlog/src/defaultReader.cpp
blob: 011422cc59ac6a89622d8392d7f23de39eedfe4e (plain)
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
/***************************************************************************
 *   Copyright (C) 2005 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.             *
 ***************************************************************************/


#include <klocale.h>
#include <kmessagebox.h>

#include "logListItem.h"
#include "ksystemlogConfig.h"
#include "logLine.h"
#include "view.h"
#include "parsingHelper.h"

#include "defaultReader.h"


DefaultReader::DefaultReader(TQObject *parent, const char *name) :
	Reader(parent, name),
	buffers(NULL)
	{
	

}


DefaultReader::~DefaultReader() {
	delete buffers;
}

void DefaultReader::readLog() {
	//Each file in the Log file list are watched with the following method
	watchLogFiles();

	if (buffers!=NULL)
		delete buffers;

	if (logManager->getGroupBy()==NO_GROUP_BY)
		buffers=new LogLineList();
	else {
		buffers=new LogLineTree(logManager->getColumns(), logManager->getGroupBy(), logManager->getGroupByColumn());
	}
	

	LogFiles& files=logManager->getLogFiles();
	
	//Inform connected TQObject that the reading has begun
	emit readingBegin();
	
	//Read each file of the list, and place their lines in the buffer
	int i=files.count()-1;
	LogFile* logFile;
	while(i>=0) {
		logFile=files[i];
		kdDebug() << "Reading file " << logFile->url.path() << endl;
		this->readFile(logFile);

		i--;
	}
	
	//Synchronize the buffer once we have fill it
	logManager->synchronize(buffers);

	//Says to the buffer that we have read all files, and that
	//the next adding will come from logChanged method
	if (!buffers->isEmpty())
		buffers->setFirstReadPerformed(true);

	//Inform connected TQObject that the reading is now finished
	emit readingEnd();

	//Emit a signal which informs connected slots that there are new lines
	emit logUpdated(logManager->getView()->getLogList()->childCount());
}

void DefaultReader::readFile(LogFile* logFile) {
	TQString message=i18n("Opening file '%1'...").arg(logFile->url.path());
	emit statusbarChanged(message);

	//Inform connected TQObject that we are now reading the "index" file
	emit readingFile(logManager->getLogFiles().count() - logManager->getLogFiles().findIndex(logFile));
	
	
	//We initialize these values from configuration to be used by the insert methods
	tmpDeleteDuplicate=KSystemLogConfig::deleteDuplicatedLines();
	tmpDeleteProcessId=KSystemLogConfig::deleteProcessIdentifier();
	tmpMaxLines=KSystemLogConfig::maxLines();
	tmpMaxCharacters=KSystemLogConfig::maxReadCharacters();
	
	//Open the file
	TQFile* file=this->openFile(logFile->url.path());
	
	//If an error occurs, end this method
	if (file==NULL) {
		return;
	}
	
	TQString buffer;
	TQString filePath=logFile->url.path();
		

	//Insert the content of the file in a string list
	TQStringList* rawBuffer=getRawBuffer(file);

	//If there is no line
	if (rawBuffer->size()==0) {
		TQString message=i18n("No log line in '%1'.").arg(logFile->url.path());
		emit statusbarChanged(message);
		
		delete rawBuffer;
		return;
	}

	kdDebug() << "Testing each line..." << endl;

	TQStringList::iterator it;
	it=rawBuffer->end();
	it--;

	//Calculate how many lines we will read
	int size=rawBuffer->size();
	int stop;
	if (size>tmpMaxLines)
		stop=size-tmpMaxLines;
	else
		stop=0;
	

	//Test each line of the raw buffer
	int i=size-1;
	
	int progress=0;
	
	int progressTotal=size-1 - stop;
	int each=progressTotal / 100;
	if (each==0)
		each=progressTotal;
	
	
	while(i>=stop) {
		buffer=*it;
		
		if (insertOrReplaceLine(buffer, logFile)==false)
			break;

		it--;
		i--;
		
		if (i>=0 && i%each==0) {
			emit openingProgressed(progress++);
		}
	}

	delete rawBuffer;
	
	logFile->lastFileSize=file->size();
	
	// Close the file
	this->closeFile(file);
	
	message=i18n("Log file '%1' loaded successfully.").arg(logFile->url.path());
	emit statusbarChanged(message);

}



void DefaultReader::logChanged(LogFile* logFile) {
	kdDebug() << "SortedReader : File has been modified !" << endl;

	//We initialize these values from configuration to be used by the insert methods
	tmpDeleteDuplicate=KSystemLogConfig::deleteDuplicatedLines();
	tmpDeleteProcessId=KSystemLogConfig::deleteProcessIdentifier();
	tmpMaxLines=KSystemLogConfig::maxLines();
	tmpMaxCharacters=KSystemLogConfig::maxReadCharacters();
	
	TQString buffer;

	TQString filePath=logFile->url.path();
	TQFile* file=this->openFile(filePath);
	
	if (file==NULL)
		return;


	//If there are new lines in the file, insert only them
	if (logFile->lastFileSize <= (int) file->size()) {
		//Place the cursor to the last line opened
		file->at(logFile->lastFileSize);
		
		kdDebug() << "Retrieving a part of the file..." << endl;
		
		//Get the maximum number of line read from LogManager
		int maxLines=KSystemLogConfig::maxLines();
	
		TQString buffer;
		TQStringList* rawBuffer=getRawBuffer(file);

		//If there is no line
		if (rawBuffer->size()==0) {
			delete rawBuffer;
			return;
		}
	
		kdDebug() << "Testing each line..." << endl;
	
		TQStringList::iterator it;
		it=rawBuffer->end();
		it--;
	
		//Calculate how many lines we will read
		int size=rawBuffer->size();
		int stop=0;
		if (size>maxLines)
			stop=size-maxLines;

		//Test each line of the raw buffer
		int i=size-1;
		while(i>=stop) {
			buffer=*it;
			
			if (insertOrReplaceLine(buffer, logFile)==false)
				break;

			it--;
			i--;
		}
	
		kdDebug() << "Total read lines : " << (size-1-i) << endl;

		delete rawBuffer;
		
	
	}
	//Else reread all lines, clear log list
	else {
		buffers->clear();

		file->close();
		
		this->readFile(logFile);
	}

	int newLineCount=buffers->getNewLineCount();

	logManager->synchronize(buffers);
	
	//Get the size file for the next calculation
	logFile->lastFileSize=file->size();
	
	// Close the file
	this->closeFile(file);
	
	TQString message;
	message=i18n("Log file '%1' has changed.").arg(logFile->url.path());
	emit statusbarChanged(message);
	
	//Emit a signal which informs connected slots that there are new lines
	emit logUpdated(newLineCount);
}




bool DefaultReader::insertLine(TQString& buffer, LogFile* originalFile) {
	LogLine* line=this->parseMessage(buffer, originalFile);
	
	//Do not insert the line if it NULL
	//TODO Maybe return true (because we don't want to stop the reading for one dirty line)
	if (line==NULL)
		return(false);
	
	
	//If the Delete Duplicated Line is checked, we first test that the line is really new
	if (tmpDeleteDuplicate==true) {
		if (buffers->lineAlreadyExists(line)==true)
			return(true);
	}

	//If the line is newer, it can be inserted
	if (buffers->isNewer(line)==true) {

		if (buffers->getItemCount()<tmpMaxLines) {
			buffers->insert(line);
		}
		else {
			return(false);
		}
	}
	//If the line is older, then inserts it only if there is still space in the buffer
	else if (buffers->getItemCount()<tmpMaxLines) {
		buffers->insert(line);
	}

	return(true);
}


bool DefaultReader::insertOrReplaceLine(TQString& buffer, LogFile* originalFile) {
	LogLine* line=this->parseMessage(buffer, originalFile);
	
	//Do not insert the line if it NULL
	//TODO Maybe return true (because we don't want to stop the reading for one dirty line)
	if (line==NULL)
		return(false);

	//If the Delete Duplicated Line is checked, we first test that the line is really new
	if (tmpDeleteDuplicate==true) {
		if (buffers->lineAlreadyExists(line)==true) {
			return(true);
		}
	}

	
	//If the line is newer, it can be inserted	
	if (buffers->isNewer(line)==true) {

		if (buffers->getItemCount()<tmpMaxLines) {
			buffers->insert(line);
			return(true);
		}
		else {
			buffers->removeOldestLine();
			buffers->insert(line);
			return(true);
		}
	}
	//If the line is older, then inserts it only if there is still space in the buffer
	else if (buffers->getItemCount()<tmpMaxLines) {
		buffers->insert(line);
		return(true);
	}

	return(false);
}

/**
 * TODO Improve speed of this method (with KRegExp class for example)
 */
LogLine* DefaultReader::parseMessage(TQString& logLine, LogFile* originalFile) {
	//kdDebug() << "Please don't use parseMessage() from SortedReader class" << endl;
	
	int year=TQDate::currentDate().year();
	
	//Month number
	TQString month;
	month=logLine.left(3);
	
	logLine=logLine.remove(0, 4);
	int monthNum=ParsingHelper::parseMonthNumber(month);
	
	//Day number
	TQString day;
	
	day=logLine.left(2);
	int dayNum=day.toInt();
	
	logLine=logLine.remove(0, 3);
	
	TQDate date(year, monthNum, dayNum);
	if (!date.isValid()) {
		kdDebug() << "Malformed date" << endl;
		date=TQDate::currentDate();
	}

	//Time
	TQString stringTime;
	stringTime=logLine.left(8);
	int h=stringTime.left(2).toInt();
	stringTime.remove(0, 3);
	int m=stringTime.left(2).toInt();
	stringTime.remove(0, 3);
	int s=stringTime.left(2).toInt();
	stringTime.remove(0, 3);
	
	TQTime time(h, m, s);
	if (!time.isValid()) {
		kdDebug() << "Malformed time" << endl;
		time=TQTime::currentTime();
	}
	
	//TQStringList
	logLine=logLine.remove(0, 9);
	
	int nextSpace;
	nextSpace=logLine.find(' ');
	
	//Host name
	TQString hostname;
	
	hostname=logLine.left(nextSpace);
	
	logLine=logLine.remove(0, nextSpace+1);
	
	
	TQString process;
	TQString message;
	
	//Process name	
	nextSpace=logLine.find(':');
	if (nextSpace!=-1) {
		process=logLine.left(nextSpace);
		
		//If the delete process identifier option is enabled
		if (tmpDeleteProcessId==true) {
			int squareBracket=process.find('[');
			
			//If we find a bracket, we remove the useless part
			if (squareBracket!=-1) {
				process=process.left(squareBracket);
			}
			
		}
		logLine=logLine.remove(0, nextSpace+1);
		
		message=logLine.remove(0, 1);
	}
	//If we can't find any ':' character, it means that this line is a 
	//internal message of syslogd
	else {
		process=i18n("none");
		
		message=logLine;
	}
	

	
	
	
	TQStringList list;
	list.append(hostname);
	list.append(process);
	list.append(message);
	
	/*
	 *TODO Try to avoid that 2 columns with the same timestamps (but not the
	 *same appearance order are not sorted correctly
	 */
	/*
	LogLine* lastLine=buffers->lastLineInserted();
	if (lastLine!=NULL) {
		if (lastLine->getTime().date()==date && lastLine->getTime().time()==time)
			time=time.addMSecs(1);
	}
	*/
	
	TQString filePath=originalFile->url.path();
	LogLine* logLineObject=new LogLine(date, time, list, filePath, originalFile->level, Globals::noMode->id);
	
	return(logLineObject);
}


TQStringList* DefaultReader::getRawBuffer(TQFile* file) {
	kdDebug() << "Retrieving the raw buffer..." << endl;

	TQString buffer;
	TQString tmpBuffer;
	TQStringList* rawBuffer=new TQStringList();
	
	int res;

	//Insert the content of the file in a string list
	while(!file->atEnd()) {
	
		//Read the first MaxCharactersRead characters
		res=file->readLine(buffer, tmpMaxCharacters);
		
		//Ignore the rest of the line
		while(res==tmpMaxCharacters)
			file->readLine(tmpBuffer, tmpMaxCharacters);
		
		//Push the new buffer to the list
		rawBuffer->push_back(buffer);
	}

	kdDebug() << "Raw buffer retrieved." << endl;
	
	return(rawBuffer);
}

#include "defaultReader.moc"