1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.scala_tools.maven;
17
18 import com.sun.org.apache.xalan.internal.xsltc.dom.MatchingIterator;
19 import java.io.BufferedReader;
20 import java.io.IOException;
21 import java.io.InputStream;
22 import java.io.InputStreamReader;
23 import java.io.PrintWriter;
24
25 import java.util.regex.Matcher;
26 import java.util.regex.Pattern;
27 import org.apache.maven.plugin.logging.Log;
28 import org.codehaus.plexus.util.IOUtil;
29 import org.codehaus.plexus.util.StringUtils;
30
31 public class StreamLogger extends Thread {
32 private static final String LS = System.getProperty("line.separator");
33 private static final boolean emacsMode = StringUtils.isNotEmpty(System.getProperty("emacsMode"));
34 private static final boolean javaMode = StringUtils.isNotEmpty(System.getProperty("javaMode"));
35
36 private InputStream in_;
37 private Log log_;
38 private boolean isErr_;
39 private PrintWriter out_;
40 private Pattern pattern_ = Pattern.compile("\\s*((/|\\w:).+?):(\\d+):\\s*(?:Error|error|Warning|warning|Caution|caution):\\s(.+)");
41
42 public StreamLogger(InputStream in, Log log, boolean isErr) {
43 in_ = in;
44 log_ = log;
45 isErr_ = isErr;
46 }
47
48 @Override
49 public void run() {
50 BufferedReader reader = null;
51 try {
52 reader = new BufferedReader(new InputStreamReader(in_));
53 String line = null;
54 StringBuilder sb = null;
55 while ((line = reader.readLine()) != null) {
56 if (isErr_) {
57 if (!emacsMode && !javaMode) {
58 log_.warn(line);
59 } else {
60 if (sb == null) {
61 sb = new StringBuilder("Compilation failure"+ LS);
62 }
63 if (javaMode) {
64 Matcher matcher = pattern_.matcher(line);
65 if (matcher.matches()) {
66 line = matcher.group(1)+":["+matcher.group(3) +",1] " + matcher.group(4);
67 }
68 }
69 sb.append(LS + line);
70 }
71 } else {
72 log_.info(line);
73 }
74 }
75 if (sb != null) {
76 log_.warn(sb.toString());
77 }
78 } catch(IOException exc) {
79 throw new RuntimeException("wrap: " + exc.getMessage(), exc);
80 } finally {
81 IOUtil.close(reader);
82 IOUtil.close(in_);
83 IOUtil.close(out_);
84 }
85 }
86 }