Logging.java

  1. /*
  2.  * #%L
  3.  * *********************************************************************************************************************
  4.  *
  5.  * SteelBlue
  6.  * http://steelblue.tidalwave.it - git clone git@bitbucket.org:tidalwave/steelblue-src.git
  7.  * %%
  8.  * Copyright (C) 2015 - 2021 Tidalwave s.a.s. (http://tidalwave.it)
  9.  * %%
  10.  *
  11.  * *********************************************************************************************************************
  12.  *
  13.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
  14.  * the License. You may obtain a copy of the License at
  15.  *
  16.  *     http://www.apache.org/licenses/LICENSE-2.0
  17.  *
  18.  * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
  19.  * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
  20.  * specific language governing permissions and limitations under the License.
  21.  *
  22.  * *********************************************************************************************************************
  23.  *
  24.  *
  25.  *
  26.  * *********************************************************************************************************************
  27.  * #L%
  28.  */
  29. package it.tidalwave.role.ui.javafx.impl.util;

  30. import javax.annotation.Nonnull;
  31. import java.util.Collection;
  32. import java.util.Optional;
  33. import it.tidalwave.util.As;
  34. import it.tidalwave.util.Finder;
  35. import it.tidalwave.role.Aggregate;
  36. import it.tidalwave.role.Composite;
  37. import it.tidalwave.role.ui.Displayable;
  38. import lombok.experimental.UtilityClass;
  39. import lombok.extern.slf4j.Slf4j;
  40. import static it.tidalwave.role.spi.impl.LogUtil.shortName;
  41. import static java.util.stream.Collectors.toList;

  42. /***********************************************************************************************************************
  43.  *
  44.  * @author  Fabrizio Giudici
  45.  *
  46.  **********************************************************************************************************************/
  47. @Slf4j @UtilityClass
  48. public class Logging
  49.   {
  50.     public static final String INDENT = " ".repeat(100);

  51.     public static final String P_LOG_CHILDREN = Logging.class.getName() + ".logCompositeChildren";

  52.     public static final boolean logChildren = Boolean.valueOf(P_LOG_CHILDREN);

  53.     /*******************************************************************************************************************
  54.      *
  55.      ******************************************************************************************************************/
  56.     public static void logObjects (@Nonnull final String prefix, @Nonnull final Collection<?> objects)
  57.       {
  58.         if (!log.isDebugEnabled())
  59.           {
  60.             return;
  61.           }

  62.         if (objects.isEmpty())
  63.           {
  64.             log.debug(">>>>{} <empty>", prefix);
  65.           }
  66.         else
  67.           {
  68.             for (final Object object : objects)
  69.               {
  70.                 logObject(prefix, object);
  71.               }
  72.           }
  73.       }

  74.     /*******************************************************************************************************************
  75.      *
  76.      ******************************************************************************************************************/
  77.     public static void logObject (@Nonnull final String indent, @Nonnull final Object object)
  78.       {
  79.         if (!log.isDebugEnabled())
  80.           {
  81.             return;
  82.           }

  83.         if (object instanceof Displayable)
  84.           {
  85.             log.debug(">>>>     {}{}: {}", indent, shortName(object.getClass()),
  86.                       ((Displayable)object).getDisplayName());
  87.           }
  88.         else
  89.           {
  90.             log.debug(">>>>     {}{}", indent, object);
  91.           }

  92.         if (object instanceof Aggregate)
  93.           {
  94.             final Aggregate<?> aggregate = (Aggregate<?>)object;
  95.             aggregate.getNames().forEach(name ->
  96.               {
  97.                 logObject(indent + "    " + name + ": ", aggregate.getByName(name).get());
  98.               });
  99.           }

  100.         if (object instanceof Composite)
  101.           {
  102.             final Finder<?> finder = ((Composite<?, ?>)object).findChildren();

  103.             // this is optional because it would jeopardize incremental loading of tress and probably cause troubles
  104.             if (!logChildren)
  105.               {
  106.                 log.debug(">>>>     {}    to see children, set system property to true: " + P_LOG_CHILDREN, indent);
  107.               }
  108.             else
  109.               {
  110.                 logObjects(indent + "    ", finder.results().stream().filter(o -> o != object).collect(toList()));
  111.               }
  112.           }

  113.         if (object instanceof As)
  114.           {
  115.             logObjects(indent + "    Role: ",
  116.                        ((As)object).asMany(Object.class).stream().filter(o -> o != object).collect(toList()));
  117.           }
  118.       }
  119.   }