From 30bda04ae349b76aa420794bd2a6daa2e79468a1 Mon Sep 17 00:00:00 2001 From: Marc Baloup Date: Tue, 29 Dec 2015 02:23:35 +0100 Subject: [PATCH] =?UTF-8?q?Cr=C3=A9ation=20d'un=20outil=20de=20lecture=20d?= =?UTF-8?q?e=20log?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Ajout d'un exécutable permettant la lecture en continu d'un fichier log avec support de la rotation des fichiers. --- .classpath | 11 ++ .project | 17 +++ bin/.gitignore | 1 + src/fr/pandacube/java/logviewer/Main.java | 137 ++++++++++++++++++ .../pandacube/java/logviewer/test/Test.java | 100 +++++++++++++ 5 files changed, 266 insertions(+) create mode 100644 .classpath create mode 100644 .project create mode 100644 bin/.gitignore create mode 100644 src/fr/pandacube/java/logviewer/Main.java create mode 100644 src/fr/pandacube/java/logviewer/test/Test.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..b932b0f --- /dev/null +++ b/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..fcc21f1 --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + LogViewer + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 0000000..44fde90 --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1 @@ +/fr/ diff --git a/src/fr/pandacube/java/logviewer/Main.java b/src/fr/pandacube/java/logviewer/Main.java new file mode 100644 index 0000000..f5697d6 --- /dev/null +++ b/src/fr/pandacube/java/logviewer/Main.java @@ -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 lineBuffer = new CircularFifoQueue(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 bytes = new ArrayList(); + + 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 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); + } + + + +} diff --git a/src/fr/pandacube/java/logviewer/test/Test.java b/src/fr/pandacube/java/logviewer/test/Test.java new file mode 100644 index 0000000..121c0bc --- /dev/null +++ b/src/fr/pandacube/java/logviewer/test/Test.java @@ -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.
+ * + * 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