View Javadoc
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  
28  import jakarta.annotation.Nonnull;
29  import java.util.Collection;
30  import it.tidalwave.util.As;
31  import it.tidalwave.role.Aggregate;
32  import it.tidalwave.role.Composite;
33  import it.tidalwave.ui.core.role.Displayable;
34  import lombok.experimental.UtilityClass;
35  import lombok.extern.slf4j.Slf4j;
36  import static java.util.stream.Collectors.*;
37  import static it.tidalwave.util.ShortNames.shortName;
38  
39  /***************************************************************************************************************************************************************
40   *
41   * @author  Fabrizio Giudici
42   *
43   **************************************************************************************************************************************************************/
44  @Slf4j @UtilityClass
45  public class Logging
46    {
47      public static final String INDENT = " ".repeat(100);
48  
49      public static final String P_LOG_CHILDREN = Logging.class.getName() + ".logCompositeChildren";
50  
51      public static final boolean logChildren = Boolean.getBoolean(P_LOG_CHILDREN);
52  
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  
63          if (objects.isEmpty())
64            {
65              log.debug(">>>>{} <empty>", prefix);
66            }
67          else
68            {
69              for (final Object object : objects)
70                {
71                  logObject(prefix, object);
72                }
73            }
74        }
75  
76      /***********************************************************************************************************************************************************
77       *
78       **********************************************************************************************************************************************************/
79      public static void logObject (@Nonnull final String indent, @Nonnull final Object object)
80        {
81          if (!log.isDebugEnabled())
82            {
83              return;
84            }
85  
86          if (object instanceof Displayable)
87            {
88              var displayableName = "";
89  
90              try
91                {
92                  displayableName = ((Displayable)object).getDisplayName();
93                }
94              catch (RuntimeException e)
95                {
96                  log.error("", e);
97                  displayableName = "<ERROR>";
98                }
99  
100             log.debug(">>>>     {}{}: {}", indent, shortName(object.getClass()), displayableName);
101           }
102         else
103           {
104             log.debug(">>>>     {}{}", indent, object);
105           }
106 
107         if (object instanceof Aggregate)
108           {
109             final var aggregate = (Aggregate<?>)object;
110             aggregate.getNames().forEach(name ->
111                        logObject(indent + "    " + name + ": ", aggregate.getByName(name).get()));
112           }
113 
114         if (object instanceof Composite)
115           {
116             final var finder = ((Composite<?, ?>)object).findChildren();
117 
118             // this is optional because it would jeopardize incremental loading of tress and probably cause troubles
119             if (!logChildren)
120               {
121                 log.debug(">>>>     {}    to see children, set system property to true: " + P_LOG_CHILDREN, indent);
122               }
123             else
124               {
125                 logObjects(indent + "    ", finder.results().stream().filter(o -> o != object).collect(toList()));
126               }
127           }
128 
129         if (object instanceof As)
130           {
131             logObjects(indent + "    Role: ",
132                        ((As)object).asMany(Object.class).stream().filter(o -> o != object).collect(toList()));
133           }
134       }
135   }