Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[JENKINS-30088] Annotate workflow metadata logs
Originally-Committed-As: 7e2c3a0f52684f989b5c398a9ffd99739a8f7b14
- Loading branch information
Showing
4 changed files
with
178 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
job/src/main/java/org/jenkinsci/plugins/workflow/job/console/WorkflowConsoleLogger.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2015, CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package org.jenkinsci.plugins.workflow.job.console; | ||
|
||
import java.io.IOException; | ||
import java.io.PrintStream; | ||
import java.nio.charset.Charset; | ||
|
||
import hudson.model.BuildListener; | ||
|
||
/** | ||
* Console logger used to write workflow related metadata in console text. | ||
* | ||
* It wraps the regular {@link BuildListener} to add a filter that annotates any text line sent to the console through | ||
* this logger. It also adds a prefix to the line ([Workflow]). | ||
* | ||
* Annotated lines will be rendered in a lighter color so they do not interefere with the important part of the log. | ||
*/ | ||
public class WorkflowConsoleLogger { | ||
|
||
private final BuildListener listener; | ||
private final WorkflowMetadataConsoleFilter annotator; | ||
|
||
public WorkflowConsoleLogger(BuildListener listener) { | ||
this.listener = listener; | ||
this.annotator = new WorkflowMetadataConsoleFilter(listener.getLogger()); | ||
} | ||
|
||
/** | ||
* Provides access to the wrapped listener logger. | ||
* @return the logger print stream | ||
*/ | ||
public PrintStream getLogger() { | ||
return listener.getLogger(); | ||
} | ||
|
||
/** | ||
* Sends an annotated log message to the console after adding the prefix [Workflow]. | ||
* @param message the message to wrap and annotate. | ||
*/ | ||
public void log(String message) { | ||
logAnnot(WorkflowRunConsoleNote.CONSOLE_NOTE_PREFIX, message); | ||
} | ||
|
||
private void logAnnot(String prefix, String message) { | ||
byte[] msg = String.format("%s%s%n", prefix, message).getBytes(Charset.defaultCharset()); | ||
try { | ||
annotator.eol(msg, msg.length); | ||
} catch (IOException e) { | ||
listener.getLogger().println("Problem with writing into console log: " + e.getMessage()); | ||
} | ||
} | ||
} |
55 changes: 55 additions & 0 deletions
55
...c/main/java/org/jenkinsci/plugins/workflow/job/console/WorkflowMetadataConsoleFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2015, CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package org.jenkinsci.plugins.workflow.job.console; | ||
|
||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
|
||
import hudson.console.LineTransformationOutputStream; | ||
|
||
/** | ||
* It transforms workflow metadata log messages through {@link WorkflowRunConsoleNote}. | ||
*/ | ||
public class WorkflowMetadataConsoleFilter extends LineTransformationOutputStream { | ||
|
||
private final OutputStream out; | ||
|
||
public WorkflowMetadataConsoleFilter(OutputStream out) { | ||
this.out = out; | ||
} | ||
|
||
@Override | ||
protected void eol(byte[] b, int len) throws IOException { | ||
new WorkflowRunConsoleNote().encodeTo(out); | ||
out.write(b, 0, len); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
super.close(); | ||
out.close(); | ||
} | ||
|
||
} |
51 changes: 41 additions & 10 deletions
51
job/src/main/java/org/jenkinsci/plugins/workflow/job/console/WorkflowRunConsoleNote.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,62 @@ | ||
/* | ||
* The MIT License | ||
* | ||
* Copyright (c) 2015, CloudBees, Inc. | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining a copy | ||
* of this software and associated documentation files (the "Software"), to deal | ||
* in the Software without restriction, including without limitation the rights | ||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
* copies of the Software, and to permit persons to whom the Software is | ||
* furnished to do so, subject to the following conditions: | ||
* | ||
* The above copyright notice and this permission notice shall be included in | ||
* all copies or substantial portions of the Software. | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
* THE SOFTWARE. | ||
*/ | ||
|
||
package org.jenkinsci.plugins.workflow.job.console; | ||
|
||
import org.jenkinsci.plugins.workflow.job.WorkflowRun; | ||
|
||
import hudson.Extension; | ||
import hudson.MarkupText; | ||
import hudson.console.ConsoleAnnotationDescriptor; | ||
import hudson.console.ConsoleAnnotator; | ||
import hudson.console.ConsoleNote; | ||
import hudson.model.Run; | ||
|
||
/** | ||
* Console note for Workflow metadata specific messages. | ||
* See {@link WorkflowConsoleLogger} for more information. | ||
*/ | ||
public class WorkflowRunConsoleNote extends ConsoleNote<Run<?, ?>> { | ||
|
||
/** | ||
* Prefix used in metadata lines. | ||
*/ | ||
public static final String CONSOLE_NOTE_PREFIX = "[Workflow] "; | ||
|
||
/** | ||
* CSS color selector. | ||
*/ | ||
private static final String TEXT_COLOR = "9A9999"; | ||
|
||
@Override | ||
public ConsoleAnnotator<Run<?,?>> annotate(Run<?, ?> context, MarkupText text, int charPos) { | ||
if (context instanceof WorkflowRun) { | ||
// TODO | ||
if (text.getText().startsWith(CONSOLE_NOTE_PREFIX)) { | ||
text.addMarkup(0, text.length(), "<span style=\"color:#"+ TEXT_COLOR +"\">", "</span>"); | ||
} | ||
} | ||
return null; | ||
} | ||
|
||
@Extension | ||
public static final class DescriptorImpl extends ConsoleAnnotationDescriptor { | ||
public String getDisplayName() { | ||
return "Workflow Console Note"; | ||
} | ||
} | ||
|
||
private static final long serialVersionUID = 1L; | ||
|
||
} |