001/*
002 * Copyright 2017-2023 original authors
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016package io.micronaut.maven;
017
018import org.apache.maven.plugin.logging.Log;
019
020import static org.fusesource.jansi.Ansi.ansi;
021
022/**
023 * A {@link Log} implementation that uses Jansi to colorize the output.
024 *
025 * @author Álvaro Sánchez-Mariscal
026 * @since 4.0.0
027 */
028public class JansiLog implements Log {
029
030    private final Log delegate;
031
032    public JansiLog(Log delegate) {
033        this.delegate = delegate;
034    }
035
036    @Override
037    public boolean isDebugEnabled() {
038        return delegate.isDebugEnabled();
039    }
040
041    @Override
042    public void debug(CharSequence content) {
043        delegate.debug(fmt(content));
044    }
045
046    @Override
047    public void debug(CharSequence content, Throwable error) {
048        delegate.debug(fmt(content), error);
049    }
050
051    @Override
052    public void debug(Throwable error) {
053        delegate.debug(error);
054    }
055
056    @Override
057    public boolean isInfoEnabled() {
058        return delegate.isInfoEnabled();
059    }
060
061    @Override
062    public void info(CharSequence content) {
063        delegate.info(fmt(content));
064    }
065
066    @Override
067    public void info(CharSequence content, Throwable error) {
068        delegate.info(fmt(content), error);
069    }
070
071    @Override
072    public void info(Throwable error) {
073        delegate.info(error);
074    }
075
076    @Override
077    public boolean isWarnEnabled() {
078        return delegate.isWarnEnabled();
079    }
080
081    @Override
082    public void warn(CharSequence content) {
083        delegate.warn(fmt(content));
084    }
085
086    @Override
087    public void warn(CharSequence content, Throwable error) {
088        delegate.warn(fmt(content), error);
089    }
090
091    @Override
092    public void warn(Throwable error) {
093        delegate.warn(error);
094    }
095
096    @Override
097    public boolean isErrorEnabled() {
098        return delegate.isErrorEnabled();
099    }
100
101    @Override
102    public void error(CharSequence content) {
103        delegate.error(fmt(content));
104    }
105
106    @Override
107    public void error(CharSequence content, Throwable error) {
108        delegate.error(fmt(content), error);
109    }
110
111    @Override
112    public void error(Throwable error) {
113        delegate.error(error);
114    }
115
116    private String fmt(CharSequence s) {
117        return ansi().fgYellow().a(s).reset().toString();
118    }
119
120}