2 * Copyright (C) 2001, 2002 The Mir-coders group
4 * This file is part of Mir.
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.
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.
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
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.
39 * Hilfs-Klasse, die in Logfiles schreibt.
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;
48 private static String lineSeparator;
50 private static HashMap /* filename / instance */ instanceRepository;
51 private RandomAccessFile raf;
52 private String fileName;
55 * lineSeparator ermitteln und Repository anlegen
58 // System.runFinalizersOnExit(true);
59 lineSeparator = System.getProperty("line.separator");
60 instanceRepository = new HashMap();
64 * Singleton zurueckliefern, anhand des Filenamens,
65 * also pro <code>fileName</code> wird eine Instanz der Logfileklassen
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);
78 returnLogfile = new Logfile(fileName);
79 instanceRepository.put(fileName, returnLogfile);
82 System.err.println("Fehler bei Instantiierung von Logfile");
88 * Privater Konstruktor
89 * @param String fileName
91 private Logfile(String fileName){
92 this.fileName = fileName;
94 File f = new File(fileName);
95 File dir = new File(f.getParent());
97 raf = new RandomAccessFile(fileName, "rw");
98 } catch (IOException e) {
99 System.err.println("Could not open logfile '"+fileName+"'");
104 * Private Methode, um eine Zeile auszugeben
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.
110 private void print(int type, String text) {
111 if (text == null) text = "null";
112 text = text.replace('\n', ' ');
115 type == LOG_DEBINFO ? "DEBINFO " :
116 type == LOG_INFO ? "INFO " :
117 type == LOG_WARNING ? "WARNING " :
118 type == LOG_ERROR ? "ERROR " :
121 String sectionText = text;
122 GregorianCalendar date = new GregorianCalendar();
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;
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.
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);
150 byte[] buf = line.getBytes();
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);
161 * Schreibt Information <code>text</code> ins Logfil.
164 public void printInfo (String text) {
165 print(LOG_INFO, text);
169 * Schreibt Warnung <code>text</code> ins Logfile.
172 public void printWarning (String text) {
173 print(LOG_WARNING, text);
177 * Schreibt Fehlermeldung <code>text</code> ins Logfile.
180 public void printError (String text) {
181 print(LOG_ERROR, text);
185 * Schreibt Debuginformation <code>text</code> ins Logfile.
188 public void printDebugInfo (String text) {
189 print(LOG_DEBINFO, text);
193 * Finalize-Methode, die alle offenen Dateien schliesst.
195 public void finalize () {
199 } catch (IOException e) { ; }
202 staticFinalize(fileName);
205 } catch (Throwable t) {
214 private static synchronized void staticFinalize (String fileName) {
215 instanceRepository.remove(fileName);