Logging.java

  1. /*
  2.  * *************************************************************************************************************************************************************
  3.  *
  4.  * SteelBlue: DCI User Interfaces
  5.  * http://tidalwave.it/projects/steelblue
  6.  *
  7.  * Copyright (C) 2015 - 2025 by Tidalwave s.a.s. (http://tidalwave.it)
  8.  *
  9.  * *************************************************************************************************************************************************************
  10.  *
  11.  * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
  12.  * You may obtain a copy of the License at
  13.  *
  14.  *     http://www.apache.org/licenses/LICENSE-2.0
  15.  *
  16.  * 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
  17.  * CONDITIONS OF ANY KIND, either express or implied.  See the License for the specific language governing permissions and limitations under the License.
  18.  *
  19.  * *************************************************************************************************************************************************************
  20.  *
  21.  * git clone https://bitbucket.org/tidalwave/steelblue-src
  22.  * git clone https://github.com/tidalwave-it/steelblue-src
  23.  *
  24.  * *************************************************************************************************************************************************************
  25.  */
  26. package it.tidalwave.ui.javafx.impl.util;

  27. import jakarta.annotation.Nonnull;
  28. import java.util.Collection;
  29. import it.tidalwave.ui.core.role.Displayable;
  30. import it.tidalwave.util.As;
  31. import it.tidalwave.role.Aggregate;
  32. import it.tidalwave.role.Composite;
  33. import lombok.experimental.UtilityClass;
  34. import lombok.extern.slf4j.Slf4j;
  35. import static java.util.stream.Collectors.*;
  36. import static it.tidalwave.util.ShortNames.shortName;

  37. /***************************************************************************************************************************************************************
  38.  *
  39.  * @author  Fabrizio Giudici
  40.  *
  41.  **************************************************************************************************************************************************************/
  42. @Slf4j @UtilityClass
  43. public class Logging
  44.   {
  45.     public static final String INDENT = " ".repeat(100);

  46.     private static final String P_LOG_CHILDREN = Logging.class.getName() + ".logCompositeChildren";

  47.     private static final boolean LOG_CHILDREN = Boolean.getBoolean(P_LOG_CHILDREN);

  48.     /***********************************************************************************************************************************************************
  49.      *
  50.      **********************************************************************************************************************************************************/
  51.     public static void logObjects (@Nonnull final String prefix, @Nonnull final Collection<?> objects)
  52.       {
  53.         if (!log.isDebugEnabled())
  54.           {
  55.             return;
  56.           }

  57.         if (objects.isEmpty())
  58.           {
  59.             log.debug(">>>>{} <empty>", prefix);
  60.           }
  61.         else
  62.           {
  63.             for (final Object object : objects)
  64.               {
  65.                 logObject(prefix, object);
  66.               }
  67.           }
  68.       }

  69.     /***********************************************************************************************************************************************************
  70.      *
  71.      **********************************************************************************************************************************************************/
  72.     public static void logObject (@Nonnull final String indent, @Nonnull final Object object)
  73.       {
  74.         if (!log.isDebugEnabled())
  75.           {
  76.             return;
  77.           }

  78.         if (object instanceof Displayable)
  79.           {
  80.             var displayableName = "";

  81.             try
  82.               {
  83.                 displayableName = ((Displayable)object).getDisplayName();
  84.               }
  85.             catch (RuntimeException e)
  86.               {
  87.                 log.error("", e);
  88.                 displayableName = "<ERROR>";
  89.               }

  90.             log.debug(">>>>     {}{}: {}", indent, shortName(object.getClass()), displayableName);
  91.           }
  92.         else
  93.           {
  94.             log.debug(">>>>     {}{}", indent, object);
  95.           }

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

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

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

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