001: /*
002: * Copyright 2005 Paul Hinds
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.tp23.antinstaller.runtime;
017:
018: import java.io.BufferedWriter;
019: import java.io.FileWriter;
020: import java.io.IOException;
021: import java.io.PrintWriter;
022: import java.io.StringWriter;
023:
024: import org.tp23.antinstaller.Installer;
025:
026: /**
027: * A Logger class that does not report errors
028: *
029: * @author Paul Hinds
030: * @version $Id: SimpleLogger.java,v 1.4 2007/01/19 00:24:36 teknopaul Exp $
031: */
032: public class SimpleLogger implements Logger {
033:
034: BufferedWriter fos;
035:
036: private String fileName;
037:
038: public SimpleLogger() {
039: }
040:
041: /**
042: * This method initialises the logger
043: */
044: public void setFileName(String fileName) {
045: this .fileName = fileName;
046: try {
047: fos = new BufferedWriter(new FileWriter(fileName, false));
048: fos.write("Logger initialized");
049: fos.newLine();
050: } catch (IOException e) {
051: fos = null;
052: }
053: }
054:
055: public String getFileName() {
056: return fileName;
057: }
058:
059: public void log(String message) {
060: if (fos == null) {
061: return;
062: }
063: try {
064: fos.write(message);
065: fos.newLine();
066: fos.flush();
067: } catch (Exception ex) {
068: throw new RuntimeException("Can not write to logs");
069: }
070: }
071:
072: public void loge(int b) {
073: if (fos == null) {
074: return;
075: }
076: try {
077: fos.write(b);
078: } catch (Exception ex) {
079: throw new RuntimeException("Can not write to logs");
080: }
081: }
082:
083: public void loge(String b) {
084: if (fos == null) {
085: return;
086: }
087: try {
088: fos.write(b);
089: } catch (Exception ex) {
090: throw new RuntimeException("Can not write to logs");
091: }
092: }
093:
094: public void log(Installer installer, Throwable exception) {
095: if (installer != null && installer.isVerbose()) {
096: log(exception);
097: }
098: }
099:
100: public void log(Throwable exception) {
101: if (fos == null) {
102: return;
103: }
104: try {
105: StringWriter writer = new StringWriter();
106: exception.printStackTrace(new PrintWriter(writer));
107: String s = writer.getBuffer().toString();
108: fos.write(s);
109: fos.newLine();
110: fos.flush();
111: } catch (IOException ex) {
112: throw new RuntimeException("Can not write to logs");
113: }
114: }
115:
116: public void close() {
117: try {
118: if (fos != null) {
119: fos.flush();
120: fos.close();
121: fos = null;
122: }
123: } catch (IOException e) {
124: System.err.println("Can't close logger");
125: }
126: }
127:
128: /**
129: * Called by the garbage collector on an object when garbage collection
130: * determines that there are no more references to the object.
131: *
132: * @throws Throwable
133: * the <code>Exception</code> raised by this method
134: * @todo Implement this java.lang.Object method
135: */
136: protected void finalize() throws Throwable {
137: if (fos != null) {
138: fos.flush();
139: fos.close();
140: }
141: }
142:
143: }
|