package org.apache.logging.log4j.status;

import java.io.IOException;
import java.io.PrintStream;
import org.apache.logging.log4j.Level;

public class StatusConsoleListener implements StatusListener {
   private Level level;
   private String[] filters;
   private final PrintStream stream;

   public StatusConsoleListener(Level level) {
      this(level, System.out);
   }

   public StatusConsoleListener(Level level, PrintStream stream) {
      this.level = Level.FATAL;
      this.filters = null;
      if (stream == null) {
         throw new IllegalArgumentException("You must provide a stream to use for this listener.");
      } else {
         this.level = level;
         this.stream = stream;
      }
   }

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

   public Level getStatusLevel() {
      return this.level;
   }

   public void log(StatusData data) {
      if (!this.filtered(data)) {
         this.stream.println(data.getFormattedStatus());
      }

   }

   public void setFilters(String... filters) {
      this.filters = filters;
   }

   private boolean filtered(StatusData data) {
      if (this.filters == null) {
         return false;
      } else {
         String caller = data.getStackTraceElement().getClassName();
         String[] arr$ = this.filters;
         int len$ = arr$.length;

         for(int i$ = 0; i$ < len$; ++i$) {
            String filter = arr$[i$];
            if (caller.startsWith(filter)) {
               return true;
            }
         }

         return false;
      }
   }

   public void close() throws IOException {
      if (this.stream != System.out && this.stream != System.err) {
         this.stream.close();
      }

   }
}
