001: package org.apache.maven.embedder;
002:
003: /*
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021:
022: import java.io.File;
023: import java.io.FileWriter;
024: import java.io.IOException;
025: import java.io.PrintWriter;
026:
027: /**
028: * @author Jason van Zyl
029: * @todo document the need to call close() once successfully constructed, otherwise file handles can be leaked. Might be good to add a finalizer too, just in case.
030: */
031: public final class MavenEmbedderFileLogger extends
032: AbstractMavenEmbedderLogger {
033: private PrintWriter log;
034:
035: public MavenEmbedderFileLogger(File logFile) {
036: try {
037: this .log = new PrintWriter(new FileWriter(logFile));
038: } catch (IOException e) {
039: // The client must make sure the file is valid.
040: // TODO: [BP] would just throwing the IOE be better? We can't just ignore it, since that would give misleading NPE's later
041: throw new RuntimeException(
042: "The embedder was unable to write to the specified log file: "
043: + logFile, e);
044: }
045: }
046:
047: public void debug(String message, Throwable throwable) {
048: if (isDebugEnabled()) {
049: print("[DEBUG] ");
050: println(message);
051:
052: if (null != throwable) {
053: throwable.printStackTrace(System.out);
054: }
055: }
056: }
057:
058: public void info(String message, Throwable throwable) {
059: if (isInfoEnabled()) {
060: print("[INFO] ");
061: println(message);
062:
063: if (null != throwable) {
064: throwable.printStackTrace(System.out);
065: }
066: }
067: }
068:
069: public void warn(String message, Throwable throwable) {
070: if (isWarnEnabled()) {
071: print("[WARNING] ");
072: println(message);
073:
074: if (null != throwable) {
075: throwable.printStackTrace(System.out);
076: }
077: }
078: }
079:
080: public void error(String message, Throwable throwable) {
081: if (isErrorEnabled()) {
082: print("[ERROR] ");
083: println(message);
084:
085: if (null != throwable) {
086: throwable.printStackTrace(System.out);
087: }
088: }
089: }
090:
091: public void fatalError(String message, Throwable throwable) {
092: if (isFatalErrorEnabled()) {
093: print("[ERROR] ");
094: println(message);
095:
096: if (null != throwable) {
097: throwable.printStackTrace(System.out);
098: }
099: }
100: }
101:
102: protected void print(String message) {
103: log.print(message);
104: }
105:
106: protected void println(String message) {
107: log.println(message);
108: }
109:
110: public void close() {
111: log.flush();
112:
113: log.close();
114: }
115: }
|