[19253] | 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 | */
|
---|
| 16 | package org.tp23.antinstaller.runtime;
|
---|
| 17 |
|
---|
| 18 | import java.io.BufferedWriter;
|
---|
| 19 | import java.io.FileWriter;
|
---|
| 20 | import java.io.IOException;
|
---|
| 21 | import java.io.PrintWriter;
|
---|
| 22 | import java.io.StringWriter;
|
---|
| 23 |
|
---|
| 24 | import 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 | */
|
---|
| 32 | public 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 | }
|
---|
| 100 | } catch (IOException e) {
|
---|
| 101 | System.err.println("Can't close logger");
|
---|
| 102 | }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
| 105 | /**
|
---|
| 106 | * Called by the garbage collector on an object when garbage collection
|
---|
| 107 | * determines that there are no more references to the object.
|
---|
| 108 | *
|
---|
| 109 | * @throws Throwable
|
---|
| 110 | * the <code>Exception</code> raised by this method
|
---|
| 111 | * @todo Implement this java.lang.Object method
|
---|
| 112 | */
|
---|
| 113 | protected void finalize() throws Throwable {
|
---|
| 114 | if (fos != null) {
|
---|
| 115 | fos.flush();
|
---|
| 116 | fos.close();
|
---|
| 117 | }
|
---|
| 118 | }
|
---|
| 119 |
|
---|
| 120 | }
|
---|