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

1 package hudson.cli;
2
3 import hudson.Extension;
4 import hudson.console.AnnotatedLargeText;
5 import hudson.model.AbstractBuild;
6 import hudson.model.AbstractProject;
7 import hudson.model.Item;
8 import hudson.model.PermalinkProjectAction.Permalink;
9 import hudson.util.IOUtils;
10 import org.kohsuke.args4j.Argument;
11 import org.kohsuke.args4j.CmdLineException;
12 import org.kohsuke.args4j.Option;
13
14 import java.io.IOException;
15 import java.io.InputStream;
16 import java.io.InputStreamReader;
17 import java.io.OutputStreamWriter;
18 import java.io.PrintStream;
19
20 /**
21 * cat/tail/head of the console output.
22 *
23 * @author Kohsuke Kawaguchi
24 */
25 @Extension
26 public class ConsoleCommand extends CLICommand {
27 @Override
28 public String getShortDescription() {
29 return "Retrieves console output of a build";
30 }
31
32 @Argument(metaVar="JOB",usage="Name of the job",required=true)
33 public AbstractProject<?,?> job;
34
35 @Argument(metaVar="BUILD",usage="Build number or permalink to point to the build. Defaults to the last build",required=false,index=1)
36 public String build="lastBuild";
37
38 @Option(name="-f",usage="If the build is in progress, stay around and append console output as it comes, like 'tail -f'")
39 public boolean follow = false;
40
41 @Option(name="-n",metaVar="N",usage="Display the last N lines")
42 public int n = -1;
43
44 protected int run() throws Exception {
45 job.checkPermission(Item.BUILD);
46
47 AbstractBuild<?,?> run;
48
49 try {
50 int n = Integer.parseInt(build);
51 run = job.getBuildByNumber(n);
52 if (run==null)
53 throw new CmdLineException("No such build #"+n);
54 } catch (NumberFormatException e) {
55 // maybe a permalink?
56 Permalink p = job.getPermalinks().get(build);
57 if (p!=null) {
58 run = (AbstractBuild)p.resolve(job);
59 if (run==null)
60 throw new CmdLineException("Permalink "+build+" produced no build");
61 } else {
62 Permalink nearest = job.getPermalinks().findNearest(build);
63 throw new CmdLineException(String.format("Not sure what you meant by \"%s\". Did you mean \"%s\"?", build, nearest.getId()));
64 }
65 }
66
67 OutputStreamWriter w = new OutputStreamWriter(stdout, getClientCharset());
68 try {
69 long pos = n>=0 ? seek(run) : 0;
70
71 if (follow) {
72 AnnotatedLargeText logText;
73 do {
74 logText = run.getLogText();
75 pos = logText.writeLogTo(pos, w);
76 } while (!logText.isComplete());
77 } else {
78 InputStream in = run.getLogInputStream();
79 IOUtils.skip(in,pos);
80 IOUtils.copy(new InputStreamReader(in,run.getCharset()),w);
81 }
82 } finally {
83 w.flush(); // this pointless flush needed to work around SSHD-154
84 w.close();
85 }
86
87 return 0;
88 }
89
90 /**
91 * Find the byte offset in the log input stream that marks "last N lines".
92 */
93 private long seek(AbstractBuild<?, ?> run) throws IOException {
94 class RingBuffer {
95 long[] lastNlines = new long[n];
96 int ptr=0;
97
98 RingBuffer() {
99 for (int i=0; i<n; i++)
100 lastNlines[i] = -1;
101 }
102
103 void add(long pos) {
104 lastNlines[ptr] = pos;
105 ptr = (ptr+1)%lastNlines.length;
106 }
107
108 long get() {
109 long v = lastNlines[ptr];
110 if (v<0) return lastNlines[0]; // didn't even wrap around
111 return v;
112 }
113 }
114 RingBuffer rb = new RingBuffer();
115
116 InputStream in = run.getLogInputStream();
117 try {
118 byte[] buf = new byte[4096];
119 int len;
120 byte prev=0;
121 long pos=0;
122 boolean prevIsNL = false;
123 while ((len=in.read(buf))>=0) {
124 for (int i=0; i<len; i++) {
125 byte ch = buf[i];
126 boolean isNL = ch=='\r' || ch=='\n';
127 if (!isNL && prevIsNL) rb.add(pos);
128 if (isNL && prevIsNL && !(prev=='\r' && ch=='\n')) rb.add(pos);
129 pos++;
130 prev = ch;
131 prevIsNL = isNL;
132 }
133 }
134
135 return rb.get();
136 } finally {
137 IOUtils.closeQuietly(in);
138 }
139 }
140
141 @Override
142 protected void printUsageSummary(PrintStream stderr) {
143 stderr.println(
144 "Produces the console output of a specific build to stdout, as if you are doing 'cat build.log'"
145 );
146 }
147 }
148