KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ant > internal > ui > antsupport > logger > NullBuildLogger


1 /*******************************************************************************
2  * Copyright (c) 2000, 2005 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ant.internal.ui.antsupport.logger;
12
13
14 import java.io.PrintStream JavaDoc;
15
16 import org.apache.tools.ant.BuildEvent;
17 import org.apache.tools.ant.BuildException;
18 import org.apache.tools.ant.BuildLogger;
19 import org.apache.tools.ant.Project;
20 import org.apache.tools.ant.util.StringUtils;
21 import org.eclipse.ant.core.AntSecurityException;
22 import org.eclipse.ant.internal.core.AbstractEclipseBuildLogger;
23 import org.eclipse.ant.internal.ui.antsupport.AntSupportMessages;
24 import org.eclipse.core.runtime.OperationCanceledException;
25
26 public class NullBuildLogger extends AbstractEclipseBuildLogger implements BuildLogger{
27
28     protected int fMessageOutputLevel = Project.MSG_INFO;
29     private PrintStream JavaDoc fErr= null;
30     private PrintStream JavaDoc fOut= null;
31     protected boolean fEmacsMode= false;
32     
33     /**
34      * An exception that has already been logged.
35      */

36     protected Throwable JavaDoc fHandledException= null;
37     
38     /**
39      * @see org.apache.tools.ant.BuildLogger#setMessageOutputLevel(int)
40      */

41     public void setMessageOutputLevel(int level) {
42         fMessageOutputLevel= level;
43     }
44     
45     protected int getMessageOutputLevel() {
46         return fMessageOutputLevel;
47     }
48
49     /**
50      * @see org.apache.tools.ant.BuildLogger#setEmacsMode(boolean)
51      */

52     public void setEmacsMode(boolean emacsMode) {
53         fEmacsMode= emacsMode;
54     }
55
56     /**
57      * @see org.apache.tools.ant.BuildListener#buildStarted(org.apache.tools.ant.BuildEvent)
58      */

59     public void buildStarted(BuildEvent event) {
60     }
61
62     /**
63      * @see org.apache.tools.ant.BuildListener#buildFinished(org.apache.tools.ant.BuildEvent)
64      */

65     public void buildFinished(BuildEvent event) {
66         String JavaDoc message= handleException(event);
67         if (message != null) {
68             logMessage(message, getMessageOutputLevel());
69         }
70         fHandledException= null;
71     }
72
73     /**
74      * @see org.apache.tools.ant.BuildListener#targetStarted(org.apache.tools.ant.BuildEvent)
75      */

76     public void targetStarted(BuildEvent event) {
77     }
78
79     /**
80      * @see org.apache.tools.ant.BuildListener#targetFinished(org.apache.tools.ant.BuildEvent)
81      */

82     public void targetFinished(BuildEvent event) {
83     }
84
85     /**
86      * @see org.apache.tools.ant.BuildListener#taskStarted(org.apache.tools.ant.BuildEvent)
87      */

88     public void taskStarted(BuildEvent event) {
89     }
90
91     /**
92      * @see org.apache.tools.ant.BuildListener#taskFinished(org.apache.tools.ant.BuildEvent)
93      */

94     public void taskFinished(BuildEvent event) {
95     }
96
97     /* (non-Javadoc)
98      * @see org.apache.tools.ant.BuildListener#messageLogged(org.apache.tools.ant.BuildEvent)
99      */

100     public void messageLogged(BuildEvent event) {
101         logMessage(event.getMessage(), event.getPriority());
102     }
103
104     protected PrintStream JavaDoc getErrorPrintStream() {
105         return fErr;
106     }
107     
108     protected PrintStream JavaDoc getOutputPrintStream() {
109         return fOut;
110     }
111     
112     /**
113      * @see org.apache.tools.ant.BuildLogger#setErrorPrintStream(java.io.PrintStream)
114      */

115     public void setErrorPrintStream(PrintStream JavaDoc err) {
116         //this build logger logs to "null" unless
117
//the user has explicitly set a logfile to use
118
if (err == System.err) {
119             fErr= null;
120         } else {
121             fErr= err;
122         }
123     }
124
125     /**
126      * @see org.apache.tools.ant.BuildLogger#setOutputPrintStream(java.io.PrintStream)
127      */

128     public void setOutputPrintStream(PrintStream JavaDoc output) {
129         //this build logger logs to "null" unless
130
//the user has explicitly set a logfile to use
131
if (output == System.out) {
132             fOut= null;
133         } else {
134             fOut= output;
135         }
136     }
137     
138     protected void logMessage(String JavaDoc message, int priority) {
139         if (priority > getMessageOutputLevel()) {
140             return;
141         }
142         
143         if (priority == Project.MSG_ERR) {
144             if (getErrorPrintStream() != null && getErrorPrintStream() != System.err) {
145                 //user has designated to log to a logfile
146
getErrorPrintStream().println(message);
147             }
148         } else {
149             if (getOutputPrintStream() != null && getOutputPrintStream() != System.out) {
150                 //user has designated to log to a logfile
151
getOutputPrintStream().println(message);
152             }
153         }
154     }
155     
156     protected String JavaDoc handleException(BuildEvent event) {
157         Throwable JavaDoc exception = event.getException();
158         if (exception == null || exception == fHandledException
159         || exception instanceof OperationCanceledException
160         || exception instanceof AntSecurityException) {
161             return null;
162         }
163         fHandledException= exception;
164         StringBuffer JavaDoc message= new StringBuffer JavaDoc();
165         message.append(StringUtils.LINE_SEP);
166         message.append(AntSupportMessages.NullBuildLogger_1);
167         message.append(StringUtils.LINE_SEP);
168         if (Project.MSG_VERBOSE <= fMessageOutputLevel || !(exception instanceof BuildException)) {
169             message.append(StringUtils.getStackTrace(exception));
170         } else {
171             if (exception instanceof BuildException) {
172                 message.append(exception.toString()).append(StringUtils.LINE_SEP);
173             } else {
174                 message.append(exception.getMessage()).append(StringUtils.LINE_SEP);
175             }
176         }
177         
178         return message.toString();
179     }
180 }
181
Popular Tags