Conversion en projet Maven

This commit is contained in:
2016-11-26 03:12:18 +01:00
parent 8dba570693
commit 5a5afa83bd
10 changed files with 65 additions and 7 deletions

View File

@@ -0,0 +1,147 @@
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 ((line = readUTF8LineFromFile(reader)) != null) {
outln(line);
}
else {
previousLength = reader.length();
reader.close();
Thread.sleep(250);
// tant que l'exeption est lancée, on réessaye
for(;;) {
try {
reader = new RandomAccessFile(f, "r");
break;
} catch (FileNotFoundException e) {
Thread.sleep(250);
}
}
if (reader.length() >= previousLength)
reader.seek(previousLength);
}
}
} 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,113 @@
package fr.pandacube.java.logviewer.test;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.nio.file.FileSystemException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
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(50));
if (r.nextInt(100) == 0) {
wr.close();
// tant que l'exeption est lancée, on réessaye
for(;;) {
try {
Files.move(f.toPath(), new File(r.nextInt(10)+".old").toPath(), StandardCopyOption.ATOMIC_MOVE);
break;
} catch (FileSystemException e) {
Thread.sleep(250);
}
}
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', 'g', 'h', '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';
}
}