1
16package org.tp23.antinstaller.runtime;
17
18import java.io.BufferedWriter;
19import java.io.FileWriter;
20import java.io.IOException;
21import java.io.PrintWriter;
22import java.io.StringWriter;
23
24import org.tp23.antinstaller.Installer;
25
26
32public class SimpleLogger implements Logger {
33
34 BufferedWriter fos;
35
36 private String fileName;
37
38 public SimpleLogger() {
39 }
40
41
44 public void setFileName(String fileName) {
45 this.fileName = fileName;
46 try {
47 fos = new BufferedWriter(new FileWriter(fileName, false));
48 fos.write("Logger initialized");
49 fos.newLine();
50 } catch (IOException e) {
51 fos = null;
52 }
53 }
54
55 public String getFileName() {
56 return fileName;
57 }
58
59 public void log(String message) {
60 if (fos == null) {
61 return;
62 }
63 try {
64 fos.write(message);
65 fos.newLine();
66 fos.flush();
67 } catch (Exception ex) {
68 throw new RuntimeException("Can not write to logs");
69 }
70 }
71
72 public void log(Installer installer, Throwable exception) {
73 if (installer != null && installer.isVerbose()) {
74 log(exception);
75 }
76 }
77
78 public void log(Throwable exception) {
79 if (fos == null) {
80 return;
81 }
82 try {
83 StringWriter writer = new StringWriter();
84 exception.printStackTrace(new PrintWriter(writer));
85 String s = writer.getBuffer().toString();
86 fos.write(s);
87 fos.newLine();
88 } catch (IOException ex) {
89 throw new RuntimeException("Can not write to logs");
90 }
91 }
92
93 public void close() {
94 try {
95 if (fos != null) {
96 fos.flush();
97 fos.close();
98 fos = null;
99 }
00 } catch (IOException e) {
01 System.err.println("Can't close logger");
02 }
03 }
04
05
13 protected void finalize() throws Throwable {
14 if (fos != null) {
15 fos.flush();
16 fos.close();
17 }
18 }
19
20}
21