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

devonfw / IDEasy / 11963246771

21 Nov 2024 11:10PM UTC coverage: 67.428% (+0.2%) from 67.239%
11963246771

push

github

web-flow
#754: bullet proof solution for processable output and logging (#777)

2487 of 4028 branches covered (61.74%)

Branch coverage included in aggregate %.

6454 of 9232 relevant lines covered (69.91%)

3.09 hits per line

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

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

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

8
  /** @see #getLevel() */
9
  protected final IdeLogLevel level;
10

11
  protected final IdeLogExceptionDetails exceptionDetails;
12

13
  final IdeLogListener listener;
14

15
  protected final boolean colored;
16

17
  private boolean enabled;
18

19
  private int count;
20

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

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

40
  @Override
41
  public IdeLogLevel getLevel() {
42

43
    return this.level;
×
44
  }
45

46
  @Override
47
  public boolean isEnabled() {
48

49
    return this.enabled;
3✔
50
  }
51

52
  void setEnabled(boolean enabled) {
53

54
    this.enabled = enabled;
3✔
55
  }
1✔
56

57

58
  @Override
59
  public int getCount() {
60

61
    return this.count;
×
62
  }
63

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

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

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

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

110
  private void warning(String message) {
111

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

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

128
    return this.colored;
×
129
  }
130

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

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

157
  /**
158
   * @param message the formatted message to log.
159
   * @param error the optional {@link Throwable} to log or {@code null} for no error.
160
   */
161
  protected abstract void doLog(String message, Throwable error);
162

163
  @Override
164
  public String toString() {
165

166
    return getClass().getSimpleName() + "@" + this.level;
×
167
  }
168

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