[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
[or-cvs] [metrics-utils/master 1/4] Don't open the server log file twice.
Author: Karsten Loesing <karsten.loesing@xxxxxxx>
Date: Fri, 24 Sep 2010 12:31:10 +0200
Subject: Don't open the server log file twice.
Commit: 9d5f67e9ed3199513760156277aee1d5a3fe9c53
---
visitor/VisiTor.java | 33 +++++++++++++++------------------
1 files changed, 15 insertions(+), 18 deletions(-)
diff --git a/visitor/VisiTor.java b/visitor/VisiTor.java
index 31bffa5..0af583d 100644
--- a/visitor/VisiTor.java
+++ b/visitor/VisiTor.java
@@ -53,34 +53,34 @@ public final class VisiTor {
"[dd/MMM/yyyy:HH:mm:ss Z]");
logFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
Pattern ipAddressPattern = Pattern.compile("(\\d+\\.){3}\\d+");
- String firstLogLine = null;
+ BufferedReader webServerLogReader = null;
+ String logLine = null;
File logFile = new File(webServerLog);
if (!logFile.exists()) {
System.out.println("FAILED\nFile does not exist! Exiting!");
return;
}
try {
- BufferedReader br = new BufferedReader(new FileReader(
+ webServerLogReader = new BufferedReader(new FileReader(
webServerLog));
- firstLogLine = br.readLine();
- br.close();
+ logLine = webServerLogReader.readLine();
} catch (IOException e) {
System.out.println("FAILED\nCould not read file! Exiting!");
e.printStackTrace();
return;
}
- if (firstLogLine == null) {
+ if (logLine == null) {
System.out.println("FAILED\nLog file is empty! Exiting!");
return;
}
- if (!ipAddressPattern.matcher(firstLogLine.split(" ")[0]).matches()) {
+ if (!ipAddressPattern.matcher(logLine.split(" ")[0]).matches()) {
System.out.println("FAILED\nFirst column does not contain IP "
+ "address! Exiting!");
return;
}
try {
- logFormat.parse(firstLogLine.split(" ")[3] + " "
- + firstLogLine.split(" ")[4]);
+ logFormat.parse(logLine.split(" ")[3] + " "
+ + logLine.split(" ")[4]);
} catch (ParseException e) {
System.out.println("FAILED\nFourth column does not contain "
+ "timestamp! Exiting!");
@@ -175,15 +175,12 @@ public final class VisiTor {
isoDateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
SortedSet<String> allDates = new TreeSet<String>();
try {
- BufferedReader br = new BufferedReader(new FileReader(
- webServerLog));
BufferedWriter bw = serverLogPartTorUsers == null ? null
: new BufferedWriter(new FileWriter(serverLogPartTorUsers));
- String line = null;
boolean haveWarnedAboutMissingExitListsStart = false,
haveWarnedAboutMissingExitListsEnd = false;
- while ((line = br.readLine()) != null) {
- String[] lineParts = line.split(" ");
+ do {
+ String[] lineParts = logLine.split(" ");
long timestamp = -1L;
try {
timestamp = logFormat.parse(lineParts[3] + " " + lineParts[4]).
@@ -243,13 +240,13 @@ public final class VisiTor {
}
if (isTorUser) {
if (bw != null) {
- bw.write(line + "\n");
+ bw.write(logLine + "\n");
}
int requestsSoFar = torRequests.containsKey(currentDate)
? torRequests.get(currentDate) : 0;
torRequests.put(currentDate, requestsSoFar + 1);
- String userAgentString = line.trim().split("\"")[
- line.trim().split("\"").length - 1];
+ String userAgentString = logLine.trim().split("\"")[
+ logLine.trim().split("\"").length - 1];
for (Map.Entry<String, Pattern> e :
torbuttonUserAgents.entrySet()) {
if (e.getValue().matcher(userAgentString).matches()) {
@@ -265,8 +262,8 @@ public final class VisiTor {
? nonTorRequests.get(currentDate) : 0;
nonTorRequests.put(currentDate, requestsSoFar + 1);
}
- }
- br.close();
+ } while ((logLine = webServerLogReader.readLine()) != null);
+ webServerLogReader.close();
if (bw != null) {
bw.close();
}
--
1.7.1