1 package cz.cuni.amis.utils;
2
3 import java.io.BufferedReader;
4 import java.io.IOException;
5 import java.io.InputStream;
6 import java.io.InputStreamReader;
7 import java.util.logging.Level;
8 import java.util.logging.Logger;
9
10
11
12
13
14
15 public class StreamSink extends Thread {
16
17 protected InputStream os = null;
18 protected Logger log;
19 protected String logId;
20 protected Level logLevel = Level.INFO;
21
22
23
24
25
26
27 public StreamSink(String name, InputStream os) {
28 this(name, os, null);
29 }
30
31
32
33
34
35
36
37
38
39 public StreamSink(String name, InputStream os, Logger log) {
40 this(name, os, log, null);
41 }
42
43
44
45
46
47
48
49
50
51
52 public StreamSink(String name, InputStream os, Logger log, String logId) {
53 super(name);
54 this.log = log;
55 this.logId = logId;
56 this.os = os;
57 }
58
59 public Level getLogLevel() {
60 return logLevel;
61 }
62
63 public StreamSink setLogLevel(Level logLevel) {
64 NullCheck.check(logLevel, "logLevel");
65 this.logLevel = logLevel;
66 return this;
67 }
68
69 public Logger getLog() {
70 return log;
71 }
72
73 protected StreamSink setLog(Logger log) {
74 this.log = log;
75 return this;
76 }
77
78 public String getLogId() {
79 return logId;
80 }
81
82 protected StreamSink setLogId(String logId) {
83 this.logId = logId;
84 return this;
85 }
86
87 protected void handleInput(String str) {
88 if (log != null && log.isLoggable(Level.INFO)) {
89 if (logId != null) {
90 log.info(logId + " " + str);
91 } else {
92 log.info(str);
93 }
94 }
95 }
96
97 @Override
98 public void run() {
99 BufferedReader stdInput = new BufferedReader(new InputStreamReader(os));
100
101 String s = null;
102 try {
103 while ((s = stdInput.readLine()) != null) {
104 handleInput(s);
105 }
106 os.close();
107 } catch (IOException ex) {
108
109
110 }
111 }
112 }