]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.scl.compiler/src/org/simantics/scl/compiler/internal/codegen/utils/LoggingOutputStream.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.scl.compiler / src / org / simantics / scl / compiler / internal / codegen / utils / LoggingOutputStream.java
1 package org.simantics.scl.compiler.internal.codegen.utils;
2
3 import java.io.ByteArrayOutputStream;
4 import java.io.OutputStream;
5
6 import org.slf4j.Logger;
7
8 public class LoggingOutputStream extends OutputStream {
9
10     private final ByteArrayOutputStream baos = new ByteArrayOutputStream(1000);
11     private final Logger logger;
12     private final LogLevel level;
13
14     public enum LogLevel {
15         TRACE, DEBUG, INFO, WARN, ERROR,
16     }
17
18     public LoggingOutputStream(Logger logger, LogLevel level) {
19         this.logger = logger;
20         this.level = level;
21     }
22
23     @Override
24     public void write(int b) {
25         if (b == '\n') {
26             String line = baos.toString();
27             baos.reset();
28
29             switch (level) {
30             case TRACE:
31                 logger.trace(line);
32                 break;
33             case DEBUG:
34                 logger.debug(line);
35                 break;
36             case ERROR:
37                 logger.error(line);
38                 break;
39             case INFO:
40                 logger.info(line);
41                 break;
42             case WARN:
43                 logger.warn(line);
44                 break;
45             }
46         } else {
47             baos.write(b);
48         }
49     }
50 }