CreateLoggerFilter.java |
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.exe; 17 18import java.io.File; 19 20import org.tp23.antinstaller.InstallerContext; 21import org.tp23.antinstaller.runtime.SimpleLogger; 22 23 24/** 25 * Creates a suitable logger for the install. The logging does not 26 * throw exceptions since it is mainly for debug and we dont want to 27 * stop an install just because logging is not working 28 * @author Paul Hinds 29 * @version $Id: CreateLoggerFilter.java,v 1.3 2007/01/09 22:41:40 teknopaul Exp $ 30 */ 31public class CreateLoggerFilter implements ExecuteFilter { 32 33 public static final String LOG_FILE_NAME = "ant.install.log"; 34 35 /** 36 */ 37 public void exec(InstallerContext ctx){ 38 SimpleLogger logger = new SimpleLogger(); 39 ctx.setLogger( logger ); 40 try { 41 String defaultName = "./ant.install.log"; 42 // @since 0.7.1 RFE-1154368 for installs from CD where ./ is not writable 43 File defaultFile = new File(defaultName); 44 try { 45 if( !defaultFile.exists() ){ 46 defaultFile.createNewFile(); 47 } 48 } 49 catch(Exception e) { 50 ;// ignore canWrite() will return false 51 } 52 if(defaultFile.canWrite()) { 53 logger.setFileName(defaultName); 54 } 55 else { 56 String tempDir = ctx.getFileRoot().getAbsolutePath(); 57 logger.setFileName(tempDir+System.getProperty("file.separator") + LOG_FILE_NAME); 58 } 59 ctx.log("Ant basedir:" + ctx.getFileRoot().getCanonicalPath()); 60 } 61 catch (Throwable ex1) { 62 ex1.printStackTrace(); 63 logger.close(); 64 // swallow exceptions 65 } 66 } 67 68} 69