Initial revision
[mir.git] / source / mir / misc / Logfile.java
diff --git a/source/mir/misc/Logfile.java b/source/mir/misc/Logfile.java
new file mode 100755 (executable)
index 0000000..2589e98
--- /dev/null
@@ -0,0 +1,192 @@
+/*
+ * put your module comment here
+ */
+package  mir.misc;
+
+import  java.util.*;
+import  java.io.*;
+
+
+/**
+ * Hilfs-Klasse, die in Logfiles schreibt.
+ *
+ */
+public final class Logfile {
+               public static final int LOG_INFO = 0;
+               public static final int LOG_WARNING = 1;
+               public static final int LOG_ERROR = 2;
+               public static final int LOG_DEBINFO = 3;
+
+               private static String lineSeparator;
+
+               private static HashMap /* filename / instance */ instanceRepository;
+               private RandomAccessFile raf;
+               private String fileName;
+
+       /**
+        * lineSeparator ermitteln und Repository anlegen
+        */
+               static {
+                       // System.runFinalizersOnExit(true);
+                       lineSeparator = System.getProperty("line.separator");
+                       instanceRepository = new HashMap();
+               }
+
+       /**
+        * Singleton zurueckliefern, anhand des Filenamens,
+        * also pro <code>fileName</code> wird eine Instanz der Logfileklassen
+        * angelegt.
+        *
+        * @param fileName
+        * @return Logfile
+        */
+               public static Logfile getInstance(String fileName) {
+                       Logfile returnLogfile = null;
+                       System.err.println(fileName);
+                       if (fileName != null) {
+                               if (instanceRepository.containsKey(fileName)) {
+                                       returnLogfile = (Logfile) instanceRepository.get(fileName);
+                               } else {
+                                       returnLogfile = new Logfile(fileName);
+                                       instanceRepository.put(fileName, returnLogfile);
+                               }
+                       } else {
+                               System.err.println("Fehler bei Instantiierung von Logfile");
+                       }
+                       return returnLogfile;
+               }
+
+       /**
+        * Privater Konstruktor
+        * @param   String fileName
+        */
+               private Logfile(String fileName){
+                       this.fileName = fileName;
+                       try {
+                               File f = new File(fileName);
+                               File dir = new File(f.getParent());
+                               dir.mkdirs();
+                               raf = new RandomAccessFile(fileName, "rw");
+                       } catch (IOException e) {
+                               System.err.println("Could not open logfile '"+fileName+"'");
+                       }
+               }
+
+       /**
+        * Private Methode, um eine Zeile auszugeben
+        *
+        * @param type  Typ der Logfilezeile (INFO, WARNING, ERROR, DEBUG)
+        * @param text  Lognachricht
+        * @todo an dieser Stelle koennte statt in das File in die Datenbank geloggt werden.
+        */
+               private void print(int type, String text) {
+       if (text == null) text = "null";
+       text = text.replace('\n', ' ');
+
+       String typeText =
+                       type == LOG_DEBINFO ? "DEBINFO " :
+                       type == LOG_INFO    ? "INFO    " :
+                       type == LOG_WARNING ? "WARNING " :
+                       type == LOG_ERROR   ? "ERROR   " :
+                       "?       ";
+
+       String sectionText = text;
+       GregorianCalendar date = new GregorianCalendar();
+
+       String line = StringUtil.pad2(date.get(Calendar.DATE))+"-"+
+                       StringUtil.pad2(date.get(Calendar.MONTH)+1)+"-"+
+                       StringUtil.pad2(date.get(Calendar.YEAR) % 100)+" ";
+       int hour = date.get(Calendar.HOUR);
+       if (date.get(Calendar.AM_PM) == Calendar.PM) hour+=12;
+       line += StringUtil.pad2(hour)+":"+
+                       StringUtil.pad2(date.get(Calendar.MINUTE))+":"+
+                       StringUtil.pad2(date.get(Calendar.SECOND))+" "+
+                       typeText+sectionText;
+
+       print(line);
+               }
+
+       /**
+        *  Interne Ausgabeprozedur.
+        *      Erfordert etwas Handarbeit, da PrintStream nicht mit RandomAcccessFile
+        *      kooperiert. Und ein RandomAccessFile brauchen wir, weil FileOutputStream
+        *      kein "append" zuläßt.
+        *
+        */
+               private void print(String line) {
+       if (raf == null) return;
+       line += lineSeparator;
+       //      byte[] buf = new byte[line.length()];
+       //line.getBytes(0, line.length(), buf, 0);
+
+       byte[] buf = line.getBytes();
+
+       try {
+                       raf.seek(raf.length());
+                       raf.write(buf, 0, line.length());
+       } catch (IOException e) {
+                       System.err.print("Could not write logfile line: "+line);
+       }
+               }
+
+       /**
+        * Schreibt Information <code>text</code> ins Logfil.
+        * @param text
+        */
+       public void printInfo (String text) {
+               print(LOG_INFO, text);
+       }
+
+       /**
+        * Schreibt Warnung <code>text</code> ins Logfile.
+        * @param text
+        */
+       public void printWarning (String text) {
+               print(LOG_WARNING, text);
+       }
+
+       /**
+        * Schreibt Fehlermeldung <code>text</code> ins Logfile.
+        * @param text
+        */
+       public void printError (String text) {
+               print(LOG_ERROR, text);
+       }
+
+       /**
+        * Schreibt Debuginformation <code>text</code> ins Logfile.
+        * @param text
+        */
+       public void printDebugInfo (String text) {
+               print(LOG_DEBINFO, text);
+       }
+
+       /**
+        * Finalize-Methode, die alle offenen Dateien schliesst.
+        */
+       public void finalize () {
+               if (raf != null) {
+                       try {
+                               raf.close();
+                       } catch (IOException e) {}
+                       raf = null;
+               }
+               staticFinalize(fileName);
+               try {
+                       super.finalize();
+               } catch (Throwable t) {
+                       ;
+               }
+       }
+
+       /**
+        * Static-Finalizer
+        * @param fileName
+        */
+       private static synchronized void staticFinalize (String fileName) {
+               instanceRepository.remove(fileName);
+       }
+}
+
+
+