• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

devonfw / IDEasy / 15911906253

26 Jun 2025 08:33PM UTC coverage: 68.328% (+0.5%) from 67.796%
15911906253

push

github

web-flow
#1303: Add option to show GDPR compliant console output and suggest for status in bug template (#1396)

Co-authored-by: jan-vcapgemini <jan-vincent.hoelzle@capgemini.com>
Co-authored-by: jan-vcapgemini <59438728+jan-vcapgemini@users.noreply.github.com>

3259 of 5176 branches covered (62.96%)

Branch coverage included in aggregate %.

8298 of 11738 relevant lines covered (70.69%)

3.13 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

68.75
cli/src/main/java/com/devonfw/tools/ide/log/AbstractIdeSubLogger.java
1
package com.devonfw.tools.ide.log;
2

3
import com.devonfw.tools.ide.cli.CliException;
4

5
/**
6
 * Abstract base implementation of {@link IdeSubLogger}.
7
 */
8
public abstract class AbstractIdeSubLogger implements IdeSubLogger {
1✔
9

10
  /** @see #getLevel() */
11
  protected final IdeLogLevel level;
12

13
  protected final IdeLogExceptionDetails exceptionDetails;
14

15
  final IdeLogListener listener;
16

17
  protected final boolean colored;
18

19
  private boolean enabled;
20

21
  private IdeLogArgFormatter argFormatter;
22

23
  /**
24
   * The constructor.
25
   *
26
   * @param level the {@link #getLevel() log-level}.
27
   * @param colored - see {@link #isColored()}.
28
   * @param exceptionDetails the {@link IdeLogExceptionDetails} configuring how to handle exceptions.
29
   * @param listener the {@link IdeLogListener} to send log-events to.
30
   */
31
  public AbstractIdeSubLogger(IdeLogLevel level, boolean colored, IdeLogExceptionDetails exceptionDetails, IdeLogListener listener) {
32

33
    super();
2✔
34
    this.level = level;
3✔
35
    this.exceptionDetails = exceptionDetails;
3✔
36
    this.argFormatter = IdeLogArgFormatter.DEFAULT;
3✔
37
    if (listener == null) {
2✔
38
      this.listener = IdeLogListenerNone.INSTANCE;
4✔
39
    } else {
40
      this.listener = listener;
3✔
41
    }
42
    this.colored = colored;
3✔
43
    this.enabled = true;
3✔
44
  }
1✔
45

46
  @Override
47
  public IdeLogLevel getLevel() {
48

49
    return this.level;
×
50
  }
51

52
  @Override
53
  public boolean isEnabled() {
54

55
    return this.enabled;
3✔
56
  }
57

58
  void setEnabled(boolean enabled) {
59

60
    this.enabled = enabled;
3✔
61
  }
1✔
62

63
  /**
64
   * Should only be used internally by logger implementation.
65
   *
66
   * @param message the message template.
67
   * @param args the dynamic arguments to fill in.
68
   * @return the resolved message with the parameters filled in.
69
   */
70
  protected String compose(String message, Object... args) {
71

72
    int pos = message.indexOf("{}");
4✔
73
    if (pos < 0) {
2!
74
      if (args.length > 0) {
×
75
        invalidMessage(message, false, args);
×
76
      }
77
      return message;
×
78
    }
79
    int argIndex = 0;
2✔
80
    int start = 0;
2✔
81
    int length = message.length();
3✔
82
    StringBuilder sb = new StringBuilder(length + 48);
7✔
83
    while (pos >= 0) {
2✔
84
      sb.append(message, start, pos);
6✔
85
      sb.append(this.argFormatter.formatArgument(args[argIndex++]));
10✔
86
      start = pos + 2;
4✔
87
      pos = message.indexOf("{}", start);
5✔
88
      if ((argIndex >= args.length) && (pos > 0)) {
6!
89
        invalidMessage(message, true, args);
×
90
        pos = -1;
×
91
      }
92
    }
93
    if (start < length) {
3✔
94
      String rest = message.substring(start);
4✔
95
      sb.append(rest);
4✔
96
    }
97
    if (argIndex < args.length) {
4!
98
      invalidMessage(message, false, args);
×
99
    }
100
    return sb.toString();
3✔
101
  }
102

103
  private void invalidMessage(String message, boolean more, Object[] args) {
104

105
    warning("Invalid log message with " + args.length + " argument(s) but " + (more ? "more" : "less")
×
106
        + " placeholders: " + message);
107
  }
×
108

109
  private void warning(String message) {
110

111
    boolean colored = isColored();
×
112
    if (colored) {
×
113
      System.err.print(IdeLogLevel.ERROR.getEndColor());
×
114
      System.err.print(IdeLogLevel.ERROR.getStartColor());
×
115
    }
116
    System.err.println(message);
×
117
    if (colored) {
×
118
      System.err.print(IdeLogLevel.ERROR.getEndColor());
×
119
    }
120
  }
×
121

122
  /**
123
   * @return {@code true} if colored logging is used, {@code false} otherwise.
124
   */
125
  public boolean isColored() {
126

127
    return this.colored;
×
128
  }
129

130
  @Override
131
  public String log(Throwable error, String message, Object... args) {
132

133
    if (!this.enabled) {
3✔
134
      // performance optimization: do not fill in arguments if disabled
135
      return message;
2✔
136
    }
137
    String actualMessage = message;
2✔
138
    if (error != null) {
2✔
139
      if (isOmitStacktrace(error)) {
4✔
140
        if (message == null) {
2!
141
          actualMessage = error.getMessage();
3✔
142
        }
143
        error = null;
3✔
144
      } else if (message == null) {
2✔
145
        actualMessage = error.toString();
3✔
146
      }
147
    }
148
    if (actualMessage == null) {
2!
149
      actualMessage = "Internal error: Both message and error is null - nothing to log!";
×
150
      // fail fast if assertions are enabled, so developers of IDEasy will find the bug immediately but in productive use better log the error and continue
151
      assert false : actualMessage;
×
152
    } else if ((args != null) && (args.length > 0)) {
5!
153
      actualMessage = compose(actualMessage, args);
5✔
154
    }
155
    boolean accept = this.listener.onLog(this.level, actualMessage, message, args, error);
10✔
156
    if (accept) {
2✔
157
      doLog(actualMessage, error);
4✔
158
    }
159
    return actualMessage;
2✔
160
  }
161

162
  private boolean isOmitStacktrace(Throwable error) {
163

164
    return (error instanceof CliException);
3✔
165
  }
166

167
  /**
168
   * @param message the formatted message to log.
169
   * @param error the optional {@link Throwable} to log or {@code null} for no error.
170
   */
171
  protected abstract void doLog(String message, Throwable error);
172

173
  /**
174
   * @param argFormatter the new {@link IdeLogArgFormatter} to use.
175
   */
176
  void setArgFormatter(IdeLogArgFormatter argFormatter) {
177

178
    this.argFormatter = argFormatter;
3✔
179
  }
1✔
180

181
  @Override
182
  public String toString() {
183

184
    return getClass().getSimpleName() + "@" + this.level;
×
185
  }
186

187
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc