summaryrefslogtreecommitdiffstats
path: root/ksystemlog/src/apache/apacheReader.cpp
blob: 591f6ce39faaf674be9e22738730d60d92ba263d (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
/***************************************************************************
 *   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 "parsingHelper.h"
#include "apacheReader.h"

ApacheReader::ApacheReader(QObject *parent, const char *name) : 
	DefaultReader(parent, name)
	{
	
	initializeTypeLevels();
}


ApacheReader::~ApacheReader() {

}


void ApacheReader::initColumns(LogViewColumns* columns) {
	columns->append(new LogViewColumn(i18n("Date"), true, false));
	columns->append(new LogViewColumn(i18n("Client"), true, false));
	columns->append(new LogViewColumn(i18n("Message"), true, false));

}

LogLine* ApacheReader::parseMessage(QString& logLine, LogFile* logFile) {

	/*
	 * Log line examples :
	 * [Wed May 18 22:16:02 2005] [error] [client 127.0.0.1] File does not exist: /var/www/html/ksystemlog/screenshots/small/kernel-view.png, referer: http://localhost.localdomain/ksystemlog/screenshots.php
	 * [Wed May 18 22:16:02 2005] [error] [client 127.0.0.1] File does not exist: /var/www/html/ksystemlog/screenshots/small/system-filter.png, referer: http://localhost.localdomain/ksystemlog/screenshots.php
	 * [Thu May 19 18:00:19 2005] [notice] mod_jk2.post_config() first invocation
	 * [Thu May 19 18:00:19 2005] [notice] Digest: generating secret for digest authentication ...
	 * [client 127.0.0.1] PHP Parse error:  parse error, unexpected T_PRIVATE, expecting T_STRING in /mnt/boulot/web/annivernet/src/fonctions/formulaire.inc.php on line 25
	 */
	
	
	QDate date;
	QTime time;
	
	QString level;
	
	//Temporary variable
	int squareBracket;
	
	//Special case which sometimes happens
	if (logLine.find("[client")==0) {
		date=QDate::currentDate();
		time=QTime::currentTime();
		level="notice";
	}
	else {
	
		//The Date
		int dateBegin=logLine.find("[");
		int dateEnd=logLine.find("]");
		
		QString type;
		QString message;
	
	
		QString strDate=logLine.mid(dateBegin+1, dateEnd-dateBegin-1);
		
		QString month=strDate.mid(4, 3);
		
		QString day=strDate.mid(8, 2);
		
		QString hour=strDate.mid(11, 2);
		QString min=strDate.mid(14, 2);
		QString sec=strDate.mid(17, 2);
		
		QString year=strDate.mid(20, 4);
	
		date=QDate(year.toInt(), ParsingHelper::parseMonthNumber(month), day.toInt());
		time=QTime(hour.toInt(), min.toInt(), sec.toInt());
	
		logLine=logLine.remove(0, dateEnd+3);
	

		//The log level
		
		squareBracket=logLine.find("]");
		level=logLine.left(squareBracket);
		logLine=logLine.remove(0, squareBracket+2);
	}
	
	//The client
	int beginSquareBracket=logLine.find("[client");
	squareBracket=logLine.find("]");
	QString client;
	if (beginSquareBracket==-1 || squareBracket==-1) {
		client="";
	}
	else {
		client=logLine.mid(8, squareBracket-8); //8=strlen("[client ")
		logLine=logLine.remove(0, squareBracket+2);
	}
	

	QStringList list;
	list.push_back(client);
	list.push_back(logLine);
	
	QString filePath=logFile->url.path();
		
	LogLine* line=new LogLine(date, time, list, filePath, getTypeLevel(level), Globals::apacheMode->id);
	
	return(line);
}


void ApacheReader::initializeTypeLevels() {
	mapTypeLevels["notice"]=Globals::informationLogLevel;
	mapTypeLevels["warn"]=Globals::warningLogLevel;
	mapTypeLevels["error"]=Globals::errorLogLevel;
}

LogLevel* ApacheReader::getTypeLevel(const QString& type) {
	QMap<QString, LogLevel*>::iterator it;
	
	it=mapTypeLevels.find(type);
	if (it!=mapTypeLevels.end()) {
		return(*it);
	}
	else {
		kdDebug() << i18n("New Log Level detected: Please send this log file to the KSystemLog developer to add it.") << endl;
		return(Globals::noneLogLevel);
	}
}


#include "apacheReader.moc"