1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26 package it.tidalwave.role.ui.javafx.impl.util;
27
28 import javax.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.role.ui.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
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
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 }