2 * put your module comment here
11 * Hilfs-Klasse, die in Logfiles schreibt.
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;
20 private static String lineSeparator;
22 private static HashMap /* filename / instance */ instanceRepository;
23 private RandomAccessFile raf;
24 private String fileName;
27 * lineSeparator ermitteln und Repository anlegen
30 // System.runFinalizersOnExit(true);
31 lineSeparator = System.getProperty("line.separator");
32 instanceRepository = new HashMap();
36 * Singleton zurueckliefern, anhand des Filenamens,
37 * also pro <code>fileName</code> wird eine Instanz der Logfileklassen
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);
50 returnLogfile = new Logfile(fileName);
51 instanceRepository.put(fileName, returnLogfile);
54 System.err.println("Fehler bei Instantiierung von Logfile");
60 * Privater Konstruktor
61 * @param String fileName
63 private Logfile(String fileName){
64 this.fileName = fileName;
66 File f = new File(fileName);
67 File dir = new File(f.getParent());
69 raf = new RandomAccessFile(fileName, "rw");
70 } catch (IOException e) {
71 System.err.println("Could not open logfile '"+fileName+"'");
76 * Private Methode, um eine Zeile auszugeben
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.
82 private void print(int type, String text) {
83 if (text == null) text = "null";
84 text = text.replace('\n', ' ');
87 type == LOG_DEBINFO ? "DEBINFO " :
88 type == LOG_INFO ? "INFO " :
89 type == LOG_WARNING ? "WARNING " :
90 type == LOG_ERROR ? "ERROR " :
93 String sectionText = text;
94 GregorianCalendar date = new GregorianCalendar();
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;
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.
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);
122 byte[] buf = line.getBytes();
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);
133 * Schreibt Information <code>text</code> ins Logfil.
136 public void printInfo (String text) {
137 print(LOG_INFO, text);
141 * Schreibt Warnung <code>text</code> ins Logfile.
144 public void printWarning (String text) {
145 print(LOG_WARNING, text);
149 * Schreibt Fehlermeldung <code>text</code> ins Logfile.
152 public void printError (String text) {
153 print(LOG_ERROR, text);
157 * Schreibt Debuginformation <code>text</code> ins Logfile.
160 public void printDebugInfo (String text) {
161 print(LOG_DEBINFO, text);
165 * Finalize-Methode, die alle offenen Dateien schliesst.
167 public void finalize () {
171 } catch (IOException e) {}
174 staticFinalize(fileName);
177 } catch (Throwable t) {
186 private static synchronized void staticFinalize (String fileName) {
187 instanceRepository.remove(fileName);