package org.zeropage.log;

import java.io.IOException;
import java.io.OutputStream;

public class OutputStreamLogListener implements LogListener {
    private OutputStream outputStream;
    private Level level;

    public OutputStreamLogListener(OutputStream outputStream) {
        this.outputStream = outputStream;
    }

    public void setLevel(Level level) {
        this.level = level;
    }

    @Override
    public void debug(String message) {
        if(Level.DEBUG.getValue() <= level.getValue()) {
            print("DEBUG: " + message);
        }

    }

    @Override
    public void info(String message) {
        if(Level.INFO.getValue() <= level.getValue()) {
            print("INFO: " + message);
        }
    }

    @Override
    public void warn(String message) {
        if(Level.WARN.getValue() <= level.getValue()) {
            print("WARNING: " + message);
        }
    }

    @Override
    public void error(String message) {
        if(Level.ERROR.getValue() <= level.getValue()) {
            print("ERROR: " + message);
        }
    }

    @Override
    public void fatal(String message) {
        if(Level.FATAL.getValue() <= level.getValue()) {
            print("FATAL: " + message);
        }
    }

    private void print(String message) {
        message = message + System.lineSeparator();
        try {
            outputStream.write(message.getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

@@ -307,24 +307,211 @@
 tputStream;%0A
+        this.level = Level.INFO;%0A    %7D%0A%0A    public OutputStreamLogListener(OutputStream outputStream, Level level) %7B%0A        this.outputStream = outputStream;%0A        this.level = level;%0A
     %7D%0A%0A    p

add new constructor