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