-
-
Notifications
You must be signed in to change notification settings - Fork 3
External Logging API: Align with new Core version, fix Findbugs #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
oleg-nenashev
merged 10 commits into
jenkinsci:referenceImpl
from
oleg-nenashev:referenceImpl
Jan 3, 2019
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
90460a0
Exrernal Logging API’s PipelineStorage now consults with Run Items
oleg-nenashev 42d1b1e
Add TODOs after the discussion with @jglick
oleg-nenashev b1063a3
Merge commit '32dc947ab6d41449d37189cb53b6a07b0d296f2f' into referenc…
oleg-nenashev 67aca31
Move the getLogFile() compatibility caching method to the External Lo…
oleg-nenashev d942beb
Add TODO to reconsider the data format
oleg-nenashev 7aa0582
Pick the deployed core versions
oleg-nenashev 60d3155
Fix FindBugs so that the PR builder can pass
oleg-nenashev e60a3ac
Reference the Incrementalify ticket
oleg-nenashev ea829fa
switch to the implementation with a single LogStorage in the core
oleg-nenashev 9d73bdb
Merge commit 'e60a3ac91a61d884e1fcded5d6b3741b75c1c87b' into referenc…
oleg-nenashev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
This file contains hidden or 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 |
|---|---|---|
|
|
@@ -15,6 +15,7 @@ public class Event { | |
| final long timestamp; | ||
| final long id; | ||
|
|
||
| //TODO:Consider <String,String> so that Key-Value storages could be quickly implemented | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes please; I do not think implementations can be expected to take arbitrary Java objects. |
||
| Map<String, Serializable> data = new HashMap<>(); | ||
|
|
||
| public Event(long id, String message, long timestamp) { | ||
|
|
||
107 changes: 105 additions & 2 deletions
107
src/main/java/io/jenkins/plugins/extlogging/api/ExternalLogBrowser.java
This file contains hidden or 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,18 +1,121 @@ | ||
| package io.jenkins.plugins.extlogging.api; | ||
|
|
||
| import jenkins.model.logging.LogBrowser; | ||
| import hudson.console.AnnotatedLargeText; | ||
| import hudson.console.ConsoleNote; | ||
| import hudson.model.Run; | ||
| import jenkins.model.logging.Loggable; | ||
|
|
||
| import javax.annotation.CheckForNull; | ||
| import javax.annotation.Nonnull; | ||
| import java.io.BufferedReader; | ||
| import java.io.ByteArrayInputStream; | ||
| import java.io.ByteArrayOutputStream; | ||
| import java.io.File; | ||
| import java.io.FileOutputStream; | ||
| import java.io.IOException; | ||
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.io.Reader; | ||
| import java.util.LinkedList; | ||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Base abstract class for External Log Browsers. | ||
| * This implementation also implements a convenience method for caching of files. | ||
| * @author Oleg Nenashev | ||
| * @since TODO | ||
| */ | ||
| public abstract class ExternalLogBrowser extends LogBrowser { | ||
| public abstract class ExternalLogBrowser<T extends Loggable> extends LoggableHandler { | ||
|
|
||
| public ExternalLogBrowser(@Nonnull Loggable loggable) { | ||
| super(loggable); | ||
| } | ||
|
|
||
| //TODO: Push warnings to Telemetry API | ||
| //Pipeline: LOGGER.log(Level.WARNING, "Avoid calling getLogFile on " + this, | ||
| // new UnsupportedOperationException()); | ||
| /** | ||
| * Gets log as a file. | ||
| * This is a compatibility method, which is used in {@link Run#getLogFile()}. | ||
| * {@link ExternalLogBrowser} implementations may provide it, e.g. by creating temporary files if needed. | ||
| * @return Log file. If it does not exist, {@link IOException} should be thrown | ||
| * @throws IOException Log file cannot be retrieved | ||
| * @deprecated The method is available for compatibility purposes only | ||
| */ | ||
| public File getLogFile() throws IOException { | ||
| File f = File.createTempFile("deprecated", ".log", getOwner().getTmpDir()); | ||
| f.deleteOnExit(); | ||
| try (OutputStream os = new FileOutputStream(f)) { | ||
| overallLog().writeRawLogTo(0, os); | ||
| } | ||
| return f; | ||
| } | ||
|
|
||
| /** | ||
| * Gets log for an object. | ||
| * @return Created log or {@link jenkins.model.logging.impl.BrokenAnnotatedLargeText} if it cannot be retrieved | ||
| */ | ||
| @Nonnull | ||
| public abstract AnnotatedLargeText<T> overallLog(); | ||
|
|
||
| //TODO: jglick requests justification of why it needs to be in the core | ||
| /** | ||
| * Gets log for a part of the object. | ||
| * @param stepId Identifier of the step to be displayed. | ||
| * It may be Pipeline step or other similar abstraction | ||
| * @param completed indicates that the step is completed | ||
| * @return Created log or {@link jenkins.model.logging.impl.BrokenAnnotatedLargeText} if it cannot be retrieved | ||
| */ | ||
| @Nonnull | ||
| public abstract AnnotatedLargeText<T> stepLog(@CheckForNull String stepId, boolean completed); | ||
|
|
||
| public InputStream getLogInputStream() throws IOException { | ||
| // Inefficient but probably rarely used anyway. | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| overallLog().writeRawLogTo(0, baos); | ||
| return new ByteArrayInputStream(baos.toByteArray()); | ||
| } | ||
|
|
||
| public Reader getLogReader() throws IOException { | ||
| // As above. | ||
| return overallLog().readAll(); | ||
| } | ||
|
|
||
| @SuppressWarnings("deprecation") | ||
| public String getLog() throws IOException { | ||
| ByteArrayOutputStream baos = new ByteArrayOutputStream(); | ||
| overallLog().writeRawLogTo(0, baos); | ||
| return baos.toString(loggable.getCharset().name()); | ||
| } | ||
|
|
||
| public List<String> getLog(int maxLines) throws IOException { | ||
| int lineCount = 0; | ||
| List<String> logLines = new LinkedList<>(); | ||
| if (maxLines == 0) { | ||
| return logLines; | ||
| } | ||
| try (BufferedReader reader = new BufferedReader(getLogReader())) { | ||
| for (String line = reader.readLine(); line != null; line = reader.readLine()) { | ||
| logLines.add(line); | ||
| ++lineCount; | ||
| if (lineCount > maxLines) { | ||
| logLines.remove(0); | ||
| } | ||
| } | ||
| } | ||
| if (lineCount > maxLines) { | ||
| logLines.set(0, "[...truncated " + (lineCount - (maxLines - 1)) + " lines...]"); | ||
| } | ||
| return ConsoleNote.removeNotes(logLines); | ||
| } | ||
|
|
||
| /** | ||
| * Deletes the log in the storage. | ||
| * @return {@code true} if the log was deleted. | ||
| * {@code false} if Log deletion is not supported. | ||
| * @throws IOException Failed to delete the log. | ||
| */ | ||
| public abstract boolean deleteLog() throws IOException; | ||
|
|
||
|
|
||
| } |
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
79 changes: 79 additions & 0 deletions
79
src/main/java/io/jenkins/plugins/extlogging/api/LoggableHandler.java
This file contains hidden or 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,79 @@ | ||
| /* | ||
| * The MIT License | ||
| * | ||
| * Copyright 2018 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 io.jenkins.plugins.extlogging.api; | ||
|
|
||
| import jenkins.model.logging.Loggable; | ||
| import org.kohsuke.accmod.Restricted; | ||
| import org.kohsuke.accmod.restrictions.Beta; | ||
| import org.kohsuke.stapler.export.Exported; | ||
| import org.kohsuke.stapler.export.ExportedBean; | ||
|
|
||
| import javax.annotation.CheckForNull; | ||
| import javax.annotation.Nonnull; | ||
|
|
||
| /** | ||
| * Object which operates with {@link Loggable} items. | ||
| * @param <TLoggable> Loggable type | ||
| * @author Oleg Nenashev | ||
| * @since TODO | ||
| */ | ||
| @ExportedBean | ||
| @Restricted(Beta.class) | ||
| public abstract class LoggableHandler <TLoggable extends Loggable> { | ||
|
|
||
| protected transient TLoggable loggable; | ||
|
|
||
| public LoggableHandler(@Nonnull TLoggable loggable) { | ||
| this.loggable = loggable; | ||
| } | ||
|
|
||
| @Exported | ||
| public String getId() { | ||
| return getClass().getName(); | ||
| } | ||
|
|
||
| /** | ||
| * Called when the owner is loaded from disk. | ||
| * The owner may be persisted on the disk, so the build reference should be {@code transient} (quasi-{@code final}) and restored here. | ||
| * @param loggable an owner to which this component is associated. | ||
| */ | ||
| public void onLoad(@Nonnull TLoggable loggable) { | ||
| this.loggable = loggable; | ||
| } | ||
|
|
||
| public static <T extends Loggable> void onLoad(@Nonnull T loggable, @CheckForNull LoggableHandler<T> logHandler) { | ||
| if (logHandler != null) { | ||
| logHandler.onLoad(loggable); | ||
| } | ||
| } | ||
|
|
||
| @Nonnull | ||
| protected TLoggable getOwner() { | ||
| if (loggable == null) { | ||
| throw new IllegalStateException("Owner has not been assigned to the object yet"); | ||
| } | ||
| return loggable; | ||
|
|
||
| } | ||
| } |
This file contains hidden or 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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should not take more than a moment right?