Mir goes GPL
[mir.git] / source / mir / misc / Logfile.java
1 /*
2  * Copyright (C) 2001, 2002  The Mir-coders group
3  *
4  * This file is part of Mir.
5  *
6  * Mir is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * Mir is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Mir; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * In addition, as a special exception, The Mir-coders gives permission to link
21  * the code of this program with the com.oreilly.servlet library, any library
22  * licensed under the Apache Software License, The Sun (tm) Java Advanced
23  * Imaging library (JAI), The Sun JIMI library (or with modified versions of
24  * the above that use the same license as the above), and distribute linked
25  * combinations including the two.  You must obey the GNU General Public
26  * License in all respects for all of the code used other than the above
27  * mentioned libraries.  If you modify this file, you may extend this exception
28  * to your version of the file, but you are not obligated to do so.  If you do
29  * not wish to do so, delete this exception statement from your version.
30  */
31
32 package  mir.misc;
33
34 import  java.util.*;
35 import  java.io.*;
36
37
38 /**
39  * Hilfs-Klasse, die in Logfiles schreibt.
40  *
41  */
42 public final class Logfile {
43                 public static final int LOG_INFO = 0;
44                 public static final int LOG_WARNING = 1;
45                 public static final int LOG_ERROR = 2;
46                 public static final int LOG_DEBINFO = 3;
47
48                 private static String lineSeparator;
49
50                 private static HashMap /* filename / instance */ instanceRepository;
51                 private RandomAccessFile raf;
52                 private String fileName;
53
54         /**
55          * lineSeparator ermitteln und Repository anlegen
56          */
57                 static {
58                         // System.runFinalizersOnExit(true);
59                         lineSeparator = System.getProperty("line.separator");
60                         instanceRepository = new HashMap();
61                 }
62
63         /**
64          * Singleton zurueckliefern, anhand des Filenamens,
65          * also pro <code>fileName</code> wird eine Instanz der Logfileklassen
66          * angelegt.
67          *
68          * @param fileName
69          * @return Logfile
70          */
71                 public static Logfile getInstance(String fileName) {
72                         Logfile returnLogfile = null;
73                         System.err.println(fileName);
74                         if (fileName != null) {
75                                 if (instanceRepository.containsKey(fileName)) {
76                                         returnLogfile = (Logfile) instanceRepository.get(fileName);
77                                 } else {
78                                         returnLogfile = new Logfile(fileName);
79                                         instanceRepository.put(fileName, returnLogfile);
80                                 }
81                         } else {
82                                 System.err.println("Fehler bei Instantiierung von Logfile");
83                         }
84                         return returnLogfile;
85                 }
86
87         /**
88          * Privater Konstruktor
89          * @param   String fileName
90          */
91                 private Logfile(String fileName){
92                         this.fileName = fileName;
93                         try {
94                                 File f = new File(fileName);
95                                 File dir = new File(f.getParent());
96                                 dir.mkdirs();
97                                 raf = new RandomAccessFile(fileName, "rw");
98                         } catch (IOException e) {
99                                 System.err.println("Could not open logfile '"+fileName+"'");
100                         }
101                 }
102
103         /**
104          * Private Methode, um eine Zeile auszugeben
105          *
106          * @param type  Typ der Logfilezeile (INFO, WARNING, ERROR, DEBUG)
107          * @param text  Lognachricht
108          * @todo an dieser Stelle koennte statt in das File in die Datenbank geloggt werden.
109          */
110                 private void print(int type, String text) {
111         if (text == null) text = "null";
112         text = text.replace('\n', ' ');
113
114         String typeText =
115                         type == LOG_DEBINFO ? "DEBINFO " :
116                         type == LOG_INFO    ? "INFO    " :
117                         type == LOG_WARNING ? "WARNING " :
118                         type == LOG_ERROR   ? "ERROR   " :
119                         "?       ";
120
121         String sectionText = text;
122         GregorianCalendar date = new GregorianCalendar();
123
124         String line = StringUtil.pad2(date.get(Calendar.DATE))+"-"+
125                         StringUtil.pad2(date.get(Calendar.MONTH)+1)+"-"+
126                         StringUtil.pad2(date.get(Calendar.YEAR) % 100)+" ";
127         int hour = date.get(Calendar.HOUR);
128         if (date.get(Calendar.AM_PM) == Calendar.PM) hour+=12;
129         line += StringUtil.pad2(hour)+":"+
130                         StringUtil.pad2(date.get(Calendar.MINUTE))+":"+
131                         StringUtil.pad2(date.get(Calendar.SECOND))+" "+
132                         typeText+sectionText;
133
134         print(line);
135                 }
136
137         /**
138          *  Interne Ausgabeprozedur.
139          *      Erfordert etwas Handarbeit, da PrintStream nicht mit RandomAcccessFile
140          *      kooperiert. Und ein RandomAccessFile brauchen wir, weil FileOutputStream
141          *      kein "append" zuläßt.
142          *
143          */
144                 private void print(String line) {
145         if (raf == null) return;
146         line += lineSeparator;
147         //      byte[] buf = new byte[line.length()];
148         //line.getBytes(0, line.length(), buf, 0);
149
150         byte[] buf = line.getBytes();
151
152         try {
153                         raf.seek(raf.length());
154                         raf.write(buf, 0, line.length());
155         } catch (IOException e) {
156                         System.err.print("Could not write logfile line: "+line);
157         }
158                 }
159
160         /**
161          * Schreibt Information <code>text</code> ins Logfil.
162          * @param text
163          */
164         public void printInfo (String text) {
165                 print(LOG_INFO, text);
166         }
167
168         /**
169          * Schreibt Warnung <code>text</code> ins Logfile.
170          * @param text
171          */
172         public void printWarning (String text) {
173                 print(LOG_WARNING, text);
174         }
175
176         /**
177          * Schreibt Fehlermeldung <code>text</code> ins Logfile.
178          * @param text
179          */
180         public void printError (String text) {
181                 print(LOG_ERROR, text);
182         }
183
184         /**
185          * Schreibt Debuginformation <code>text</code> ins Logfile.
186          * @param text
187          */
188         public void printDebugInfo (String text) {
189                 print(LOG_DEBINFO, text);
190         }
191
192         /**
193          * Finalize-Methode, die alle offenen Dateien schliesst.
194          */
195         public void finalize () {
196                 if (raf != null) {
197                         try {
198                                 raf.close();
199                         } catch (IOException e) { ; }
200                         raf = null;
201                 }
202                 staticFinalize(fileName);
203                 try {
204                         super.finalize();
205                 } catch (Throwable t) {
206                         ;
207                 }
208         }
209
210         /**
211          * Static-Finalizer
212          * @param fileName
213          */
214         private static synchronized void staticFinalize (String fileName) {
215                 instanceRepository.remove(fileName);
216         }
217 }
218
219
220