1775ab48d89fd53f75dbaca7bf6d4cfa2e23cfca
[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         //config var, required since it's "null"
27         //TODO: should have a defaul value once I figure out 
28         //how to disperse home dir. -mh
29         private static String logDir="/tmp/";
30
31         /**
32          * lineSeparator ermitteln und Repository anlegen
33          */
34                 static {
35                         // System.runFinalizersOnExit(true);
36                         lineSeparator = System.getProperty("line.separator");
37                         instanceRepository = new HashMap();
38                 }
39
40         /**
41          * Singleton zurueckliefern, anhand des Filenamens,
42          * also pro <code>fileName</code> wird eine Instanz der Logfileklassen
43          * angelegt.
44          *
45          * @param fileName
46          * @return Logfile
47          */
48                 public static Logfile getInstance(String fileName) {
49             //TODO: tokenize the CallerClass name (fileName)
50                         Logfile returnLogfile = null;
51                         System.err.println(fileName);
52                         if (fileName != null) {
53                                 if (instanceRepository.containsKey(fileName)) {
54                                         returnLogfile = (Logfile) instanceRepository.get(fileName);
55                                 } else {
56                                         returnLogfile = new Logfile(fileName);
57                                         instanceRepository.put(fileName, returnLogfile);
58                     Integer i = new Integer(instanceRepository.size());
59                     System.err.println("SIZZE: "+i.toString());
60                                 }
61                         } else {
62                                 System.err.println("Fehler bei Instantiierung von Logfile");
63                         }
64                         return returnLogfile;
65                 }
66
67         /**
68          * Privater Konstruktor
69          * @param   String fileName
70          */
71                 private Logfile(String file){
72                         this.fileName = logDir+file;
73                         System.err.println("FILENAME "+fileName+"DD");
74                         try {
75                                 File f = new File(this.fileName);
76                                 File dir = new File(f.getParent());
77                                 dir.mkdirs();
78                                 raf = new RandomAccessFile(fileName, "rw");
79                         } catch (IOException e) {
80                                 System.err.println("Could not open logfile '"+fileName+"'");
81                         }
82                 }
83
84         //TODO: should have a defaul value once I figure out 
85         //how to disperse home dir. -mh
86         public static void setLogDir(String dir) {
87             logDir=StringUtil.addSeparator(dir);
88                         System.err.println("set logDir "+logDir);
89         }
90
91         /**
92          * Private Methode, um eine Zeile auszugeben
93          *
94          * @param type  Typ der Logfilezeile (INFO, WARNING, ERROR, DEBUG)
95          * @param text  Lognachricht
96          * @todo an dieser Stelle koennte statt in das File in die Datenbank geloggt werden.
97          */
98                 private void print(int type, String text) {
99         if (text == null) text = "null";
100         text = text.replace('\n', ' ');
101
102         String typeText =
103                         type == LOG_DEBINFO ? "DEBINFO " :
104                         type == LOG_INFO    ? "INFO    " :
105                         type == LOG_WARNING ? "WARNING " :
106                         type == LOG_ERROR   ? "ERROR   " :
107                         "?       ";
108
109         String sectionText = text;
110         GregorianCalendar date = new GregorianCalendar();
111
112         String line = StringUtil.pad2(date.get(Calendar.DATE))+"-"+
113                         StringUtil.pad2(date.get(Calendar.MONTH)+1)+"-"+
114                         StringUtil.pad2(date.get(Calendar.YEAR) % 100)+" ";
115         int hour = date.get(Calendar.HOUR);
116         if (date.get(Calendar.AM_PM) == Calendar.PM) hour+=12;
117         line += StringUtil.pad2(hour)+":"+
118                         StringUtil.pad2(date.get(Calendar.MINUTE))+":"+
119                         StringUtil.pad2(date.get(Calendar.SECOND))+" "+
120                         typeText+sectionText;
121
122         print(line);
123                 }
124
125         /**
126          *  Interne Ausgabeprozedur.
127          *      Erfordert etwas Handarbeit, da PrintStream nicht mit RandomAcccessFile
128          *      kooperiert. Und ein RandomAccessFile brauchen wir, weil FileOutputStream
129          *      kein "append" zuläßt.
130          *
131          */
132                 private void print(String line) {
133         if (raf == null) return;
134         line += lineSeparator;
135         //      byte[] buf = new byte[line.length()];
136         //line.getBytes(0, line.length(), buf, 0);
137
138         byte[] buf = line.getBytes();
139
140         try {
141                         raf.seek(raf.length());
142                         raf.write(buf, 0, line.length());
143         } catch (IOException e) {
144                         System.err.print("Could not write logfile line: "+line);
145         }
146                 }
147
148         /**
149          * Schreibt Information <code>text</code> ins Logfil.
150          * @param text
151          */
152         public void printInfo (String text) {
153                 print(LOG_INFO, text);
154         }
155
156         /**
157          * Schreibt Warnung <code>text</code> ins Logfile.
158          * @param text
159          */
160         public void printWarning (String text) {
161                 print(LOG_WARNING, text);
162         }
163
164         /**
165          * Schreibt Fehlermeldung <code>text</code> ins Logfile.
166          * @param text
167          */
168         public void printError (String text) {
169                 print(LOG_ERROR, text);
170         }
171
172         /**
173          * Schreibt Debuginformation <code>text</code> ins Logfile.
174          * @param text
175          */
176         public void printDebugInfo (String text) {
177                 print(LOG_DEBINFO, text);
178         }
179
180         /**
181          * Finalize-Methode, die alle offenen Dateien schliesst.
182          */
183         public void finalize () {
184                 if (raf != null) {
185                         try {
186                                 raf.close();
187                         } catch (IOException e) {}
188                         raf = null;
189                 }
190                 staticFinalize(fileName);
191                 try {
192                         super.finalize();
193                 } catch (Throwable t) {
194                         ;
195                 }
196         }
197
198         /**
199          * Static-Finalizer
200          * @param fileName
201          */
202         private static synchronized void staticFinalize (String fileName) {
203                 instanceRepository.remove(fileName);
204         }
205 }
206
207
208