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;
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/";
32 * lineSeparator ermitteln und Repository anlegen
35 // System.runFinalizersOnExit(true);
36 lineSeparator = System.getProperty("line.separator");
37 instanceRepository = new HashMap();
41 * Singleton zurueckliefern, anhand des Filenamens,
42 * also pro <code>fileName</code> wird eine Instanz der Logfileklassen
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);
56 returnLogfile = new Logfile(fileName);
57 instanceRepository.put(fileName, returnLogfile);
58 Integer i = new Integer(instanceRepository.size());
59 System.err.println("SIZZE: "+i.toString());
62 System.err.println("Fehler bei Instantiierung von Logfile");
68 * Privater Konstruktor
69 * @param String fileName
71 private Logfile(String file){
72 this.fileName = logDir+file;
73 System.err.println("FILENAME "+fileName+"DD");
75 File f = new File(this.fileName);
76 File dir = new File(f.getParent());
78 raf = new RandomAccessFile(fileName, "rw");
79 } catch (IOException e) {
80 System.err.println("Could not open logfile '"+fileName+"'");
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);
92 * Private Methode, um eine Zeile auszugeben
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.
98 private void print(int type, String text) {
99 if (text == null) text = "null";
100 text = text.replace('\n', ' ');
103 type == LOG_DEBINFO ? "DEBINFO " :
104 type == LOG_INFO ? "INFO " :
105 type == LOG_WARNING ? "WARNING " :
106 type == LOG_ERROR ? "ERROR " :
109 String sectionText = text;
110 GregorianCalendar date = new GregorianCalendar();
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;
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.
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);
138 byte[] buf = line.getBytes();
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);
149 * Schreibt Information <code>text</code> ins Logfil.
152 public void printInfo (String text) {
153 print(LOG_INFO, text);
157 * Schreibt Warnung <code>text</code> ins Logfile.
160 public void printWarning (String text) {
161 print(LOG_WARNING, text);
165 * Schreibt Fehlermeldung <code>text</code> ins Logfile.
168 public void printError (String text) {
169 print(LOG_ERROR, text);
173 * Schreibt Debuginformation <code>text</code> ins Logfile.
176 public void printDebugInfo (String text) {
177 print(LOG_DEBINFO, text);
181 * Finalize-Methode, die alle offenen Dateien schliesst.
183 public void finalize () {
187 } catch (IOException e) {}
190 staticFinalize(fileName);
193 } catch (Throwable t) {
202 private static synchronized void staticFinalize (String fileName) {
203 instanceRepository.remove(fileName);