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

devonfw / IDEasy / 13063267543

30 Jan 2025 11:34PM UTC coverage: 68.379% (-0.2%) from 68.557%
13063267543

push

github

web-flow
#954: improve repository support (#990)

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

2857 of 4597 branches covered (62.15%)

Branch coverage included in aggregate %.

7391 of 10390 relevant lines covered (71.14%)

3.1 hits per line

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

66.96
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 int count;
22

23
  /**
24
   * The constructor.
25
   *
26
   * @param level the {@link #getLevel() log-level}.
27
   */
28
  public AbstractIdeSubLogger(IdeLogLevel level, boolean colored, IdeLogExceptionDetails exceptionDetails, IdeLogListener listener) {
29

30
    super();
2✔
31
    this.level = level;
3✔
32
    this.exceptionDetails = exceptionDetails;
3✔
33
    if (listener == null) {
2✔
34
      this.listener = IdeLogListenerNone.INSTANCE;
4✔
35
    } else {
36
      this.listener = listener;
3✔
37
    }
38
    this.colored = colored;
3✔
39
    this.enabled = true;
3✔
40
  }
1✔
41

42
  @Override
43
  public IdeLogLevel getLevel() {
44

45
    return this.level;
×
46
  }
47

48
  @Override
49
  public boolean isEnabled() {
50

51
    return this.enabled;
3✔
52
  }
53

54
  void setEnabled(boolean enabled) {
55

56
    this.enabled = enabled;
3✔
57
  }
1✔
58

59

60
  @Override
61
  public int getCount() {
62

63
    return this.count;
×
64
  }
65

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

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

106
  private void invalidMessage(String message, boolean more, Object[] args) {
107

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

112
  private void warning(String message) {
113

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

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

130
    return this.colored;
×
131
  }
132

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

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

181
    return getClass().getSimpleName() + "@" + this.level;
×
182
  }
183

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

© 2025 Coveralls, Inc