Création d'un outil de lecture de log

- Ajout d'un exécutable permettant la lecture en continu d'un fichier
log avec support de la rotation des fichiers.
This commit is contained in:
Marc Baloup 2015-12-29 02:23:35 +01:00
commit 30bda04ae3
5 changed files with 266 additions and 0 deletions

11
.classpath Normal file
View File

@ -0,0 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="src" path="src"/>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="lib" path="lib/commons-collections4-4.1.jar">
<attributes>
<attribute name="javadoc_location" value="jar:platform:/resource/LogViewer/lib/commons-collections4-4.1-javadoc.jar!/"/>
</attributes>
</classpathentry>
<classpathentry kind="output" path="bin"/>
</classpath>

17
.project Normal file
View File

@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>LogViewer</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

1
bin/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
/fr/

View File

@ -0,0 +1,137 @@
package fr.pandacube.java.logviewer;
import java.io.EOFException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.collections4.queue.CircularFifoQueue;
public class Main {
public static final String ANSI_RESET = "\u001B[0m";
public static final String ANSI_ERASE_SCREEN = "\u001B[2J";
public static final String ANSI_CURSOR_TOP_LEFT = "\u001B[1;1H";
public static void main(String[] args) {
if (args.length == 0) {
outln("1 argument attendu : le fichier à lire.");
return;
}
File f = new File(args[0]);
if (!f.exists()) {
outln("Le fichier spécifié n'existe pas");
return;
}
if (!f.isFile()) {
outln("Le fichier spécifié n'est pas un fichier régulier");
return;
}
CircularFifoQueue<String> lineBuffer = new CircularFifoQueue<String>(100);
try {
RandomAccessFile reader = new RandomAccessFile(f, "r");
try {
String line;
while ((line = readUTF8LineFromFile(reader)) != null) {
lineBuffer.add(line);
}
out(ANSI_ERASE_SCREEN+ANSI_CURSOR_TOP_LEFT);
while (!lineBuffer.isEmpty())
outln(lineBuffer.poll());
long previousLength = 0;
while(true) {
if (reader.length() < previousLength) {
reader.seek(0);
}
if ((line = readUTF8LineFromFile(reader)) != null) {
outln(line);
previousLength = reader.length();
}
else {
Thread.sleep(10);
}
}
} finally {
reader.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) { }
}
public static String readUTF8LineFromFile(RandomAccessFile reader) {
List<Byte> bytes = new ArrayList<Byte>();
byte b;
try {
for (;;) {
b = reader.readByte();
if ("\n".getBytes()[0] == b)
break;
bytes.add(b);
}
} catch (EOFException e) {
} catch (IOException e) {
e.printStackTrace();
}
byte[] priBytes = new byte[bytes.size()];
for (int i=0; i<bytes.size(); i++)
priBytes[i] = bytes.get(i);
if (priBytes.length > 0)
return new String(priBytes);
return null;
}
public static void outln(Object o) {
System.out.println(o);
}
public static void out(Object o) {
System.out.print(o);
}
}

View File

@ -0,0 +1,100 @@
package fr.pandacube.java.logviewer.test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Random;
import java.util.concurrent.atomic.AtomicBoolean;
import fr.pandacube.java.logviewer.Main;
/**
* Test du logiciel LogViewer.<br/>
*
* Lance un Thread qui écrit aléatoirement dans un fichier de la même manière que dans un fichier log.
* Ce fichier est réinitialisé pour simuler la rotation des fichiers logs.
*
*
* @author Marc
*
*/
public class Test {
private static BufferedWriter wr;
public static void main(String[] args) throws Exception {
File f = new File("testFile");
f.createNewFile();
AtomicBoolean terminate = new AtomicBoolean(false);
wr = new BufferedWriter(new FileWriter(f));
Thread th = new Thread(() -> {
try {
while(!terminate.get()) {
wr.write(randomLine());
wr.flush();
Thread.sleep(r.nextInt(10));
if (r.nextInt(10) == 0) {
wr.close();
f.delete();
f.createNewFile();
wr = new BufferedWriter(new FileWriter(f));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}, "Testing Thread");
th.start();
Thread.currentThread().setName("Tested Thread");
Main.main(new String[] {"testFile"});
terminate.set(true);
th.join();
wr.close();
}
private static Random r = new Random();
private static char[] chars = new char[] {' ', ' ', ' ', 'a', 'b', 'c', 'd', 'e', 'f', '♪', '♫', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
public static String randomLine() {
int length = r.nextInt(100)+15;
String s = "";
for (int i=0; i<length; i++)
s+=chars[r.nextInt(chars.length)];
return s+'\n';
}
}