/*
 * Bach - Java Shell Builder
 * Copyright (C) 2020 Christian Stein
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package de.sormuras.bach.util;

import java.text.MessageFormat;
import java.util.Collection;
import java.util.List;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;

/** An entry-collecting system logger implementation. */
public /*static*/ class Logbook implements System.Logger {

  public static Logbook ofSystem() {
    var debug = Boolean.getBoolean("ebug") || "".equals(System.getProperty("ebug"));
    var dryRun = Boolean.getBoolean("ry-run") || "".equals(System.getProperty("ry-run"));
    return new Logbook(System.out::println, debug, dryRun);
  }

  private final Consumer<String> consumer;
  private final boolean debug;
  private final boolean dryRun;
  private final Collection<Entry> entries;

  public Logbook(Consumer<String> consumer, boolean debug, boolean dryRun) {
    this.consumer = consumer;
    this.debug = debug;
    this.dryRun = dryRun;
    this.entries = new ConcurrentLinkedQueue<>();
  }

  public boolean isDebug() {
    return debug;
  }

  public boolean isDryRun() {
    return dryRun;
  }

  @Override
  public String getName() {
    return "Logbook";
  }

  @Override
  public boolean isLoggable(Level level) {
    if (level == Level.ALL) return isDebug();
    if (level == Level.OFF) return isDryRun();
    return true;
  }

  @Override
  public void log(Level level, ResourceBundle bundle, String message, Throwable thrown) {
    entries.add(new Entry(level, message, thrown));
  }

  @Override
  public void log(Level level, ResourceBundle bundle, String pattern, Object... arguments) {
    entries.add(new Entry(level, MessageFormat.format(pattern, arguments), null));
  }

  public List<String> messages() {
    return lines(entry -> entry.message);
  }

  public List<String> lines(Function<Entry, String> mapper) {
    return entries.stream().map(mapper).collect(Collectors.toList());
  }

  public final class Entry {
    private final Level level;
    private final String message;
    private final Throwable thrown;

    public Entry(Level level, String message, Throwable thrown) {
      this.level = level;
      this.message = message;
      this.thrown = thrown;
      if (debug) consumer.accept(message);
    }

    @Override
    public String toString() {
      if (thrown == null) return level + "|" + message;
      return level + "|" + message + " -> " + thrown;
    }
  }
}

@@ -934,16 +934,48 @@
 lectors;
+%0Aimport java.util.stream.Stream;
 %0A%0A/** An
@@ -2177,245 +2177,584 @@
     
-entries.add(new Entry(level, message, thrown));%0A  %7D%0A%0A  @Override%0A  public void log(Level level, ResourceBundle bundle, String pattern, Object... arguments) %7B%0A    entries.add(new Entry(level, MessageFormat.format(pattern, arguments), null
+if (message == null %7C%7C message.isBlank()) return;%0A    var entry = new Entry(level, message, thrown);%0A    if (debug) consumer.accept(entry.toString());%0A    entries.add(entry);%0A  %7D%0A%0A  @Override%0A  public void log(Level level, ResourceBundle bundle, String pattern, Object... arguments) %7B%0A    var message = arguments == null ? pattern : MessageFormat.format(pattern, arguments);%0A    log(level, bundle, message, (Throwable) null);%0A  %7D%0A%0A  public Stream%3CEntry%3E entries(Level threshold) %7B%0A    return entries.stream().filter(entry -%3E entry.level.getSeverity() %3E= threshold.getSeverity(
 ));%0A
@@ -2986,16 +2986,23 @@
  public 
+static 
 final cl
@@ -3106,21 +3106,24 @@
 rowable 
-throw
+exceptio
 n;%0A%0A    
@@ -3166,37 +3166,40 @@
 sage, Throwable 
-throw
+exceptio
 n) %7B%0A      this.
@@ -3258,228 +3258,424 @@
 his.
-thrown = thrown;%0A      if (debug) consumer.accept(message);%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A      if (thrown == null) return level + %22%7C%22 + message;%0A      return level + %22%7C%22 + message + %22 -%3E %22 + thrown
+exception = exception;%0A    %7D%0A%0A    public Level level() %7B%0A      return level;%0A    %7D%0A%0A    public String message() %7B%0A      return message;%0A    %7D%0A%0A    public Throwable exception() %7B%0A      return exception;%0A    %7D%0A%0A    @Override%0A    public String toString() %7B%0A      var exceptionMessage = exception == null ? %22%22 : %22 -%3E %22 + exception;%0A      return String.format(%22%25c%7C%25s%25s%22, level.name().charAt(0), message, exceptionMessage)
 ;%0A  

Fix log implementation