2589e9855acf3c70fe60141834052b0f7170f5ed
[mir.git] / source / mir / misc / Logfile.java
1 /*
2  * put your module comment here
3  */
4 package  mir.misc;
5
6 import  java.util.*;
7 import  java.io.*;
8
9
10 /**
11  * Hilfs-Klasse, die in Logfiles schreibt.
12  *
13  */
14 public final class Logfile {
15                 public static final int LOG_INFO = 0;
16                 public static final int LOG_WARNING = 1;
17                 public static final int LOG_ERROR = 2;
18                 public static final int LOG_DEBINFO = 3;
19
20                 private static String lineSeparator;
21
22                 private static HashMap /* filename / instance */ instanceRepository;
23                 private RandomAccessFile raf;
24                 private String fileName;
25
26         /**
27          * lineSeparator ermitteln und Repository anlegen
28          */
29                 static {
30                         // System.runFinalizersOnExit(true);
31                         lineSeparator = System.getProperty("line.separator");
32                         instanceRepository = new HashMap();
33                 }
34
35         /**
36          * Singleton zurueckliefern, anhand des Filenamens,
37          * also pro <code>fileName</code> wird eine Instanz der Logfileklassen
38          * angelegt.
39          *
40          * @param fileName
41          * @return Logfile
42          */
43                 public static Logfile getInstance(String fileName) {
44                         Logfile returnLogfile = null;
45                         System.err.println(fileName);
46                         if (fileName != null) {
47                                 if (instanceRepository.containsKey(fileName)) {
48                                         returnLogfile = (Logfile) instanceRepository.get(fileName);
49                                 } else {
50                                         returnLogfile = new Logfile(fileName);
51                                         instanceRepository.put(fileName, returnLogfile);
52                                 }
53                         } else {
54                                 System.err.println("Fehler bei Instantiierung von Logfile");
55                         }
56                         return returnLogfile;
57                 }
58
59         /**
60          * Privater Konstruktor
61          * @param   String fileName
62          */
63                 private Logfile(String fileName){
64                         this.fileName = fileName;
65                         try {
66                                 File f = new File(fileName);
67                                 File dir = new File(f.getParent());
68                                 dir.mkdirs();
69                                 raf = new RandomAccessFile(fileName, "rw");
70                         } catch (IOException e) {
71                                 System.err.println("Could not open logfile '"+fileName+"'");
72                         }
73                 }
74
75         /**
76          * Private Methode, um eine Zeile auszugeben
77          *
78          * @param type  Typ der Logfilezeile (INFO, WARNING, ERROR, DEBUG)
79          * @param text  Lognachricht
80          * @todo an dieser Stelle koennte statt in das File in die Datenbank geloggt werden.
81          */
82                 private void print(int type, String text) {
83         if (text == null) text = "null";
84         text = text.replace('\n', ' ');
85
86         String typeText =
87                         type == LOG_DEBINFO ? "DEBINFO " :
88                         type == LOG_INFO    ? "INFO    " :
89                         type == LOG_WARNING ? "WARNING " :
90                         type == LOG_ERROR   ? "ERROR   " :
91                         "?       ";
92
93         String sectionText = text;
94         GregorianCalendar date = new GregorianCalendar();
95
96         String line = StringUtil.pad2(date.get(Calendar.DATE))+"-"+
97                         StringUtil.pad2(date.get(Calendar.MONTH)+1)+"-"+
98                         StringUtil.pad2(date.get(Calendar.YEAR) % 100)+" ";
99         int hour = date.get(Calendar.HOUR);
100         if (date.get(Calendar.AM_PM) == Calendar.PM) hour+=12;
101         line += StringUtil.pad2(hour)+":"+
102                         StringUtil.pad2(date.get(Calendar.MINUTE))+":"+
103                         StringUtil.pad2(date.get(Calendar.SECOND))+" "+
104                         typeText+sectionText;
105
106         print(line);
107                 }
108
109         /**
110          *  Interne Ausgabeprozedur.
111          *      Erfordert etwas Handarbeit, da PrintStream nicht mit RandomAcccessFile
112          *      kooperiert. Und ein RandomAccessFile brauchen wir, weil FileOutputStream
113          *      kein "append" zuläßt.
114          *
115          */
116                 private void print(String line) {
117         if (raf == null) return;
118         line += lineSeparator;
119         //      byte[] buf = new byte[line.length()];
120         //line.getBytes(0, line.length(), buf, 0);
121
122         byte[] buf = line.getBytes();
123
124         try {
125                         raf.seek(raf.length());
126                         raf.write(buf, 0, line.length());
127         } catch (IOException e) {
128                         System.err.print("Could not write logfile line: "+line);
129         }
130                 }
131
132         /**
133          * Schreibt Information <code>text</code> ins Logfil.
134          * @param text
135          */
136         public void printInfo (String text) {
137                 print(LOG_INFO, text);
138         }
139
140         /**
141          * Schreibt Warnung <code>text</code> ins Logfile.
142          * @param text
143          */
144         public void printWarning (String text) {
145                 print(LOG_WARNING, text);
146         }
147
148         /**
149          * Schreibt Fehlermeldung <code>text</code> ins Logfile.
150          * @param text
151          */
152         public void printError (String text) {
153                 print(LOG_ERROR, text);
154         }
155
156         /**
157          * Schreibt Debuginformation <code>text</code> ins Logfile.
158          * @param text
159          */
160         public void printDebugInfo (String text) {
161                 print(LOG_DEBINFO, text);
162         }
163
164         /**
165          * Finalize-Methode, die alle offenen Dateien schliesst.
166          */
167         public void finalize () {
168                 if (raf != null) {
169                         try {
170                                 raf.close();
171                         } catch (IOException e) {}
172                         raf = null;
173                 }
174                 staticFinalize(fileName);
175                 try {
176                         super.finalize();
177                 } catch (Throwable t) {
178                         ;
179                 }
180         }
181
182         /**
183          * Static-Finalizer
184          * @param fileName
185          */
186         private static synchronized void staticFinalize (String fileName) {
187                 instanceRepository.remove(fileName);
188         }
189 }
190
191
192