package tech.nevets.tvpn;


import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class Logger {
    private static LogOutputStream logOut;

    public static void loadLogOutStream(LogOutputStream lo) {
        logOut = lo;
    }

    private String className;

    public Logger(Class<?> clazz) {
        className = clazz.getSimpleName();
    }

    public void log(String message) {
        write(message, "INFO");
    }

    public void warn(String message) {
        write(message, "WARN");
    }

    public void error(String message) {
        write(message, "ERROR");
    }

    private void write(String message, String type) {
        try {
            logOut.write(("[" + className + "] (" + type + ") " + message + '\n').getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            System.err.println("Error logging message: " + e.getMessage());
        }

        // DEBUG
        System.out.print("[" + className + "] (" + type + ") " + message + '\n');
    }

}
