001/*
002 * SPDX-License-Identifier: Apache-2.0
003 *
004 * Copyright 2020-2024 Andres Almiray.
005 *
006 * Licensed under the Apache License, Version 2.0 (the "License");
007 * you may not use this file except in compliance with the License.
008 * You may obtain a copy of the License at
009 *
010 *     https://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018package org.kordamp.maven.checker.cli.internal;
019
020import org.kordamp.maven.checker.Logger;
021import org.slf4j.helpers.MessageFormatter;
022import picocli.CommandLine;
023
024import java.io.PrintWriter;
025
026/**
027 * @author Andres Almiray
028 * @since 1.1.0
029 */
030public class SimpleLoggerAdapter implements Logger {
031    private final PrintWriter out;
032    private final Level level;
033
034    public SimpleLoggerAdapter(PrintWriter out) {
035        this(out, Level.WARN);
036    }
037
038    public SimpleLoggerAdapter(PrintWriter out, Level level) {
039        this.out = out;
040        this.level = level;
041    }
042
043    @Override
044    public void debug(String message) {
045        if (isLevelEnabled(Level.DEBUG)) {
046            out.println(Level.DEBUG + message);
047        }
048    }
049
050    @Override
051    public void info(String message) {
052        if (isLevelEnabled(Level.INFO)) {
053            out.println(Level.INFO + message);
054        }
055    }
056
057    @Override
058    public void warn(String message) {
059        if (isLevelEnabled(Level.WARN)) {
060            out.println(Level.WARN + message);
061        }
062    }
063
064    @Override
065    public void error(String message) {
066        if (isLevelEnabled(Level.ERROR)) {
067            out.println(Level.ERROR + message);
068        }
069    }
070
071    @Override
072    public void debug(String message, Object... args) {
073        if (isLevelEnabled(Level.DEBUG)) {
074            out.println(Level.DEBUG + MessageFormatter.arrayFormat(message, args).getMessage());
075        }
076    }
077
078    @Override
079    public void info(String message, Object... args) {
080        if (isLevelEnabled(Level.INFO)) {
081            out.println(Level.INFO + MessageFormatter.arrayFormat(message, args).getMessage());
082        }
083    }
084
085    @Override
086    public void warn(String message, Object... args) {
087        if (isLevelEnabled(Level.WARN)) {
088            out.println(Level.WARN + MessageFormatter.arrayFormat(message, args).getMessage());
089        }
090    }
091
092    @Override
093    public void error(String message, Object... args) {
094        if (isLevelEnabled(Level.ERROR)) {
095            out.println(Level.ERROR + MessageFormatter.arrayFormat(message, args).getMessage());
096        }
097    }
098
099    @Override
100    public void debug(String message, Throwable throwable) {
101        if (isLevelEnabled(Level.DEBUG)) {
102            out.println(Level.DEBUG + message);
103            printThrowable(throwable);
104        }
105    }
106
107    @Override
108    public void info(String message, Throwable throwable) {
109        if (isLevelEnabled(Level.INFO)) {
110            out.println(Level.INFO + message);
111            printThrowable(throwable);
112        }
113    }
114
115    @Override
116    public void warn(String message, Throwable throwable) {
117        if (isLevelEnabled(Level.WARN)) {
118            out.println(Level.WARN + message);
119            printThrowable(throwable);
120        }
121    }
122
123    @Override
124    public void error(String message, Throwable throwable) {
125        if (isLevelEnabled(Level.ERROR)) {
126            out.println(Level.ERROR + message);
127            printThrowable(throwable);
128        }
129    }
130
131    private void printThrowable(Throwable throwable) {
132        if (null != throwable) {
133            throwable.printStackTrace(new Colorizer(out));
134        }
135    }
136
137    private boolean isLevelEnabled(Level requested) {
138        return requested.ordinal() >= level.ordinal();
139    }
140
141    public enum Level {
142        DEBUG("cyan"),
143        INFO("blue"),
144        WARN("yellow"),
145        ERROR("red");
146
147        private final String color;
148
149        Level(String color) {
150            this.color = color;
151        }
152
153        @Override
154        public String toString() {
155            return "[" + colorize(name()) + "] ";
156        }
157
158        private String colorize(String input) {
159            return CommandLine.Help.Ansi.AUTO.string("@|" + color + " " + input + "|@");
160        }
161    }
162}