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

devonfw / IDEasy / 17730250095

15 Sep 2025 10:38AM UTC coverage: 68.686% (+0.01%) from 68.674%
17730250095

push

github

web-flow
#1460: added --no-colors option to disable colored logging and fixed wix logging (#1483)

3401 of 5419 branches covered (62.76%)

Branch coverage included in aggregate %.

8880 of 12461 relevant lines covered (71.26%)

3.13 hits per line

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

69.3
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 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
  void setColored(boolean colored) {
64
    
65
    this.colored = colored;
3✔
66
  }
1✔
67

68
  /**
69
   * Should only be used internally by logger implementation.
70
   *
71
   * @param message the message template.
72
   * @param args the dynamic arguments to fill in.
73
   * @return the resolved message with the parameters filled in.
74
   */
75
  protected String compose(String message, Object... args) {
76

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

108
  private void invalidMessage(String message, boolean more, Object[] args) {
109

110
    warning("Invalid log message with " + args.length + " argument(s) but " + (more ? "more" : "less")
×
111
        + " placeholders: " + message);
112
  }
×
113

114
  private void warning(String message) {
115

116
    boolean colored = isColored();
×
117
    if (colored) {
×
118
      System.err.print(IdeLogLevel.ERROR.getEndColor());
×
119
      System.err.print(IdeLogLevel.ERROR.getStartColor());
×
120
    }
121
    System.err.println(message);
×
122
    if (colored) {
×
123
      System.err.print(IdeLogLevel.ERROR.getEndColor());
×
124
    }
125
  }
×
126

127
  /**
128
   * @return {@code true} if colored logging is used, {@code false} otherwise.
129
   */
130
  public boolean isColored() {
131

132
    return this.colored;
×
133
  }
134

135
  @Override
136
  public String log(Throwable error, String message, Object... args) {
137

138
    if (!this.enabled) {
3✔
139
      // performance optimization: do not fill in arguments if disabled
140
      return message;
2✔
141
    }
142
    String actualMessage = message;
2✔
143
    if (error != null) {
2✔
144
      if (isOmitStacktrace(error)) {
4✔
145
        if (message == null) {
2!
146
          actualMessage = error.getMessage();
3✔
147
        }
148
        error = null;
3✔
149
      } else if (message == null) {
2✔
150
        actualMessage = error.toString();
3✔
151
      }
152
    }
153
    if (actualMessage == null) {
2!
154
      actualMessage = "Internal error: Both message and error is null - nothing to log!";
×
155
      // 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
156
      assert false : actualMessage;
×
157
    } else if ((args != null) && (args.length > 0)) {
5!
158
      actualMessage = compose(actualMessage, args);
5✔
159
    }
160
    boolean accept = this.listener.onLog(this.level, actualMessage, message, args, error);
10✔
161
    if (accept) {
2✔
162
      doLog(actualMessage, error);
4✔
163
    }
164
    return actualMessage;
2✔
165
  }
166

167
  private boolean isOmitStacktrace(Throwable error) {
168

169
    return (error instanceof CliException);
3✔
170
  }
171

172
  /**
173
   * @param message the formatted message to log.
174
   * @param error the optional {@link Throwable} to log or {@code null} for no error.
175
   */
176
  protected abstract void doLog(String message, Throwable error);
177

178
  /**
179
   * @param argFormatter the new {@link IdeLogArgFormatter} to use.
180
   */
181
  void setArgFormatter(IdeLogArgFormatter argFormatter) {
182

183
    this.argFormatter = argFormatter;
3✔
184
  }
1✔
185

186
  @Override
187
  public String toString() {
188

189
    return getClass().getSimpleName() + "@" + this.level;
×
190
  }
191

192
}
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