1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32 package org.spf4j.os;
33
34 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35 import java.io.BufferedReader;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.io.InputStreamReader;
39 import java.nio.charset.Charset;
40 import java.util.logging.Level;
41 import java.util.logging.Logger;
42 import javax.annotation.Nullable;
43
44
45
46
47 @SuppressFBWarnings("LO_SUSPECT_LOG_PARAMETER")
48 public final class LoggingProcessHandler implements ProcessHandler<Void, Void> {
49
50 private Logger log;
51
52 @SuppressFBWarnings("EI_EXPOSE_REP2")
53 public LoggingProcessHandler(final Logger log) {
54 this.log = log;
55 }
56
57 public void started(final Process p) {
58 int pid = ProcessUtil.getPid(p);
59 log.log(Level.FINE, "Started {0} with pid={1} ", new Object[]{p, pid});
60 this.log = Logger.getLogger(log.getName() + '.' + pid);
61 }
62
63 @Override
64 @Nullable
65 public Void handleStdOut(final InputStream is) throws IOException {
66 BufferedReader reader = new BufferedReader(new InputStreamReader(is, Charset.defaultCharset()));
67 String line;
68 while ((line = reader.readLine()) != null) {
69 log.fine(line);
70 }
71 return null;
72 }
73
74 @Override
75 @Nullable
76 public Void handleStdErr(final InputStream stderr) throws IOException {
77 BufferedReader reader = new BufferedReader(new InputStreamReader(stderr, Charset.defaultCharset()));
78 String line;
79 while ((line = reader.readLine()) != null) {
80 log.severe(line);
81 }
82 return null;
83 }
84
85 @Override
86 public String toString() {
87 return "LoggingProcessHandler{" + "log=" + log + '}';
88 }
89
90 }