001    /**
002     * Copyright (c) 2000-2011 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
013     */
014    
015    package com.liferay.util.ant;
016    
017    import com.liferay.portal.kernel.io.unsync.UnsyncBufferedReader;
018    import com.liferay.portal.kernel.io.unsync.UnsyncStringReader;
019    import com.liferay.portal.kernel.util.StringBundler;
020    
021    import java.io.IOException;
022    
023    import org.apache.tools.ant.BuildEvent;
024    import org.apache.tools.ant.DefaultLogger;
025    import org.apache.tools.ant.Project;
026    import org.apache.tools.ant.util.StringUtils;
027    
028    /**
029     * @author Brian Wing Shun Chan
030     */
031    public class SystemLogger extends DefaultLogger {
032    
033            @Override
034            public void messageLogged(BuildEvent event) {
035                    int priority = event.getPriority();
036    
037                    if (priority <= msgOutputLevel) {
038                            StringBundler sb = new StringBundler();
039    
040                            try {
041                                    UnsyncBufferedReader unsyncBufferedReader =
042                                            new UnsyncBufferedReader(
043                                                    new UnsyncStringReader(event.getMessage()));
044    
045                                    String line = unsyncBufferedReader.readLine();
046    
047                                    boolean first = true;
048    
049                                    while (line != null) {
050                                            if (!first) {
051                                                    sb.append(StringUtils.LINE_SEP);
052                                            }
053    
054                                            first = false;
055    
056                                            sb.append("  ");
057                                            sb.append(line);
058    
059                                            line = unsyncBufferedReader.readLine();
060                                    }
061                            }
062                            catch (IOException e) {
063                            }
064    
065                            String msg = sb.toString();
066    
067                            if (priority != Project.MSG_ERR) {
068                                    printMessage(msg, out, priority);
069                            }
070                            else {
071                                    printMessage(msg, err, priority);
072                            }
073    
074                            log(msg);
075                    }
076            }
077    
078    }