brooklyn-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From geomacy <...@git.apache.org>
Subject [GitHub] brooklyn-server pull request #731: BROOKLYN-440: ssh not use StreamGobbler f...
Date Tue, 13 Jun 2017 12:37:36 GMT
Github user geomacy commented on a diff in the pull request:

    https://github.com/apache/brooklyn-server/pull/731#discussion_r121618034
  
    --- Diff: utils/common/src/main/java/org/apache/brooklyn/util/stream/LoggingOutputStream.java
---
    @@ -0,0 +1,134 @@
    +/*
    + * Licensed to the Apache Software Foundation (ASF) under one
    + * or more contributor license agreements.  See the NOTICE file
    + * distributed with this work for additional information
    + * regarding copyright ownership.  The ASF licenses this file
    + * to you under the Apache License, Version 2.0 (the
    + * "License"); you may not use this file except in compliance
    + * with the License.  You may obtain a copy of the License at
    + *
    + *     http://www.apache.org/licenses/LICENSE-2.0
    + *
    + * Unless required by applicable law or agreed to in writing,
    + * software distributed under the License is distributed on an
    + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    + * KIND, either express or implied.  See the License for the
    + * specific language governing permissions and limitations
    + * under the License.
    + */
    +package org.apache.brooklyn.util.stream;
    +
    +import java.io.FilterOutputStream;
    +import java.io.IOException;
    +import java.io.OutputStream;
    +import java.util.concurrent.atomic.AtomicBoolean;
    +
    +import org.slf4j.Logger;
    +
    +/**
    + * Wraps another output stream, intercepting the writes to log it.
    + */
    +public class LoggingOutputStream extends FilterOutputStream {
    +
    +    private static final OutputStream NOOP_OUTPUT_STREAM = new FilterOutputStream(null)
{
    +        @Override public void write(int b) throws IOException {
    +        }
    +        @Override public void flush() throws IOException {
    +        }
    +        @Override public void close() throws IOException {
    +        }        
    +    };
    +    
    +    public static Builder builder() {
    +        return new Builder();
    +    }
    +    
    +    public static class Builder {
    +        OutputStream out;
    +        Logger log;
    +        String logPrefix;
    +        
    +        public Builder outputStream(OutputStream val) {
    +            this.out = val;
    +            return this;
    +        }
    +        public Builder logger(Logger val) {
    +            this.log = val;
    +            return this;
    +        }
    +        public Builder logPrefix(String val) {
    +            this.logPrefix = val;
    +            return this;
    +        }
    +        public LoggingOutputStream build() {
    +            return new LoggingOutputStream(this);
    +        }
    +    }
    +    
    +    protected final Logger log;
    +    protected final String logPrefix;
    +    private final AtomicBoolean running = new AtomicBoolean(true);
    +    private final StringBuilder lineSoFar = new StringBuilder(16);
    +
    +    private LoggingOutputStream(Builder builder) {
    +        super(builder.out != null ? builder.out : NOOP_OUTPUT_STREAM);
    +        log = builder.log;
    +        logPrefix = (builder.logPrefix != null) ? builder.logPrefix : "";
    +      }
    +
    +    @Override
    +    public void write(int b) throws IOException {
    +        if (running.get() && b >= 0) onChar(b);
    +        out.write(b);
    +    }
    +
    +    @Override
    +    public void flush() throws IOException {
    +        try {
    +            if (lineSoFar.length() > 0) {
    +                onLine(lineSoFar.toString());
    +                lineSoFar.setLength(0);
    +            }
    +        } finally {
    +            super.flush();
    +        }
    +    }
    +    
    +    // Overriding close() because FilterOutputStream's close() method pre-JDK8 has bad
behavior:
    +    // it silently ignores any exception thrown by flush(). Instead, just close the delegate
stream.
    +    // It should flush itself if necessary.
    +    @Override
    +    public void close() throws IOException {
    +        try {
    +            onLine(lineSoFar.toString());
    +            lineSoFar.setLength(0);
    +        } finally {
    +            out.close();
    +            running.set(false);
    +        }
    +    }
    +    
    +    public void onChar(int c) {
    +        if (c=='\n' || c=='\r') {
    +            if (lineSoFar.length()>0)
    +                //suppress blank lines, so that we can treat either newline char as a
line separator
    +                //(eg to show curl updates frequently)
    +                onLine(lineSoFar.toString());
    +            lineSoFar.setLength(0);
    +        } else {
    +            lineSoFar.append((char)c);
    +        }
    +    }
    +    
    +    public void onLine(String line) {
    +        //right trim, in case there is \r or other funnies
    +        while (line.length()>0 && Character.isWhitespace(line.charAt(line.length()-1)))
    +            line = line.substring(0, line.length()-1);
    +        //right trim, in case there is \r or other funnies
    --- End diff --
    
    `left trim` here; not sure I see the need for this check, since `onChar` never appends
`\n` or `\r` to `lineSoFar`?


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

Mime
View raw message