001package ball.util.ant.taskdefs;
002/*-
003 * ##########################################################################
004 * Utilities
005 * $Id: AntTaskLogMethods.java 5285 2020-02-05 04:23:21Z ball $
006 * $HeadURL: svn+ssh://svn.hcf.dev/var/spool/scm/repository.svn/ball-util/trunk/src/main/java/ball/util/ant/taskdefs/AntTaskLogMethods.java $
007 * %%
008 * Copyright (C) 2008 - 2020 Allen D. Ball
009 * %%
010 * Licensed under the Apache License, Version 2.0 (the "License");
011 * you may not use this file except in compliance with the License.
012 * You may obtain a copy of the License at
013 *
014 *      http://www.apache.org/licenses/LICENSE-2.0
015 *
016 * Unless required by applicable law or agreed to in writing, software
017 * distributed under the License is distributed on an "AS IS" BASIS,
018 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
019 * See the License for the specific language governing permissions and
020 * limitations under the License.
021 * ##########################################################################
022 */
023import ball.text.TextTable;
024import java.io.BufferedReader;
025import java.io.File;
026import java.util.stream.Stream;
027import javax.swing.table.TableModel;
028import org.apache.tools.ant.Project;
029
030import static org.apache.commons.lang3.StringUtils.EMPTY;
031
032/**
033 * Interface to provide common default logging methods for
034 * {@link org.apache.tools.ant.Task}s.
035 *
036 * @author {@link.uri mailto:ball@hcf.dev Allen D. Ball}
037 * @version $Revision: 5285 $
038 */
039public interface AntTaskLogMethods extends AntTaskMixIn {
040    void log(String message);
041    void log(String message, int messageLevel);
042    void log(String message, Throwable throwable, int messageLevel);
043    void log(Throwable throwable, int messageLevel);
044
045    /**
046     * See {@link #log(Stream)}.
047     *
048     * @param   model           The {@link TableModel} to log.
049     */
050    default void log(TableModel model) { log(model, Project.MSG_INFO); }
051
052    /**
053     * See {@link #log(Stream,int)}.
054     *
055     * @param   model           The {@link TableModel} to log.
056     * @param   msgLevel        The log message level.
057     */
058    default void log(TableModel model, int msgLevel) {
059        try (BufferedReader reader =
060                 new TextTable(model).getBufferedReader()) {
061            log(reader.lines(), Project.MSG_INFO);
062        } catch (Throwable throwable) {
063            throw new IllegalStateException(throwable);
064        }
065    }
066
067    /**
068     * See {@link org.apache.tools.ant.Task#log(String)}.
069     *
070     * @param   stream          The {@link Stream} of {@link String}s to
071     *                          log.
072     */
073    default void log(Stream<String> stream) {
074        log(stream, Project.MSG_INFO);
075    }
076
077    /**
078     * See {@link org.apache.tools.ant.Task#log(String,int)}.
079     *
080     * @param   stream          The {@link Stream} of {@link String}s to
081     *                          log.
082     * @param   msgLevel        The log message level.
083     */
084    default void log(Stream<String> stream, int msgLevel) {
085        stream.forEach(t -> log(t, msgLevel));
086    }
087
088    /**
089     * See {@link org.apache.tools.ant.Task#log(String)}.
090     *
091     * @param   file            The {@link File}.
092     * @param   lineno          The line number in the {@link File}.
093     * @param   message         The message ({@link String}).
094     */
095    default void log(File file, int lineno, String message) {
096        log(String.valueOf(file) + ":" + String.valueOf(lineno)
097            + ": " + message);
098    }
099
100    /**
101     * Log an empty {@link String}.
102     */
103    default void log() { log(Project.MSG_INFO); }
104
105    /**
106     * Log an empty {@link String}.
107     *
108     * @param   msgLevel        The log message level.
109     */
110    default void log(int msgLevel) { log(EMPTY, msgLevel); }
111}