1 /* 
2  * Copyright 2005 Paul Hinds
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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/**
27 * A Logger class that does not report errors
28 * 
29 * @author Paul Hinds
30 * @version $Id: SimpleLogger.java,v 1.4 2007/01/19 00:24:36 teknopaul Exp $
31 */
32public class SimpleLogger implements Logger {
33
34    BufferedWriter fos;
35
36    private String fileName;
37
38    public SimpleLogger() {
39    }
40
41    /**
42     * This method initialises the logger
43     */
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    /**
06     * Called by the garbage collector on an object when garbage collection
07     * determines that there are no more references to the object.
08     * 
09     * @throws Throwable
10     *             the <code>Exception</code> raised by this method
11     * @todo Implement this java.lang.Object method
12     */
13    protected void finalize() throws Throwable {
14        if (fos != null) {
15            fos.flush();
16            fos.close();
17        }
18    }
19
20}
21