package org.lwjgl;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.function.Supplier;

/**
 * A (mostly) stupidly simple logger.
 */
@FunctionalInterface
public interface StupidSimpleLogger {
	/**
	 * Logs a message provided lazily.
	 */
	void log(Supplier<String> message);

	/**
	 * Logs a message.
	 */
	default void log(String message) {
		log(() -> message);
	}

	/**
	 * Logs a message provided lazily and an error.
	 */
	default void log(Supplier<String> message, Throwable throwable) {
		log(() -> {
			StringWriter buf = new StringWriter().append(message.get()).append('\n');
			throwable.printStackTrace(new PrintWriter(buf));
			return buf.toString();
		});
	}

	/**
	 * Logs a message and an error.
	 */
	default void log(String message, Throwable throwable) {
		log(() -> message, throwable);
	}

	/**
	 * A No-Op implementation of the logger. All default forwarding methods are overidden with No-Op implementations.
	 */
	public static final StupidSimpleLogger NOOP = new StupidSimpleLogger() {
		@Override
		public void log(Supplier<String> message) {}

		@Override
		public void log(String message) {}

		@Override
		public void log(Supplier<String> message, Throwable throwable) {}

		@Override
		public void log(String message, Throwable throwable) {}
	};
}
