File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\util\LogTaskListener.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2009, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24
25 package hudson.util;
26
27 import hudson.console.ConsoleNote;
28 import hudson.model.TaskListener;
29 import java.io.ByteArrayOutputStream;
30 import java.io.IOException;
31 import java.io.OutputStream;
32 import java.io.PrintStream;
33 import java.io.PrintWriter;
34 import java.io.Serializable;
35 import java.util.logging.Level;
36 import java.util.logging.LogRecord;
37 import java.util.logging.Logger;
38
39 /**
40 * {@link TaskListener} which sends messages to a {@link Logger}.
41 */
42 public class LogTaskListener extends AbstractTaskListener implements Serializable {
43
44 private final TaskListener delegate;
45
46 public LogTaskListener(Logger logger, Level level) {
47 delegate = new StreamTaskListener(new LogOutputStream(logger, level, new Throwable().getStackTrace()[1]));
48 }
49
50 public PrintStream getLogger() {
51 return delegate.getLogger();
52 }
53
54 public PrintWriter error(String msg) {
55 return delegate.error(msg);
56 }
57
58 public PrintWriter error(String format, Object... args) {
59 return delegate.error(format, args);
60 }
61
62 public PrintWriter fatalError(String msg) {
63 return delegate.fatalError(msg);
64 }
65
66 public PrintWriter fatalError(String format, Object... args) {
67 return delegate.fatalError(format, args);
68 }
69
70 public void annotate(ConsoleNote ann) {
71 // no annotation support
72 }
73
74 public void close() {
75 delegate.getLogger().close();
76 }
77
78 private static class LogOutputStream extends OutputStream {
79
80 private final Logger logger;
81 private final Level level;
82 private final StackTraceElement caller;
83 private final ByteArrayOutputStream baos = new ByteArrayOutputStream();
84
85 public LogOutputStream(Logger logger, Level level, StackTraceElement caller) {
86 this.logger = logger;
87 this.level = level;
88 this.caller = caller;
89 }
90
91 public void write(int b) throws IOException {
92 if (b == '\r' || b == '\n') {
93 flush();
94 } else {
95 baos.write(b);
96 }
97 }
98
99 @Override
100 public void flush() throws IOException {
101 if (baos.size() > 0) {
102 LogRecord lr = new LogRecord(level, baos.toString());
103 lr.setLoggerName(logger.getName());
104 lr.setSourceClassName(caller.getClassName());
105 lr.setSourceMethodName(caller.getMethodName());
106 logger.log(lr);
107 }
108 baos.reset();
109 }
110
111 @Override
112 public void close() throws IOException {
113 flush();
114 }
115
116 }
117
118 private static final long serialVersionUID = 1L;
119 }
120